【问题标题】:Vertical arrangement in matplotlib subplotsmatplotlib 子图中的垂直排列
【发布时间】:2020-10-07 19:30:49
【问题描述】:

我正在尝试用 4 个子图绘制一个图。下面的代码和我写的差不多。

import numpy as np
import matplotlib.pyplot as plt
def trial(x, r, ax=None):
    y = r*np.sin(x)
    ax[0].plot(x, r*x)
    ax[0].set_title('x')
    ax[1].plot(x, y)
    ax[1].set_title('sinx')

x = np.linspace(0, 4*np.pi)    
fig, (ax1, ax2) = plt.subplots(2,2)
trial(x, 1, ax1)
trial(x, 2, ax2)

这是我得到的输出。

如果我只绘制 ax1(从子图中删除最后一个索引之后),x 和 sin(x) 是垂直排列的。但是一旦我添加 ax2,它们就会变成水平排列的。我希望 x 和 sin(x) 与 ax1 和 ax2 垂直排列。我应该做哪些更正?

【问题讨论】:

  • @QuangHoang 感谢您在不点击 imgur 链接的情况下使这些数字可见。

标签: python-3.x matplotlib


【解决方案1】:

我们试试吧:

fig, axes = plt.subplots(2,2)
trial(x, 1, axes[:,0])
trial(x, 2, axes[:,1])

输出:

【讨论】:

  • 不,我希望它看起来像这样,第 1 行中的 x 和第 2 行中的 sinx
  • 谢谢。我是 matplotlib 新手。
猜你喜欢
  • 1970-01-01
  • 2014-04-15
  • 2020-08-11
  • 2016-12-29
  • 2018-01-07
  • 1970-01-01
  • 2022-12-06
  • 2021-10-11
  • 1970-01-01
相关资源
最近更新 更多