python - 如何在彼此之上创建 2 个具有相同 x 轴和不同 y 轴的图形?
解决方案
您可以使用plt.subplots
和适当的参数设置您的轴(特别注意gridspec_kw
参数)。
你想要类似的东西
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,
)
完整示例:
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()
会给你
推荐阅读
- flutter - 带有透明图标的剪辑容器
- c++ - 分配和构造对象
- javascript - 将对象属性键与数组中的对象属性值进行比较,返回“总点数”
- javascript - 根据对象的属性将对象数组转换为哈希
- c# - .appxbundle 无法修改注册表
- java - 如何同时添加响应ID和加入对象
- angular - 为什么我不能像这样禁用 mat-stroke-button?
- python - 我想将两个 id 从 .html 发布到视图
- javascript - Webpack Split Chunks Plugin 覆盖 cacheGroups,正则表达式匹配错误?
- reactjs - 我们可以在对象属性中使用变量吗?