简体   繁体   English

如何在彼此之上创建 2 个图形,它们都具有相同的 x 轴和不同的 y 轴?

[英]How can would I create 2 graphs on top of each other with both having the same x-axis and different y-axis?

So say I have the data in the right format for a line graph on top of a colour map.所以说我有正确格式的数据,用于彩色地图顶部的折线图。 How would I go about stacking them like seen below in Python with them both having the same x-axis but different y-axis?我将如何像下面在 Python 中看到的那样堆叠它们,它们都具有相同的 x 轴但不同的 y 轴?

Thanks谢谢

在此处输入图片说明

You can setup your axes like that using plt.subplots and the appropriate arguments (paying special attention to the gridspec_kw argument).您可以使用plt.subplots和适当的参数(特别注意gridspec_kw参数)来设置您的轴。

You want something like你想要类似的东西

gridspec_kw = dict(
    # Defines the heights of the two plots 
    # (bottom plot twice the size of the top plot)
    height_ratios=(1, 2),  
    # Zero space between axes
    hspace=0,
)

# Setup the figure with 2 rows, sharing the x-axis and with 
# the gridspec_kw arguments defined above
fig, axes = plt.subplots(
    nrows=2, ncols=1, sharex=True, 
    gridspec_kw=gridspec_kw,
)

Full example:完整示例:

import numpy as np 
import matplotlib.pyplot as plt 

x = np.random.normal(size=10_000)
y = np.random.uniform(size=10_000)


gridspec_kw = dict(
    height_ratios=(1, 2),
    hspace=0,
)

fig, axes = plt.subplots(
    nrows=2, ncols=1, sharex=True, gridspec_kw=gridspec_kw,
)

axes[0].hist(x)
axes[1].hist2d(x, y)

plt.show()

will give you会给你

在此处输入图片说明

暂无
暂无

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

相关问题 Pyqtgraph:在同一图形窗口中实时绘制两个相同X轴但两个Y轴相反方向的图形 - Pyqtgraph: plot real time two graphs same X-axis but two different Y-axis in opposite direction in a same graph window 如何用pyplot在相同的x轴(日期时间)但不同的y轴上绘制线形图和条形图? - How to plot line and bar-chart on the same x-axis (datetime) but different y-axis with pyplot? 在python中具有不同x轴和y轴刻度的两个(或更多)图 - two (or more) graphs in one plot with different x-axis AND y-axis scales in python 如何使用python在x轴或y轴标签之间添加更多刻度? - How can I add more ticks between x-axis or y-axis labels using python? 如何在 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? 如何使plot的x轴原点和y轴原点在matplotlib中重叠? - How can I make the origin of the x-axis and the origin of the y-axis of a plot overlap in matplotlib? Altair - 将 y 轴与不同图表的 x 轴链接 - Altair - link y-axis with x-axis of a different chart Plot 具有不同的 x 轴和 y 轴使用 matplotlib - Plot with different x-axis and y-axis using matplotlib Plotly 如何将x轴移动到顶部和y轴向右移动? - Plotly How to move x-axis to top and y-axis to the right? 如何更改matlibplot中x轴和y轴的范围? - How to change the range of the x-axis and y-axis in matlibplot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM