matplotlib的基本使用

2019-07-29  本文已影响0人  严简易

matplotlib的基本使用——学习笔记

1、matplotlib简介

matplotlib是Python最流行的底层绘图库,主要做数据可视化图表,名字取材于MATLAB[^ ①],模仿MATLAB构建。

2、基本使用

☞小例子2.1

from matplotlib import pyplot as plt
x = range(2, 26, 2)
y = [12, 13, 15, 17, 19, 20, 23, 20, 16, 13, 10, 9]
plt.plot(x, y)
plt.show()

[图片上传失败...(image-c30227-1564367650708)]

用plot绘图很简单,点位置与对应x y相同索引的坐标位置,例中即为(2, 12), (4, 13), (6, 15), (8, 17)…..

3、设置图像大小

4、调整x轴或y轴的刻度分布

☞小例子4.1

from matplotlib import pyplot as plt
x = range(2, 26, 2)
y = [12, 13, 15, 17, 19, 20, 23, 20, 16, 13, 10, 9]
_x = ['{}:00时'.format(i) for i in x]
plt.xticks(x, _x, rotation = 45)
plt.plot(x, y)
plt.show()

[图片上传失败...(image-510d24-1564367650708)]

假如我们想要x轴的刻度更稀疏一些,我们可以这样做:

  • plt.xticks(x[::2], _x[::2], rotation = 45)取步长为2,注意x与_x参数一一对应

得到的结果为:

[图片上传失败...(image-e12122-1564367650708)]

这样就大功告成啦!不过,我们还会发现一个小问题,我们的_x字符标签中的中文字"时"并没有正常显示,这是因为matplotlib默认不支持中文字符。设置中文显示方法中文请往下看。

5、设置中文显示

☞小例子5.1

from matplotlib import pyplot as plt
from matplotlib import font_manager
#实例化FontProperties
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")
x = range(2, 26, 2)
y = [12, 13, 15, 17, 19, 20, 23, 20, 16, 13, 10, 9]
_x = ['{}:00时'.format(i) for i in x]
plt.xticks(x, _x, rotation = 45, fontproperties=my_font)
plt.plot(x, y)
plt.show()

[图片上传失败...(image-24a157-1564367650708)]

6、为轴添加描述信息

7、自定义绘制图形的风格

8、图例的使用

当我们需要对多个图形进行对比时,我们会在一张图像中绘制多个图形。为了更明显地展现对比,我们需要对不同图形添加不同的图例。

☞小例子

from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")

x = range(2, 26, 2)
y_summer = [20, 21, 23, 24, 27, 30, 34, 30, 25, 23, 21, 19]
y_spring = [12, 13, 15, 17, 19, 20, 23, 20, 16, 13, 10, 9]

plt.xlabel("时间", fontproperties=my_font)
plt.ylabel("温度", fontproperties=my_font)
plt.title("春、夏季2时到24时温度的变化情况", fontproperties=my_font)

plt.plot(x, y_summer, label="夏天", linestyle='-', alpha=0.7)
plt.plot(x, y_spring, label="春天", linestyle='--', alpha=0.7)

plt.legend(prop=my_font, loc="best")

plt.show()

[图片上传失败...(image-c3c86-1564367650708)]

9、折线图

10、散点图

☞小例子10.1

from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")

# x坐标
x_3 = list(range(1, 32))
x_9 = list(range(35, 66))

# x轴标签
x_3_ticks = ["三月{}日".format(i) for i in range(1, 32)]
x_9_ticks = ["十月{}日".format(i) for i in range(1, 32)]

# y轴坐标
y_3 = [11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,15,19,21,22,22,22,23]
y_9 = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,13,6]

# x轴
x_ticks = x_3_ticks + x_9_ticks
x = x_3 + x_9
# y轴
y = y_3 + y_9

#图像大小
fig = plt.figure(figsize=(10, 4), dpi=80)

# 刻度
plt.xticks(x[::3], x_ticks[::3], rotation=45, fontproperties=my_font)

# 描述
plt.xlabel("时间", fontproperties=my_font)
plt.ylabel("天最高温度", fontproperties=my_font)
plt.title("北京市三月份、十月份每日最高温度散点图", fontproperties=my_font)

# 绘制散点图
plt.scatter(x_3, y_3, label="三月")
plt.scatter(x_9, y_9, label="十月")
# 图例
plt.legend(prop=my_font, loc="best")

plt.show()

[图片上传失败...(image-b6f186-1564367650708)]

11、条形图

☞小例子11.1

from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname = "/System/Library/Fonts/PingFang.ttc")

a = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸",
    "加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪",
    "神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传",
    "银河护卫队2","情圣","新木乃伊",]

b=[56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,
  10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]

x_ = range(len(a))
y_ = b

# 图像大小
fig = plt.figure(figsize=(20,8), dpi=80)

# 绘图
plt.bar(x_, y_, width = 0.3, color = "C")

# 标签
plt.xticks(x_, a, fontproperties=my_font, rotation=45)
plt.xlabel("电影", fontproperties=my_font)
plt.ylabel("票房(亿)", fontproperties=my_font)
plt.title("票房前十排行", fontproperties=my_font)

plt.show()

[图片上传失败...(image-ad5d81-1564367650708)]

12、直方图

☞小例子12.1

from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname = "/System/Library/Fonts/PingFang.ttc")

a=[131,  98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 
  127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115,  99, 136, 126, 134,  95, 138, 117, 111,78, 
  132, 124, 113, 150, 110, 117,  86,  95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 
  123,  86, 101,  99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 
  107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 
  138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140,  83, 110, 102,123,107, 143, 115, 136, 
  118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 
  135,115, 146, 137, 116, 103, 144,  83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 
  126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 
  141, 117, 106, 114, 121, 114, 133, 137,  92,121, 112, 146,  97, 137, 105,  98, 117, 112,  81,  97, 139, 113,
  134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112,  83,  
  94, 146, 133, 101,131, 116, 111,  84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]

# 图像大小
fig = plt.figure(figsize=(20, 8), dpi=80)

#设置组距
bin_width = 3
num_bins = int((max(a) - min(a))/bin_width) 

#绘图,如果有参数`normed=1`则按频率分布绘制
plt.hist(a, num_bins)

#设置标签
plt.xticks(list(range(min(a),max(a)))[::bin_width], rotation=45)

#显示网格
plt.grid(linestyle="-.", alpha=0.5) 
plt.show()

[图片上传失败...(image-ad354f-1564367650708)]


补充:多子图

fig, ax = plt.subplots(2, 3, sharex='col', sharey='row')
for i in range(2):
   for j in range(3):
       ax[i, j].text(0.5, 0.5, str((i, j)),fontsize=18, ha='center')

[^ ①]: MATLAB 是美国MathWorks公司出品的商业数学软件,用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境,主要包括MATLAB和Simulink两大部分。

上一篇 下一篇

猜你喜欢

热点阅读