matplotlib的基本使用

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

1、matplotlib简介

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

2、基本使用
  • 导入

  • from matplotlib import pyplot as plt

  • 绘图

    • 折线图

    • plt.plot(x, y)

    • 散点图

    • plt.scatter(x, y)

    • 条形图

    • plt.bar(x, y, width=0.2, color="orange")

    • 直方图

      • bin_width = 3 #定义组距
      • num_bins = int(max(a) - min(a)) / bin_width #组数
      • plt.hist(a, num_bins) #绘图
    • 显示图案

      • plot.show()

☞小例子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()

[外链图片转存失败(img-xl1EuwSJ-1565157789775)(https://github.com/BlackGrain/tuchuang/blob/master/Matplotlib/plt.plot.png?raw=true)]

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

3、设置图像大小
  • fig = plt.figure(figsize=(20, 8), dpi=80) 由此设置的图片大为(20*80)*(8*80)像素,即1600*640像素
4、调整x轴或y轴的刻度分布
  • plt.xticks(x)设置x的刻度
  • plt.xticks(x[::2])当刻度较为密集的时候开业使用步长取较为稀疏的刻度
    • plt.xticks(x, _x, rotation=45)参数x表示刻度,参数_x表示与刻度对应的标记字符串,参数rotation表示标记字符串的旋转角度。x _x长度一致,互相对应。当_x缺失时,坐标轴标记即显示刻度。

☞小例子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()

[外链图片转存失败(img-QOtrZcGK-1565157789781)(https://github.com/BlackGrain/tuchuang/blob/master/Matplotlib/plt.xticks.png?raw=true)]

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

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

得到的结果为:

[外链图片转存失败(img-BG5WONVh-1565157789782)(https://github.com/BlackGrain/tuchuang/blob/master/Matplotlib/plt.xticks.[::2].png?raw=true)]

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

5、设置中文显示
  • 通过matplotlib 下的font_manager解决(windows/linux/mac)
    • from matplotlib import font_manager导入font_manager模块
    • my_font = font_manger.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")实例化FontProperties,其中参数fname=“字体目录”:
      • Mac os下的系统字体目录为:/System/Library/Fonts/
      • Linux下的系统字体目录为:/usr/share/fonts
      • Windows下的系统字体目录为:C:\WINDOWS\Fonts
      • 当然,我们也可以直接把字体文件放在工作区目录中使用,方便且通用。
    • 在需要使用中文的函数中添加参数fontproperties=my_font,如:
      • plt.xticks(x, _x, rotation = 45, fontproperties=my_font)
      • 此外,有一特例,图例函数lengend()中,需要添加参数为prop=my_font

☞小例子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()

[外链图片转存失败(img-VyVWPuTd-1565157789783)(https://github.com/BlackGrain/tuchuang/blob/master/Matplotlib/中文显示.png?raw=true)]

6、为轴添加描述信息
7、自定义绘制图形的风格
  • plt.plot(x, y, color ='r', linestyle='--', linewidth=5, alpha=0.5)

    • color='r' #线条颜色

    • linestyle='--' #线条样式

    • linewidth=5 #线条宽度

    • alpha=0.5 #透明度

      颜色字符color风格字符linestyle
      r红色-实线
      g绿色虚线 破折线
      b蓝色-.点划线
      w白色:点虚线
      ''留空
      c青色
      m洋红
      y黄色
      k黑色
      00ff0016进制
      0.8灰度值字符串
8、图例的使用

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

  • plt.plot(x, y, label="label") #在plt.plot中添加参数label="label"
  • plt.legend(prop=my_font, loc="best") #参数prop=my_font定义中文字符,参数loc定义图例的生成位置,具体请见matplotlib源码注释。

☞小例子

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()

[外链图片转存失败(img-lXLoC93j-1565157789788)(https://github.com/BlackGrain/tuchuang/blob/master/Matplotlib/夏天冬天对比图.png?raw=true)]

9、折线图
10、散点图
  • 用两组数据构成多个坐标点,考察坐标点的分布,判断两变量之间是否存在某种关联或总结坐标点的分布模式。
  • **特点:**判断变量之间是否存在数量关联趋势,展示离群点(分布规律)
  • 方法:plt.scatter(x, y)

☞小例子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()

[外链图片转存失败(img-wgUTPqFb-1565157789790)(https://github.com/BlackGrain/tuchuang/blob/master/Matplotlib/天气散点图.png?raw=true)]

11、条形图
  • 排列在工作表的列或行中的数据可以绘制到条形图中。
  • 特点:绘制连离散的数据,能够一眼看出各个数据的大小,比较数据之间的差别。
  • 方法:plt.bar(x, y, width = , color = )

☞小例子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()

[外链图片转存失败(img-Zbh3TIIY-1565157789792)(https://github.com/BlackGrain/tuchuang/blob/master/Matplotlib/电影票房.png?raw=true)]

12、直方图
  • 由一系列高度不等的纵向条纹或线段表示数据分布的情况。 一般用横轴表示数据范围,纵轴表示分布情况。
  • **特点:**绘制连续性的数据,展示一组或者多组数据的分布状况(统计)
  • 方法:
    • bin_width = 3#设置组距
    • num_bins = int((max(a) - min(a))/bin_width)#分为多少组
    • plt.hist(a, num_bins)#绘图,如果有参数normed=1则按频率分布绘制
    • plt.xticks(list(range(min(a),max(a)))[::bin_width], rotation=45)#设置标签
    • plt.grid(linestyle="-.", alpha=0.5)#显示网格

☞小例子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()

[外链图片转存失败(img-qfpMlIQ6-1565157789794)(https://github.com/BlackGrain/tuchuang/blob/master/Matplotlib/电影市场分布.png?raw=true)]


补充:多子图

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两大部分。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值