【问题标题】:Creating 10 different figures in Matplotlib lib without creating them individually在 Matplotlib lib 中创建 10 个不同的图形而不单独创建它们
【发布时间】:2020-09-25 16:11:46
【问题描述】:

我正在尝试使用 Matplotlib 创建一个绘图,该绘图具有两个 x 轴、顶部和底部以及一个 y 轴。然后我想把这个情节分成10个不同的数字。我试图弄清楚如何在不单独创建每个图形的情况下做到这一点。

例如:x 轴为 30 - 60 英里,次要 x 与主轴相同,但单位为英尺。我不想在一张图表上显示 30 英里。我希望每个图表只有 3 英里。

有人对此有什么建议吗?

【问题讨论】:

  • 请提供您正在使用的数据!
  • 欢迎来到 SO。请阅读stackoverflow.com/help/mcve 并编辑您的帖子。
  • Matplotlib 中的每个图形或子图都是单独的绘图对象。您必须创建每个。为什么不想单独创建它们?发布一个小的工作示例,也许我们可以帮助您简化操作。

标签: python python-3.x numpy matplotlib


【解决方案1】:

这个怎么样?当然,您必须弄清楚格式。

import numpy as np
import matplotlib.pyplot as plt

X = np.arange(30, 61)
Y = np.cos(X*0.5)

n_plots = np.ptp(X) // 3  # 3 miles per subplot
XY_chunks = list(zip(np.array_split(X, n_plots), 
                     np.array_split(Y, n_plots)))

fig, axs = plt.subplots(nrows=1, ncols=n_plots, figsize=(15, 2), sharey='row')

for i in range(n_plots):
    XX, YY = XY_chunks[i]  # Unpack X and Y for this plot
    axs[i].plot(XX,YY)  # Plot miles
    ax2 = axs[i].twiny()  # This will be your feet axis
    ax2.set_xticklabels((XX*5280).round(), rotation=90)  # Convert to feet
    axs[i].set_xlabel('Miles')
    ax2.set_xlabel('Feet')

plt.show()

【讨论】:

    猜你喜欢
    • 2021-06-11
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 2013-07-08
    • 2014-10-15
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多