【问题标题】:Arranging figures horizonatlly水平排列图形
【发布时间】:2021-01-18 23:28:33
【问题描述】:

我想知道除了使用子图之外,是否有一种简单的方法可以在 jupyter-lab 中水平排列多个 matplotlib.pyplot 图形。这些数字是由一个模块使用类似于

的语法一一生成的
import matplotlib.pyplot as plt

for i in range(N):
    plt.figure(i)
    plt.plot(...)

我不能轻易更改模块的代码,但 jupyter-lab 从上到下排列数字有点烦人。我能以某种方式改变这种行为吗?

【问题讨论】:

  • 我应该再次澄清:我对如何创建人物本身没有影响。我只是想知道是否有一个巧妙的解决方案,使用 jupyter-lab 来并排显示数字而不是垂直排列

标签: python matplotlib jupyter-lab


【解决方案1】:

关注https://matplotlib.org/3.1.0/gallery/subplots_axes_and_figures/subplots_demo.html的信息

# sphinx_gallery_thumbnail_number = 11

import matplotlib.pyplot as plt
import numpy as np

# Some example data to display
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

并排排列两个地块

fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Horizontally stacked subplots')
ax1.plot(x, y)
ax2.plot(x, -y)

或者在两个方向上堆叠它们

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0,0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0,1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1,0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1,1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()

在你的例子中

import matplotlib.pyplot as plt

fig, axs = plt.subplots(1, N)
for i in range(N):
    axs[0, i].set_title('plot '+str(i))
    axs.plot(...)

【讨论】:

  • 我非常清楚这个解决方案,不幸的是它不适用于我的情况。正如我试图传达的那样,我无法直接访问绘图本身。图形的创建和绘图调用都在一个模块内处理。我想做的(即使我意识到这可能很有挑战性)就是并排显示各个图形,而不是垂直排列......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
  • 2014-03-12
  • 2015-10-06
  • 2021-12-04
  • 2023-03-29
相关资源
最近更新 更多