前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何用python画图--matplotlib实例与补充

如何用python画图--matplotlib实例与补充

作者头像
MeteoAI
发布2019-07-24 15:56:54
1.4K0
发布2019-07-24 15:56:54
举报
文章被收录于专栏:MeteoAI

今天我们给大家补充一些matplotlib的常用技能,赶紧来学习吧~

1. Figure, Axes, Axis的关系:

在第一篇文章中,大家一定看到了包含Figure,Axes,Axis等的代码,可能会一头雾水,不知道这些分别表示什么。这里就给大家仔细解释一下。掌握了这些之后,绘图就游刃有余啦。

如下图所示,Figure是绘制对象,一个Figure对象可以包含多个Axes子图,一个Axes是一个绘图区域,不加设置时,Axes为1,且每次绘图其实都是在figure上的Axes上绘图。 Axis表示坐标轴。

一个Figure对象中可以包含一个或者多个Axes对象,每个Axes对象都拥有独立的绘图区域。

一个图中更多的参数设置,详情请查阅官网(墙裂建议查阅)

2. 毕业论文中的实例图:

大家一定很好奇,在论文级别的略微复杂一些的图如何操作呢?

这就给大家分享一个实例。代码是可以直接运行的,墙裂建议大家用它来练练手,一行代码一行代码的熟悉一下,相信你们会学到很多!

如果还不会配置python的环境,请移步一文教你解决Python所有安装配置

代码语言:javascript
复制
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()
3. matplotlib如何正常显示中文:
许多朋友会发现,中文字符有时候在matplotlib中显示不正常,这也是正常现象,下面来教大家怎么一步一步解决这个问题。
【1】 Windows:
代码语言:javascript
复制
# 画图之前加上:
# 用来正常显示中文标签,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
【2】 Mac:

但是,但是... 以上的操作在mac上无效 !!!

因为在mac中的/Library/Fonts文件夹中,虽有多种不同的字体,但并没有显示中文的SimHei.ttf,所以需要先进行下载和安装,并修改一下matplotlibrc脚本,就可以使得matplotlib在画图时正常显示中文了。

  • 第一步:下载字体并安装:SimHei
  • 第二步:将字体文件SimHei.ttf拷贝到python安装路径/site-packages/matplotlib/mpl-data/fonts/ttf目录中
  • 第三步:修改~python安装路径/site-packages/matplotlib/mpl-data/matplotlibrc文件。找到如下两项,去掉前面的#,并在font.sans-serif冒号后面加上SimHei,如下:
代码语言:javascript
复制
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,以解决负号’-‘显示为方块的问题。

  • 第四步:在脚本运行前加入:
代码语言:javascript
复制
# 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之后,可在之后使用中直接加入:

代码语言:javascript
复制
plt.rcParams['font.sans-serif']=[u'SimHei']
plt.rcParams['axes.unicode_minus']=False
4. 更多的配色参考:

绘图时,可以供大家调色选择。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-11-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 MeteoAI 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 3. matplotlib如何正常显示中文:
    • 许多朋友会发现,中文字符有时候在matplotlib中显示不正常,这也是正常现象,下面来教大家怎么一步一步解决这个问题。
      • 【1】 Windows:
        • 【2】 Mac:
        • 4. 更多的配色参考:
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档