简体   繁体   English

如何在其中一个图上具有一个共享的X轴和多个Y轴的堆叠图?

[英]How can I have a stacked plot with a shared X axis and multiple Y axis on one of the plots?

I am trying to modify this example so also have a second plot on top sharing the same X-axis. 我正在尝试修改此示例,因此在顶部还具有共享相同X轴的第二个图。 Unfortunately, it is not clear how to add subplots to this structure. 不幸的是,目前尚不清楚如何向该结构添加子图。 I have tried to directly combine a different example, but it just creates two figures. 我试图直接结合一个不同的示例,但它仅创建了两个数字。

How do I put another plot separated from the two-y-axis plot with the same X-axis? 如何放置另一个从X轴相同的X轴上分开的图?

"""
Parasite axis demo

The following code is an example of a parasite axis. It aims to show a user how
to plot multiple different values onto one single plot. Notice how in this
example, par1 and par2 are both calling twinx meaning both are tied directly to
the x-axis. From there, each of those two axis can behave separately from the
each other, meaning they can take on separate values from themselves as well as
the x-axis.
"""
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt

host = host_subplot(111, axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)

par1 = host.twinx()
par2 = host.twinx()

offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right",
                                    axes=par2,
                                    offset=(offset, 0))

par2.axis["right"].toggle(all=True)

host.set_xlim(0, 2)
host.set_ylim(0, 2)

host.set_xlabel("Distance")
host.set_ylabel("Density")
par1.set_ylabel("Temperature")
par2.set_ylabel("Velocity")

p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")

par1.set_ylim(0, 4)
par2.set_ylim(1, 65)

host.legend()

host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
par2.axis["right"].label.set_color(p3.get_color())

## Attempts to add stacked plot on top
f, axarr = plt.subplots(2, sharex=True)
axarr[1].plot([0,1,2], [5,6,7])
axarr[1].set_title('Sharing X axis')

plt.draw()
plt.show()

You can create a new host subplot and share the xaxis. 您可以创建一个新的主机子图并共享xaxis。

replace host = host_subplot(111, axes_class=AA.Axes) with host = host_subplot(211, axes_class=AA.Axes) host = host_subplot(211, axes_class=AA.Axes)替换host = host_subplot(111, axes_class=AA.Axes) host = host_subplot(211, axes_class=AA.Axes)

replace the lines below your line ## Attempts to add stacked plot on top with 替换行## Attempts to add stacked plot on top下方的行, ## Attempts to add stacked plot on top

host2 = host_subplot(212, axes_class=AA.Axes, sharex=host)
host2.plot([0,1,2], [5,6,7])
host2.set_title('Sharing X axis')
plt.tight_layout()
plt.draw()
plt.show()

this results in 这导致

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 plotly 2 堆叠图各种 y 轴类型但共享 x 轴 - plotly 2 stacked plots various y-axis types but shared x-axis python沿y轴的多个堆积图 - python multiple stacked plots along y axis Plotly:如何在悬停三个 y 布局和一个 x 轴共享图形时显示所有堆叠的 y 轴数据值? - Plotly: How to show all the stacked y axis data values while hovering for three y layout and one x axis shared graph? 如何更改我的绘图线条样式并为一个 x 轴设置两个 Y 轴? - How do i change my plot line style and have two Y-axis for one x-axis? 如何在 yellowbrick plot 中标记 X 轴和 Y 轴? 我使用的代码如下所示 - How can I labeled the X axis and Y axis in yellowbrick plot? The code is I used is show below Matplotlib:如何 plot 两个条形图具有相同的 x/y 轴,但一个沿 y 轴从另一个开始 - Matplotlib : How to plot two bar plots with the same x/y axes but one start over the other along y axis 如何在 python 中以像素数为 x 轴,灰度颜色为 y 轴 plot 图形? - How can I plot the figure with the number of pixel as a x-axis and the grayscale color as a y-axis in python? 如何在 y 轴不基于计数的 Python 中创建堆叠条形图 plot - How can I create a Stacked Bar plot in Python where the y axis is NOT based on counts Dash/Plotly:如何使用相同的 x 轴在 y 轴上绘制两列? - Dash/Plotly: How can I plot two columns on the y-axis with the same x axis? 如何使用pyplot旋转图的x轴和y轴 - How can I rotate a plot x axis and y axis using pyplot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM