学无先后,达者为师

网站首页 编程语言 正文

subplots_adjust()函数--matplotlib

作者:牵牛花主人 更新时间: 2022-10-29 编程语言

1. 函数功能

调整子区的展现效果

2. 函数语法

 subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

3. 函数参数与示例

参数 含义
left 可选参数,浮点数;子区左边的位置,默认为 0.125,以画布figure为参考系
right 可选参数,浮点数;子区右边的位置 ,默认为 0.9,以画布figure为参考系
bottom 可选参数,浮点数;子区底边的位置,默认为 0.11,以画布figure为参考系
top 可选参数,浮点数;子区顶边的位置,默认为 0.88,以画布figure为参考系
wspace 可选参数,浮点数;子区之间的空白宽度,默认为 0.2,以绘图区的平均宽度为参考
hspace 可选参数,浮点数;子区之间的空白高度,默认为 0.2,以绘图区的平均宽度为参考

3.1 hsapce

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 2 * np.pi, 500)
y1 = np.sin(x) * np.cos(x)
y2 = np.exp(-x)
y3 = np.sqrt(x)
y4 = x / 4

fig, ax = plt.subplots(4, 1, facecolor='beige', sharex=True,
                       subplot_kw=dict(facecolor='seashell'))
ax[0].plot(x, y1, c='r', lw=2)
ax[1].plot(x, y2, c='y', ls="--")
ax[2].plot(x, y3, c='g', ls=":")
ax[3].plot(x, y4, c='m', ls='-.', lw=2)

plt.show()

在这里插入图片描述
对于本例中的图形,所有图形共享x轴,则图形与图形之间不需要空隙,垂直方向的空隙可以通过hspace=0,实现消除

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 2 * np.pi, 500)
y1 = np.sin(x) * np.cos(x)
y2 = np.exp(-x)
y3 = np.sqrt(x)
y4 = x / 4

fig, ax = plt.subplots(4, 1, facecolor='beige', sharex=True,
                       subplot_kw=dict(facecolor='seashell'))

fig.subplots_adjust(hspace=0)

ax[0].plot(x, y1, c='r', lw=2)
ax[1].plot(x, y2, c='y', ls="--")
ax[2].plot(x, y3, c='g', ls=":")
ax[3].plot(x, y4, c='m', ls='-.', lw=2)

plt.show()

在这里插入图片描述

3.2 left right bottom top

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 2 * np.pi, 500)
y1 = np.sin(x) * np.cos(x)
y2 = np.exp(-x)
y3 = np.sqrt(x)
y4 = x / 4

fig, ax = plt.subplots(4, 1, facecolor='beige', sharex=True,
                       subplot_kw=dict(facecolor='seashell'))

fig.subplots_adjust(left=0.05, right=0.98, bottom=0.05,
                    top=0.95, hspace=0)

ax[0].plot(x, y1, c='r', lw=2)
ax[1].plot(x, y2, c='y', ls="--")
ax[2].plot(x, y3, c='g', ls=":")
ax[3].plot(x, y4, c='m', ls='-.', lw=2)

plt.show()

在这里插入图片描述

原文链接:https://blog.csdn.net/chongbaikaishi/article/details/127578822

栏目分类
最近更新