Python图像绘制基础——子图绘制

import matplotlib.pyplot as plt

my_dpi = 96
fig, axs = plt.subplots(2, 2, figsize=(480 / my_dpi, 480 / my_dpi), dpi=my_dpi,
                        sharex=False,  # x轴刻度值共享开启
                        sharey=False,  # y轴刻度值共享关闭

                        )
# fig为matplotlib.figure.Figure对象
# axs为matplotlib.axes.Axes,把fig分成2x2的子图
axs[0][0].plot([1, 2, 3])
axs[0][1].bar([1, 2, 3], [4, 5, 6])
axs[0][1].set(title='title')  # 设置axes及子图标题
axs[0][1].set_xlabel('set_xlabel', fontsize=15, color='g')  # 设置x轴刻度标签
axs[0][1].set_ylabel('set_ylabel', fontsize=15, color='g')  # 设置y轴刻度标签
axs[0][1].set_xlim(0, 8)  # 设置x轴刻度范围
axs[0][1].set_xticks(range(0, 10, 2))  # 设置x轴刻度间距
axs[0][1].tick_params(axis='x',  # 可选'y','both'
                      labelsize=20, rotation=45)  # x轴标签旋转、字号等

axs[1][0].plot([1, 2, 3])
axs[1][1].bar([1, 2, 3], [4, 5, 6])

fig.suptitle('matplotlib object-oriented', color='r')  # 设置fig即整整张图的标题

# 修改子图在整个figure中的位置(上下左右)
plt.subplots_adjust(left=0.125,
                    bottom=-0.61,
                    right=1.3,  # 防止右边子图y轴标题与左边子图重叠
                    top=0.88,
                    wspace=0.2,
                    hspace=0.2
                    )

# 参数介绍
'''
## The figure subplot parameters.  All dimensions are a fraction of the figure width and height.
#figure.subplot.left:   0.125  # the left side of the subplots of the figure
#figure.subplot.right:  0.9    # the right side of the subplots of the figure
#figure.subplot.bottom: 0.11   # the bottom of the subplots of the figure
#figure.subplot.top:    0.88   # the top of the subplots of the figure
#figure.subplot.wspace: 0.2    # the amount of width reserved for space between subplots,
                               # expressed as a fraction of the average axis width
#figure.subplot.hspace: 0.2    # the amount of height reserved for space between subplots,
                               # expressed as a fraction of the average axis height
'''

plt.show()

具体代码和注释已给出,下图是运行效果:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YinHeCS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值