共享X轴Python子图

问题描述

经过 很多 的战斗和观看youtube教程,我已经实现了创建具有唯一名称和4个子图的图形窗口的目标。

我使用的方法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,2)
Sidewalk.plot(x,label='Line 3')
Sidewalk.plot(x,label='Line 4')
pyplot.ylabel("Ylabel")
Sidewalk.get_legend_handles_labels()
pyplot.legend()
pyplot.show()

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

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

有人能为我指出所有这些子图的X轴共享位置/方向的正确方向吗?

顺便说一句,我已经意识到代码pyplot.show()并没有任何影响,但是每个人都包括了吗?

解决方法

使用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),1))
axes[2].plot((0,0.5),1))
axes[3].plot((1,1.5),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,2,sharex = ax1)
ax3 = fig.add_subplot(4,3,sharex = ax1)
ax4 = fig.add_subplot(4,4,sharex = ax1)

ax1.plot((0,1))
ax2.plot((0,1))
ax3.plot((0,1))
ax4.plot((1,1))

如果要使用pyplot API,它将看起来像这样。

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

# Plot something in the current subplot
pyplot.plot((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,sharex = share_handle)
pyplot.plot((0,1))

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

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

The results of the above code in Jupyter. Four plots,each with a line covering various segments of a shared X axis.

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