Python之matplotlib学习笔记

  • 为什么要学习matplotlib,这个就不细说了,主要是实用啊,而且本人的工作也能经常用得上,因此将学习过程中遇到的方方面面记录一下,方便大家,以后自己查也方便,争取能弄的比较全乎。不知不觉又立了个flag,对自己说个呵呵先。

题外话,为什么有些blog原创审核不过,我仔细看了一下,可能是图片上有水印,可是,那个水印就是csdn自己生成的,就是我自己的blog地址啊啊啊啊啊!

  • 首先是安装: pip install matplotlib 这没什么好说的,超级简单了
  • 然后就是使用了, 常用的有折线图,柱状图,stack offset(不知道中文该怎么说),饼图,等等等等,学一个记录一个
  • 正常画图的话,首先是导入from matplotlib import pyplot as plt, 这算是一个常规写法,当然也可以自己另外取名;
  • 第一步传入 x,y坐标轴的坐标,以列表形式传入,我这里就以随机数导入进去了:
catalyst = ['Fe_1', "Fe_2", "Fe_3", "Fe_4"]  # x
conversion = [random.uniform(10, 50) for _ in range(len(catalyst))]  # y1
selectivity_C2 = [random.uniform(10, 50) for _ in range(len(catalyst))]  # y2
selectivity_C6 = [random.uniform(10, 50) for _ in range(len(catalyst))]  # y3
selectivity_C10 = [random.uniform(10, 50) for _ in range(len(catalyst))]  # y4
selectivity_coke = [random.uniform(10, 50) for _ in range(len(catalyst))]  # y5
selectivity_others = [random.uniform(10, 50) for _ in range(len(catalyst))]  # y6

(有人私信我是啥职业,自己猜~)

然后就是最简单的画图:

plt.plot(catalyst,conversion)
plt.plot(catalyst,selectivity_C2)
plt.plot(catalyst,selectivity_C6)
plt.plot(catalyst,selectivity_C10)
plt.plot(catalyst,selectivity_coke)
plt.plot(catalyst,selectivity_others)
plt.show()

以上连起来执行,实际上就可以画图了:
但是是不是还缺东西?我们想加上x y轴的标题怎么办?还有图的标题?

plt.title('The performance of different catalysts')  # title of whole picture
plt.xlabel('Catalysts')  # title of x axis
plt.ylabel('Productivity (%)')  # title of y axis

运行;

python matplotlib折线图显示点值 matplotlib折线图标记特定点_学习笔记


有点那么回事了吧,常画图的同学都知道,数据实际上是出来了,接下来就是美化了,一般美化的话,比如改字体大小,改线条样式与线型,加上legend,等等。可以通过如下方法:

plt.plot(catalyst, conversion, label="conversion", linestyle='--', linewidth=3, marker='o')

这里指定了数据的标签名,线型,线宽,marker;

plt.title('The performance of different catalysts')  
plt.xlabel('Catalysts')  # title of x axis
plt.ylabel('Productivity (%)')  # title of y axis

这里指定了图片的大标题及坐标轴题目:

linestyle:

python matplotlib折线图显示点值 matplotlib折线图标记特定点_随机数_02

marker列表:

python matplotlib折线图显示点值 matplotlib折线图标记特定点_学习笔记_03


同时加上坐标标签,这样我们再生成一下:

plt.legend()  # show the legend
plt.show()

python matplotlib折线图显示点值 matplotlib折线图标记特定点_学习笔记_04

数据是比较乱了些,因为我采用了随机数,但是功能大部分都出来了,还有把linewidth设为0, 出来的就是散点图:

python matplotlib折线图显示点值 matplotlib折线图标记特定点_随机数_05


对了,还有设置字体: 需要导入一下字体包与类实例化:

from matplotlib.font_manager import FontProperties

