- Mastering Matplotlib 2.x
- Benjamin Walter Keller
- 276字
- 2021-06-10 19:29:06
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')
- 數(shù)據(jù)展現(xiàn)的藝術(shù)
- Word 2000、Excel 2000、PowerPoint 2000上機(jī)指導(dǎo)與練習(xí)
- Mastering Matplotlib 2.x
- OpenStack for Architects
- 極簡AI入門:一本書讀懂人工智能思維與應(yīng)用
- 智能工業(yè)報(bào)警系統(tǒng)
- 最簡數(shù)據(jù)挖掘
- 機(jī)器人創(chuàng)新實(shí)訓(xùn)教程
- ESP8266 Home Automation Projects
- 軟件工程及實(shí)踐
- AI的25種可能
- Linux系統(tǒng)管理員工具集
- 從零開始學(xué)JavaScript
- 21天學(xué)通Linux嵌入式開發(fā)
- 玩轉(zhuǎn)PowerPoint