How to use styles to change the appearance of our plots

By using the style module within pyplot, you can see that when we call the function available, we actually get a list containing a number of different styles. Let's assume that each of these different styles acts to change the attributes and the appearance of the plots:

So, by using the plot.style.use method, we can load up any one of these default style sheets. Using the ggplot (as shown in the preceding output) will actually mimic the appearance of the ggplot library, as you can see in the following code:

# Using styles
plt.style.use('dark_background')
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3))
plt.ylabel('Axis Label')
plt.subplot(222)
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

After calling the plt.style.use('ggplot') method, we have the same kind of plots shown, but the axis objects and the appearance of these has been changed fairly significantly. There exists a white grid on a gray background, the fonts have changed as well as their colors, and the default color choices have changed as well.

Hence, we see that the histogram will have different colors, as shown in the following output:

We can also change this to any other choice as well. If you're familiar with the Seaborn library, a Python library for doing more complicated analysis statistically, you can choose options that will mimic the Seaborn library:

# Using styles
plt.style.use('seaborn-talk')
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3))
plt.ylabel('Axis Label')
plt.subplot(222)
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset