Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
共享X轴Python子地块_Python_Matplotlib_Sharing - Fatal编程技术网

共享X轴Python子地块

共享X轴Python子地块,python,matplotlib,sharing,Python,Matplotlib,Sharing,经过大量的斗争和观看youtube教程,我已经能够实现我的目标,创建一个图形窗口,具有唯一的名称和4个子窗口 我使用的方法是add_subplot,因为我更好地理解代码,而不是解包元组方法,即图 我尝试做的最后一点是共享我的x轴,在做了大量的尝试后,即尝试插入代码sharex=Cols或sharex=True,我没有成功 我的完整代码如下:- import pandas import numpy from matplotlib import pyplot x = numpy.linspace(

经过大量的斗争和观看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, 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代码没有任何影响,但每个人都包括它?

使用matplotlib有两种主要方法。第一个是pyplot有状态API,您可以在其中调用pyplot.legend,另一个是面向对象的API,您可以在其中调用'SideTank.get_handles_标签。两者都可以,但混合太多有时会导致混乱。这就是你的问题所在。比如说,

SideTank.get_legend_handles_labels()
pyplot.legend()
第一个调用是ooapi的一部分,第二个调用是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))
如果您想使用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显示的是数字而不是子图。因此,在设置完整个图形后,您可能只想调用它一次。

使用matplotlib有两种主要方法。第一个是pyplot有状态API,您可以在其中调用pyplot.legend,另一个是面向对象的API,您可以在其中调用'SideTank.get_handles_标签。两者都可以,但混合太多有时会导致混乱。这就是你的问题所在。比如说,

SideTank.get_legend_handles_labels()
pyplot.legend()
第一个调用是ooapi的一部分,第二个调用是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))
如果您想使用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显示的是数字而不是子图。因此,在设置完整个图形后,您可能只想调用它一次。

作为旁注,SideTank.get_legend_handles_labels本身不做任何事情:它会重新调用可以传递给另一个对legend的调用的对象。通常,只需调用axes.legend就足够了,我认为它可以调用get_legend_handles_labels:您只需要在代码末尾调用pyplot.show一次。是一个工作示例。作为侧注,SideTank.get_legend_handles_标签本身不起任何作用:它会重新返回可以传递给另一个legend调用的对象。通常,只需调用axes.legend就足够了,我认为它可以调用get_legend_handles_labels:您只需要在代码末尾调用pyplot.show一次。这是一个工作样本。