我是靠谱客的博主 醉熏高山,这篇文章主要介绍Python Matplotlib学习笔记,现在分享给大家,希望可以做个参考。

matplotlib.pyplot(plt)

  • add_subplot()的作用以及plt的初步理解
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import matplotlib.pyplot as plt fig1 = plt.figure('1') # 定义一个总图fig1,图像标题更改为'1',否则默认为'Figure 1' fig2 = plt.figure('2') # 图像标题更改为'2' fig3 = plt.figure('3') # 图像标题更改为'3' # 参数:add_subplot(row, column, index) # 这里的row和column指将总图fig划分为几行几列 # index指代子图的序号及其在总图中的位置 # 所以index的最大值为row*column fig1.add_subplot(111) fig2.add_subplot(221) fig3.add_subplot(339) plt.show() # 打印图片 # 最后会输出三个总图“1”、“2”、“3”,各带有一个子图 # fig3中的子图会显示在总图的右下角
  • plt.subplots()的作用
复制代码
1
2
3
4
5
6
7
8
import matplotlib.pyplot as plt fig = plt.figure('1') # 定义一个总图fig1,并将图像标题更改为'1',否则默认为'Figure 1' ax1 = plt.subplots(2, 2) # 重新建立一个总图再添加2*2的子图 fig, ax2 = plt.subplots(2, 2) # 效果同上,不太清楚加fig, 的作用 plt.show() # 打印图片 # 最后会输出三个总图
  • 一定要注意plt.subplots()和plt.subplot(),前者多一个字母“s”,会新建一张总图,而后者不会
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import matplotlib.pyplot as plt fig = plt.figure('1') ax = plt.subplot(231) ax = plt.subplot(232) ax = plt.subplot(233) ax = plt.subplot(234) ax = plt.subplot(235) ax = plt.subplot(236) plt.show() # 打印图片 # 最后会输出一张总图“1”,并有六张顺序排列的子图 fig = plt.figure('1') ax = plt.subplot(231) ax = plt.subplot(232) # 注意:以下使用了一个plt.subplots # 一旦使用plt.subplots,就会创建一张新的总图,并且其只有两个参数 # 两个参数一定要用“,”分隔 ax = plt.subplots(2, 3) # 会产生一张铺满子图的新图 # 下面的图会覆盖plt.subplots(2, 3)中的子图 ax = plt.subplot(234) ax = plt.subplot(235) ax = plt.subplot(236) plt.show()
  • .plot()的作用及参数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import numpy as np import matplotlib.pyplot as plt a = np.asarray([[1, 2, 3, 4], [1, 2, 3, 4]]) fig, ax = plt.subplots() ax.plot(a, '^', label="tt") # label配合legend(图例函数)后显示 plt.show() '^'为关键字参数,代表正三角形图标,更多图标如下: 颜色: 'b': blue 蓝 'g': green 绿 'r': red 红 'c': cyan 蓝绿 'm': magenta 洋红 'y': yellow 黄 'k': black 黑 'w': white 白 图标: '.': point marker ',': pixel marker 'o': circle marker 'v': triangle_down marker '^': triangle_up marker '<': triangle_left marker '>': triangle_right marker '1': tri_down marker '2': tri_up marker '3': tri_left marker '4': tri_right marker 's': square marker 'p': pentagon marker '*': star marker 'h': hexagon1 marker 'H': hexagon2 marker '+': plus marker 'x': x marker 'D': diamond marker 'd': thin_diamond marker '|': vline marker '_': hline marker 线条: '-': solid line style 实线 '--': dashed line style 虚线 '-.': dash-dot line style 点画线 ':': dotted line style 点线
  • .legend(bbox_to_anchor=())

复制代码
1
2
3
4
5
6
# 接上节代码 # 将图例显示在x=1.0,y=1.0的位置 # 这里的x,y是整张图的坐标位置,即下面的图例会显示在图片右上角 # bbox_to_anchor还有两个参数:length和width,顾名思义确定图例的长和高 lgd = ax.legend(bbox_to_anchor=(1.0, 1.0)) plt.show()

最后

以上就是醉熏高山最近收集整理的关于Python Matplotlib学习笔记的全部内容,更多相关Python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部