『Python』matplotlib共享绘图区域坐标轴

Posted 年少不轻狂

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了『Python』matplotlib共享绘图区域坐标轴相关的知识,希望对你有一定的参考价值。

1. 共享单一绘图区域的坐标轴

有时候,我们想将多张图形放在同一个绘图区域,不想在每个绘图区域只绘制一幅图形。这时候,就可以借助共享坐标轴的方法实现在一个绘图区域绘制多幅图形的目的。

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

mpl.use(\'Qt5Agg\')
mpl.rcParams[\'font.sans-serif\'] = [\'SimHei\']
mpl.rcParams[\'font.serif\'] = [\'SimHei\']
mpl.rcParams[\'axes.unicode_minus\'] = False  # 解决保存图像是负号\'-\'显示为方块的问题,或者转换负号为字符串

fig, ax1 = plt.subplots()

t = np.arange(0.05, 10., 0.01)
s1 = np.exp(t)
ax1.plot(t, s1, c="b", ls="-")

ax1.set_xlabel("x坐标轴")
ax1.set_ylabel("以e为底的指数", color="b")
ax1.tick_params("y", colors="b")

ax2 = ax1.twinx()
s2 = np.cos(t ** 2)
ax2.plot(t, s2, c="r", ls=":")

ax2.set_ylabel("余弦函数", color="r")
ax2.tick_params("y", colors="r")

plt.show()

同样可以用twiny()方法共享y轴

2. 共享不同子区绘图区域的坐标轴

subplots()函数有两个命名关键字参数sharexsharey,有四种取值

  • \'row\'
  • \'col\'
  • \'all\',等同于True
  • \'none\',等同于False
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.use(\'Qt5Agg\')
mpl.rcParams[\'font.sans-serif\'] = [\'SimHei\']
mpl.rcParams[\'font.serif\'] = [\'SimHei\']
mpl.rcParams[\'axes.unicode_minus\'] = False  # 解决保存图像是负号\'-\'显示为方块的问题,或者转换负号为字符串

x1 = np.linspace(0, 2 * np.pi, 400)
y1 = np.cos(x1 ** 2)

x2 = np.linspace(0.01, 10, 100)
y2 = np.sin(x2)

x3 = np.random.rand(100)
y3 = np.linspace(0, 3, 100)

x4 = np.arange(0, 6, 0.5)
y4 = np.power(x4, 3)

fig, ax = plt.subplots(2, 2)

ax1 = ax[0, 0]
ax1.plot(x1, y1)

ax2 = ax[0, 1]
ax2.plot(x2, y2)

ax3 = ax[1, 0]
ax3.scatter(x3, y3)

ax4 = ax[1, 1]
ax4.scatter(x4, y4)

plt.show()

基本图形如图所示:

  • sharex=\'all\'
  • sharex=\'none\'
  • sharex=\'row\'
  • sharex=\'col\'

sharey类似

将共享坐标轴的子区之间的空隙去掉

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

mpl.use(\'Qt5Agg\')
mpl.rcParams[\'font.sans-serif\'] = [\'SimHei\']
mpl.rcParams[\'font.serif\'] = [\'SimHei\']
mpl.rcParams[\'axes.unicode_minus\'] = False  # 解决保存图像是负号\'-\'显示为方块的问题,或者转换负号为字符串

x1 = np.linspace(0, 2 * np.pi, 400)
y1 = np.cos(x1 ** 2)

x2 = np.linspace(0.01, 10, 100)
y2 = np.sin(x2)

x3 = np.random.rand(100)
y3 = np.linspace(0, 3, 100)

x4 = np.arange(0, 6, 0.5)
y4 = np.power(x4, 3)

fig, ax = plt.subplots(2, 2, sharex=\'all\', sharey=\'all\')
fig.subplots_adjust(hspace=0, wspace=0)

ax1 = ax[0, 0]
ax1.plot(x1, y1)

ax2 = ax[0, 1]
ax2.plot(x2, y2)

ax3 = ax[1, 0]
ax3.scatter(x3, y3)

ax4 = ax[1, 1]
ax4.scatter(x4, y4)

plt.show()

3. 共享个别子区绘图区域的坐标轴

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

mpl.use(\'Qt5Agg\')
mpl.rcParams[\'font.sans-serif\'] = [\'SimHei\']
mpl.rcParams[\'font.serif\'] = [\'SimHei\']
mpl.rcParams[\'axes.unicode_minus\'] = False  # 解决保存图像是负号\'-\'显示为方块的问题,或者转换负号为字符串

x1 = np.linspace(0, 2 * np.pi, 400)
y1 = np.cos(x1 ** 2)

x2 = np.linspace(0.01, 10, 100)
y2 = np.sin(x2)

x3 = np.random.rand(100)
y3 = np.linspace(0, 3, 100)

x4 = np.arange(0, 6, 0.5)
y4 = np.power(x4, 3)

fig, ax = plt.subplots(2, 2)

ax1 = plt.subplot(221)
ax1.plot(x1, y1)

ax2 = plt.subplot(222)
ax2.plot(x2, y2)

ax3 = plt.subplot(223)
ax3.scatter(x3, y3)

ax4 = plt.subplot(224, sharex=ax1)
ax4.scatter(x4, y4)

plt.show()

4. 优化坐标轴范围显示

plt.autoscale(enable=True, axis="both", tight=True)

以上是关于『Python』matplotlib共享绘图区域坐标轴的主要内容,如果未能解决你的问题,请参考以下文章

matplotlib学习日记-共享绘图区域的坐标轴

python matplotlib绘图大全(散点图柱状图饼图极坐标图热量图三维图以及热图)

Python 绘图包 Matplotlib Pyplot 教程

Matplotlib库(简介)

python matplotlib subplot 上面面积大下面小怎么办

python3绘图示例6-2(基于matplotlib,绘图流程介绍及设置等)