【问题标题】:Sharing X axis Python Subplots共享 X 轴 Python 子图
【发布时间】:2020-08-21 23:25:59
【问题描述】:

大量打斗和观看 youtube 教程之后,我已经能够实现创建一个具有唯一名称和 4 个子图的图形窗口的目标。 p>

我使用的方法是add_subplot,因为我比解包元组方法更了解代码,即图,...

我要做的最后一点是分享我的 x 轴,经过大量尝试,即尝试插入代码 sharex="Cols"sharex = True 我没有成功。

我的完整代码如下:-

import pandas
import numpy
from matplotlib import pyplot

x = numpy.linspace(0, 2*numpy.pi, 400)
y = numpy.sin(x**2)
yy = numpy.sin(x**2+3)

My_Figure_Window = pyplot.figure("Title Of Figure Window")
pyplot.style.use("ggplot")
My_Figure_Window.suptitle("Main Title")

Sub_Plot_Rows = 4
Sub_Plot_Cols = 1

Wall = My_Figure_Window.add_subplot(Sub_Plot_Rows, Sub_Plot_Cols, 1)
Wall.plot(x, y, color="r", label='Line 1')
Wall.plot(x, yy, color="b", label='Line 2')
pyplot.ylabel("Ylabel")
Wall.get_legend_handles_labels()
pyplot.legend()
pyplot.show()

Sidewalk = My_Figure_Window.add_subplot(Sub_Plot_Rows, Sub_Plot_Cols, 2)
Sidewalk.plot(x, y, label='Line 3')
Sidewalk.plot(x, yy, label='Line 4')
pyplot.ylabel("Ylabel")
Sidewalk.get_legend_handles_labels()
pyplot.legend()
pyplot.show()

HeadWall = My_Figure_Window.add_subplot(Sub_Plot_Rows, Sub_Plot_Cols, 3)
HeadWall.plot(x, y, label='Line 5')
HeadWall.plot(x, yy, label='Line 6')
pyplot.ylabel("Ylabel")
HeadWall.get_legend_handles_labels()
pyplot.legend()
pyplot.show()

SideTank = My_Figure_Window.add_subplot(Sub_Plot_Rows, Sub_Plot_Cols, 4)
SideTank.plot(x, y, label='Line 7')
SideTank.plot(x, yy, label='Line 8')
pyplot.ylabel("Ylabel")
SideTank.get_legend_handles_labels()
pyplot.legend()
pyplot.show()

谁能指出我在哪里/如何为所有这些子图共享我的 X 轴的正确方向?

顺便说一句,我意识到代码pyplot.show() 没有任何影响,但每个人都包含它吗?

【问题讨论】:

  • 作为旁注,SideTank.get_legend_handles_labels() 自己什么都不做:它返回可以传递给另一个对 legend 的调用的对象。通常,只需调用 axes.legend() 就足够了,我相信它会调用 get_legend_handles_labels():matplotlib.org/3.1.1/api/_as_gen/…
  • 您只需要在代码末尾调用一次pyplot.show()Here 是一个工作示例。

标签: python matplotlib sharing


【解决方案1】:

使用 matplotlib 有两种主要方法。第一个是 pyplot “有状态” API,您可以在其中进行 pyplot.legend() 之类的调用,另一个是面向对象的 API,您可以在其中进行诸如 `SideTank.get_handles_labels() 之类的调用。两者都很好,但是将它们混合太多有时会导致混乱。这是你的问题。例如,

SideTank.get_legend_handles_labels()
pyplot.legend()

第一个调用是 OO API 的一部分,第二个调用是 pyplot 的。如果您正在使用 OO API,您可能想要使用 SideTank.legend(),而仅当您使用 Pyplot API 时才使用 pyplot.legend()

如果你想使用 pyplot API:

如果您想使用 OO API:

以下是实现双轴的几种方法:

# This will create a Figure and a list of 4 axes objects. All of those axes objects will have sharex X axes
figure, axes = pyplot.subplots(nrows=4, sharex=True)

axes[0].plot((0, 1), (0, 1))
axes[1].plot((0, 2), (0, 1))
axes[2].plot((0, 0.5), (0, 1))
axes[3].plot((1, 1.5), (0, 1))
# Create a figure
fig = pyplot.figure()

# Use that figure to create a first subplot
ax1 = fig.add_subplot(4, 1, 1)
# Make the following subplots twin that subplot's x axis.
ax2 = fig.add_subplot(4, 1, 2, sharex = ax1)
ax3 = fig.add_subplot(4, 1, 3, sharex = ax1)
ax4 = fig.add_subplot(4, 1, 4, sharex = ax1)

ax1.plot((0, 1), (0, 1))
ax2.plot((0, 2), (0, 1))
ax3.plot((0, 0.5), (0, 1))
ax4.plot((1, 1.5), (0, 1))

如果你想使用 pyplot API,它看起来像这样。

# Create a current figure and subplot
pyplot.figure()
pyplot.subplot(4, 1, 1)

# Plot something in the current subplot
pyplot.plot((0, 1), (0, 1))

# short for "get current axes"
# This returns an Axis, so we're dipping into the OO API; it's not uncommon to need to
# do that to do more advanced things, but it's a signal to be careful!
share_handle = pyplot.gca() 

# Select a new subplot, and tell it to share its x axis with the first subplot.
pyplot.subplot(4, 1, 2, sharex = share_handle)
pyplot.plot((0, 2), (0, 1))

pyplot.subplot(4, 1, 3, sharex = share_handle)
pyplot.plot((0, 0.5), (0, 1))

pyplot.subplot(4, 1, 4, sharex = share_handle)
pyplot.plot((1, 1.5), (0, 1))

所有这些都将创建如下内容:

关于您的第二个问题,pyplot.show() 显示 数字 而不是子图。因此,您可能只想在完成整个图形的设置后调用它一次。

【讨论】:

    猜你喜欢
    • 2018-01-04
    • 2016-10-10
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    相关资源
    最近更新 更多