Arial= FontProperties(fname='C:\Windows\Fonts\Arial.ttf')
plt.title('The performance of different catalysts',fontproperties=Arial,fontsize=20)  # title of whole picture
plt.xlabel('Catalysts',fontproperties=Arial,fontsize=14)  # title of x axis
plt.ylabel('Productivity (%)',fontproperties=Arial,fontsize=14)  # title of y axis

再画图:

python matplotlib折线图显示点值 matplotlib折线图标记特定点_数据_06


太拥挤了,我想改一下y轴上限到80,加上如下代码:

plt.ylim(0,80) # set the limit boudaries of y axis

再改一下legend的位置,字体,边框

plt.legend(loc='upper left', fontsize=10, frameon=False)  # show the legend

# loc 有:
# 0: ‘best'
# 1: ‘upper right'
# 2: ‘upper left'
# 3: ‘lower left'
# 4: ‘lower right'
# 5: ‘right'
# 6: ‘center left'
# 7: ‘center right'
# 8: ‘lower center'
# 9: ‘upper center'
# 10: ‘center'

python matplotlib折线图显示点值 matplotlib折线图标记特定点_学习笔记_07


还可以加上边框颜色,阴影等:

plt.legend(loc='upper left', fontsize=10, edgecolor='blue',shadow=True)

python matplotlib折线图显示点值 matplotlib折线图标记特定点_学习笔记_08


大体上各个地方都照顾到了,以上为折线图的画法,以后随着学习的深入,还会继续更新,另外,个人喜欢紧凑布局,加上一行:

plt.tight_layout()

如下为全部源码:

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File    : Fig2A.py.py
@Time    : 2019/8/5 9:19
@Author  : Sound_of_Silence
"""
from matplotlib import pyplot as plt
import random
from matplotlib.font_manager import FontProperties

Arial = FontProperties(fname='C:\Windows\Fonts\Arial.ttf')

catalyst = ['Fe_1', "Fe_2", "Fe_3", "Fe_4"]  # x
conversion = [random.uniform(10, 50) for _ in range(len(catalyst))]  # y1
selectivity_C2 = [random.uniform(10, 50) for _ in range(len(catalyst))]  # y2
selectivity_C6 = [random.uniform(10, 50) for _ in range(len(catalyst))]  # y3
selectivity_C10 = [random.uniform(10, 50) for _ in range(len(catalyst))]  # y4
selectivity_coke = [random.uniform(10, 50) for _ in range(len(catalyst))]  # y5
selectivity_others = [random.uniform(10, 50) for _ in range(len(catalyst))]  # y6

plt.plot(catalyst, conversion, label="conversion", linestyle='--', linewidth=3, marker='o')
plt.plot(catalyst, selectivity_C2, label="selectivity_C2", linestyle='-.', linewidth=3, marker='.')
plt.plot(catalyst, selectivity_C6, label="selectivity_C6", linestyle='--', linewidth=3, marker=',')
plt.plot(catalyst, selectivity_C10, label="selectivity_C10", linestyle='--', linewidth=3, marker='8')
plt.plot(catalyst, selectivity_coke, label="selectivity_coke", linestyle='--', linewidth=3, marker='*')
plt.plot(catalyst, selectivity_others, label="selectivity_others", linestyle='--', linewidth=3, marker='D')

plt.title('The performance of different catalysts',fontproperties=Arial,fontsize=20)  # title of whole picture
plt.xlabel('Catalysts',fontproperties=Arial,fontsize=14)  # title of x axis
plt.ylabel('Productivity (%)',fontproperties=Arial,fontsize=14)  # title of y axis
plt.tight_layout()  # tight lay out
plt.ylim(0,80)
plt.legend(loc='upper left', fontsize=10, edgecolor='blue',shadow=True)  # show the legend

plt.savefig('fig1.png')
plt.show()

补充:可以在代码中加入一行plt.xkcd(),会变个样哦,很好玩~~

python matplotlib折线图显示点值 matplotlib折线图标记特定点_数据_09