- 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')
- Mastercam 2017數(shù)控加工自動編程經(jīng)典實例(第4版)
- Excel 2007函數(shù)與公式自學寶典
- 教父母學會上網(wǎng)
- HBase Design Patterns
- 大數(shù)據(jù)技術(shù)入門(第2版)
- Maya極速引擎:材質(zhì)篇
- STM32嵌入式微控制器快速上手
- 新手學電腦快速入門
- Windows游戲程序設(shè)計基礎(chǔ)
- 精通數(shù)據(jù)科學算法
- Python:Data Analytics and Visualization
- Extending Ansible
- 嵌入式GUI開發(fā)設(shè)計
- 基于RPA技術(shù)財務(wù)機器人的應(yīng)用與研究
- 生成對抗網(wǎng)絡(luò)項目實戰(zhàn)