注:编码中用到的国民生产总值的数据可以在国家数据网站获得。
matplotlib图像可以分为如下四层结构:
1.canvas(画板):位于最底层,导入库时就存在。
2.figure(画布):在画板之上,从这一层开始设置参数。
3.axes(子图):将画布分为不同块,实现分面绘图。
4.图表信息(构图元素):添加或修改axes上的构图信息。
一、编码风格
1.基础流程
(1)导入模块
from matplotlib import pyplot as plt
(2)创建画布和子图
pic=plt.figure(figsize=(20,12))#设置画布尺寸
ax1=pic.add_subplot(2,1,1)#将画布划分为2*1的图阵,并选择第一张。
(3)添加画布内容
title | 标题 |
xlabel | x轴名称 |
ylabel | y轴名称 |
xlim | x轴范围 |
ylim | y轴范围 |
xticks | x轴刻度的数目和取值 |
yticks | y轴刻度的数目和取值 |
legend | 图例 |
(4)图形保存和展示
plt.savefig('filepath')#保存
plt.show()#展示
补充:pic.text(x,y,text)函数可以用来在图形的指定位置添加文本标注。
2.绘图风格
pyplot的子模块style里面定义了许多预设风格。可以通过print(plt.style,available)查看所有预设风格名称。
print(plt.style.available)
>
['Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10']
使用plt.style.use函数使用风格:
plt.style.use('bmh')
二、动态rc参数
1.线条常用的rc参数
lines.linewidth | 宽度,取值0-10,默认为1.5 |
lines.linestyle | 样式 |