今天我们给大家补充一些matplotlib的常用技能,赶紧来学习吧~
1. Figure, Axes, Axis的关系:
在第一篇文章中,大家一定看到了包含Figure,Axes,Axis等的代码,可能会一头雾水,不知道这些分别表示什么。这里就给大家仔细解释一下。掌握了这些之后,绘图就游刃有余啦。
如下图所示,Figure是绘制对象,一个Figure对象可以包含多个Axes子图,一个Axes是一个绘图区域,不加设置时,Axes为1,且每次绘图其实都是在figure上的Axes上绘图。 Axis表示坐标轴。
一个Figure对象中可以包含一个或者多个Axes对象,每个Axes对象都拥有独立的绘图区域。
一个图中更多的参数设置,详情请查阅官网(墙裂建议查阅)
2. 毕业论文中的实例图:
大家一定很好奇,在论文级别的略微复杂一些的图如何操作呢?
这就给大家分享一个实例。代码是可以直接运行的,墙裂建议大家用它来练练手,一行代码一行代码的熟悉一下,相信你们会学到很多!
如果还不会配置python的环境,请移步一文教你解决Python所有安装配置
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
### Data:
#1290-2017
a1=(21.98, 14.97, 7.42, 3.30)
a2=(24.07, 16.59, 8.41, 3.50)
a3=(19., 12.67, 6., 3. )
#1290-1850
b1=(22.10, 13.73, 6.42, 2.67)
b2=(24.34, 15.84, 7.62, 2.93)
b3=(18.64, 10.45, 4.55, 2.27)
#1850-2017
c1=(21.56, 19.16, 10.78, 5.39)
c2=(22.99, 19.54 , 11.49, 5.75)
c3=(20. , 18.75, 10. , 5. )
#1981-2017
d1=(16.22, 16.22, 13.51, 5.41)
d2=(25., 25., 25., 10.)
d3=(5.88, 5.88, 0., 0.)
### Plot:
ind = np.arange(4) # the x locations for the groups
width = 0.2 # the width of the bars
fig, ax = plt.subplots(2,2,figsize=(15, 15),dpi=100)
rects1 = ax[0,0].bar(ind - 0.2, a1, width, color='mediumseagreen', label='所有年份')
rects2 = ax[0,0].bar(ind , a2, width, color='IndianRed', label='海冰偏少年')
rects3 = ax[0,0].bar(ind + 0.2, a3, width, color='steelblue', label='海冰偏多年')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax[0,0].set_ylabel(u'强冷冬年占比(%)',fontsize=20)
ax[0,0].set_title('(a) 1289-2017',fontsize=20)
ax[0,0].set_xticks(ind)
ax[0,0].set_xticklabels(('0.75 std', '1 std', '1.5 std', '2 std'),fontsize=20)
ax[0,0].legend(loc=0,prop={'size': 14},framealpha=0.3)
ax[0,0].tick_params(labelcolor='k', labelsize='20', width=3)
ax[0,0].set_ylim(0,26)
rects1 = ax[0,1].bar(ind - 0.2, d1, width, color='mediumseagreen', label='所有年份')
rects2 = ax[0,1].bar(ind , d2, width, color='IndianRed', label='海冰偏少年')
rects3 = ax[0,1].bar(ind + 0.2, d3, width, color='steelblue', label='海冰偏多年')
# Add some text for labels, title and custom x-axis tick labels, etc.
# ax[0,1].set_ylabel(u'强冷冬年占比(%)',fontsize=20)
ax[0,1].set_title('(b) 1981-2017',fontsize=20)
ax[0,1].set_xticks(ind)
ax[0,1].set_xticklabels(('0.75 std', '1 std', '1.5 std', '2 std'),fontsize=20)
ax[0,1].legend(loc=0,prop={'size': 14},framealpha=0.3)
ax[0,1].tick_params(labelcolor='k', labelsize='20', width=3)
ax[0,1].set_ylim(0,26)
rects1 = ax[1,0].bar(ind - 0.2, b1, width, color='mediumseagreen', label='所有年份')
rects2 = ax[1,0].bar(ind , b2, width, color='IndianRed', label='海冰偏少年')
rects3 = ax[1,0].bar(ind + 0.2, b3, width, color='steelblue', label='海冰偏多年')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax[1,0].set_ylabel(u'强冷冬年占比(%)',fontsize=20)
ax[1,0].set_title('(c) 1290-1850',fontsize=20)
ax[1,0].set_xticks(ind)
ax[1,0].set_xticklabels(('0.75 std', '1 std', '1.5 std', '2 std'),fontsize=20)
ax[1,0].legend(loc=0,prop={'size': 14},framealpha=0.3)
ax[1,0].tick_params(labelcolor='k', labelsize='20', width=3)
ax[1,0].set_ylim(0,26)
rects1 = ax[1,1].bar(ind - 0.2, c1, width, color='mediumseagreen', label='所有年份')
rects2 = ax[1,1].bar(ind , c2, width, color='IndianRed', label='海冰偏少年')
rects3 = ax[1,1].bar(ind + 0.2, c3, width, color='steelblue', label='海冰偏多年')
# Add some text for labels, title and custom x-axis tick labels, etc.
# ax[3].set_ylabel(u'强冷冬年占比(%)',fontsize=20)
ax[1,1].set_title('(d) 1850-2017',fontsize=20)
ax[1,1].set_xticks(ind)
ax[1,1].set_xticklabels(('0.75 std', '1 std', '1.5 std', '2 std'),fontsize=20)
ax[1,1].legend(loc=0,prop={'size': 14},framealpha=0.3)
ax[1,1].tick_params(labelcolor='k', labelsize='20', width=3)
ax[1,1].set_ylim(0,26)
# ax[1,1].set_yticks([-1,0,1])
plt.subplots_adjust(hspace =0.128)
plt.subplots_adjust(wspace =0.078)
plt.show()
# 画图之前加上:
# 用来正常显示中文标签,u'内容'
plt.rcParams['font.sans-serif']=[u'SimHei'] # 或其他系统有的字体,比如: ['Microsoft YaHei']
# 用来正常显示负号
plt.rcParams['axes.unicode_minus']=False
# 同样,可以进行一些其他画图配置:
plt.rcParams['figure.figsize']=10,10
plt.rcParams['figure.dpi']=300
但是,但是... 以上的操作在mac上无效 !!!
因为在mac中的/Library/Fonts文件夹中,虽有多种不同的字体,但并没有显示中文的SimHei.ttf,所以需要先进行下载和安装,并修改一下matplotlibrc脚本,就可以使得matplotlib在画图时正常显示中文了。
font.family : sans-serif
font.sans-serif : SimHei, Bitstream Vera Sans, Lucida Grande,Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
同时找到axes.unicode_minus
,将True改为False,以解决负号’-‘显示为方块的问题。
# matplotlib不会每次启动时都重新扫描所有的字体文件并创建字体索引列表,
# 因此在复制完字体文件之后,需要运行下面的语句以重新创建字体索引列表
import matplotlib as mpl
from matplotlib.font_manager import _rebuild
_rebuild()
#防止中文乱码问题
mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False
在rebuild之后,可在之后使用中直接加入:
plt.rcParams['font.sans-serif']=[u'SimHei']
plt.rcParams['axes.unicode_minus']=False
绘图时,可以供大家调色选择。