文章目录 [隐藏]
1.绘制子图subplot、axes
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 |
import matplotlib.pyplot as plt from matplotlib.pyplot import MultipleLocator x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 6] # subplot 在画布上按区块绘制 # axes 在画布上按比例绘制 # 子块图-1 # 2,1,1 将画布分成 2行 1列 取第1行的块 sub1 = plt.subplot(2, 1, 1, facecolor=(0.1843, 0.3098, 0.3098)) # facecolor设置背景色 sub1.set_title('子块图-1') # 子图标题 sub1.plot(x, y) # 绘制子图 # 子块图-2 sub2 = plt.subplot(2, 1, 2) # 取第2块作图 sub2.xaxis.set_major_locator(MultipleLocator(1)) # 刻度间隔 sub2.plot(x, y, marker='D') # marker更改默认标记 sub2.set_title('子块图-2') # 子插图-1 # [x,y,w,h]相对于坐标原点(默认左下)的位置。4个参数都取之0-1,表示占比。 axes1 = plt.axes([0, 0, 0.1, 0.2], facecolor='y') axes1.plot(x, y) # 绘制子坐标系, # 子插图-2 axes2 = plt.axes([0.7, 0.2, 0.1, 0.1], facecolor='g') axes2.plot(x, y, "r") # 子插图-3 axes3 = plt.axes([0.9, 0.6, 0.1, 0.4]) axes3.plot(x, y) plt.show() |
2.绘制子图add_subplot指定窗口大小和显示网格
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 |
import matplotlib.pyplot as plt from matplotlib.pyplot import MultipleLocator # 创建窗口 fig = plt.figure(num=11, figsize=(9, 5), dpi=80, facecolor='green') # 开启1个窗口,设置大小,分辨率 # 添加子块图。将窗口分为2行1列 ax1 = fig.add_subplot(2, 1, 1) # 取第1个。如果是多行多列,先从左到右取完一行,再取下一行 ax2 = fig.add_subplot(2, 1, 2) # 取第2个 # 设置子块图-1 ax1.set_title('这是块图1') # 设置标题 ax1.set_xlabel('x-轴') # 设置x轴名称 ax1.set_ylabel('y-轴') # 设置y轴名称 ax1.set_xlim(-1, 7) # 设置x轴范围 ax1.set_ylim(-2, 10) # 设置y轴范围 # ax1.axis([-6, 6, -10, 10]) # 效果同上面2行。 ax1.xaxis.set_major_locator(MultipleLocator(1)) # x轴刻度间隔 ax1.yaxis.set_major_locator(MultipleLocator(2)) # y轴刻度间隔 ax1.grid( # 显示网格 b=False, # 如果未设置其它参数,当True时才按默认配置显示。只有有其它参数,True、False、None都显示。 which='both', # major(只绘制整数)、minor(只绘制小数)、both-都绘制,默认值为major。 axis='both', # 显示哪个轴。'x','y','both' alpha=0.35, # 自加的k-v参数,透明度,值越小越透明 color='skyblue', # 自加的k-v参数,颜色 linestyle='--', # 自加的k-v参数,网格线样式 linewidth=1) # 自加的k-v参数,网格线粗细,值越小越细 # plt.grid(None) # 取消网格 x1 = [1, 2, 3, 4, 5] y1 = [2, 3, 5, 7, 6] x2 = [-1, 2, 3, 3, 5, 7] y2 = [0, 2, 4, 7, 5, 6] ax1.plot(x1, y1, marker='D', color='g', label='Line-1') # 线条1。增加线的图例。点图:marker图标 ax1.plot(x2, y2, linestyle='--', alpha=0.5, color='r', label='Line-2') # 线条2。增加线的图例 ax1.legend(loc='upper left') # 显示图例。loc设置位置 ax1.text(6, 6, '点(x=6,y=6)') # 指定位置x,y显示文字,xy对应两个轴的刻度。 # 设置子块图-2 ax2.set_title('这是块图2') ax2.text(0.45, 0.6, '这个块图是空的') plt.show() |
3.绘制子图subplots
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import matplotlib.pyplot as plt # fig, (ax0, ax1, ax2) = plt.subplots(nrows=3) # 分为3行 # 一次性创建窗口和多个子图2行2列共4个 fig, axss = plt.subplots(2, 2, facecolor='yellow') # fig-新窗口,axss-子块图列表 # 设置子块图-1 ax1 = axss[0][1] # 通过子图数组获取一个子图 ax1.set_title('这是块图1') # 设置标题 ax1.axis([0, 6, 0, 10]) # 刻度范围 x1 = [1, 2, 3, 4, 5] y1 = [2, 3, 5, 7, 6] ax1.plot(x1, y1, marker='D', color='g', label='Line-1') # 线条1。增加线的图例。点图:marker图标 ax1.legend(loc='lower right') # 显示图例 # 设置子块图-2 ax3 = axss[1][0] ax3.set_title('这是块图2') x2 = [0, 1, 3, 3, 5] y2 = [1, 2, 5, 7, 4] ax3.plot(x2, y2) plt.show() |
4.绘制不等分子图subplot
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 |
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 6] # 划分为2行1列。即2块空间 plt.subplot(2, 1, 2) # 在第2块-底部的空间,绘制一个图形 plt.plot(x, y, 'olivedrab') plt.title('跨两列') # 重新划分为2行2列。即4块空间 plt.subplot(2, 2, 1) # 在第1块-上半部分的,左边的空间,绘制图形 plt.plot(x, y, 'silver') plt.title('left') plt.subplot(2, 2, 2) # 在第2块-上半部分的,右边的空间,绘制图形 plt.plot(x, y, 'orangered') plt.title('right') # 【 图1 】 # 划分为2行1列。即2块空间 plt.subplot(2, 1, 1) # 在第1块-上部的空间,绘制一个图形 plt.plot(x, y, 'olivedrab') plt.title('跨两列') # 重新划分为2行2列。即4块空间 plt.subplot(2, 2, 3) # 在第3块-底部的,左边的空间,绘制一个图形。1、2块都在第一行,即顶部 plt.plot(x, y, 'silver') plt.title('left') plt.subplot(2, 2, 4) # 在第4块-底部的,右边的空间,绘制一个图形。 plt.plot(x, y, 'orangered') plt.title('right') # 【 图2 】 plt.show() |
5.绘制不等分子图subplot2grid
1 2 3 4 5 6 7 8 9 |
import matplotlib.pyplot as plt ax1 = plt.subplot2grid((3,3), (0, 0), colspan=3)# 3行3列,取左上角0行,0列格子,跨3列 ax2 = plt.subplot2grid((3,3), (1, 0), colspan=2)# 3x3网格,取1,0,跨2列 ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)# 3x3网格,取1,2,跨2行 ax4 = plt.subplot2grid((3,3), (2, 0))# 3x3网格,,取2,0 ax5 = plt.subplot2grid((3,3), (2, 1))# 3x3网格,,取2,1 plt.show() |
6.绘制不等分子图GridSpec
1 2 3 4 5 6 7 8 9 10 11 |
import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec gs = GridSpec(3, 3) ax1 = plt.subplot(gs[0, 0:3]) # 在0行,连续取0,1,2列 ax2 = plt.subplot(gs[1, 0:2]) # 在1行,连续取0,1列 ax3 = plt.subplot(gs[1:3, 2:3]) # 连续在1,2行,取2列 ax4 = plt.subplot(gs[2, 0:1]) # 在2行,取0列 ax5 = plt.subplot(gs[-1, -2]) # 在2行,取1列 plt.show() |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/2956.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设