[英]How can would I create 2 graphs on top of each other with both having the same x-axis and different y-axis?
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.