python - 多个 Y 轴比例不同的堆叠水平图
问题描述
嗨我正在尝试创建:
- 水平堆叠的地块
- 在两个图上都有辅助轴
- 轴上有不同的比例 - 不幸的是,我的 Y 轴目前每个子图都有相同的比例...... :(
当前代码:
# Create axes
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle("XYZ")
fig.set_figheight(5)
fig.set_figwidth(15)
# First graph
ax1.scatter(
df_PTA_clip_pstar["start_time"],
df_PTA_clip_pstar["pstar"],
s=5,
c="black",
label="P*",
)
plt.ylabel("P*")
ax1.scatter(df_PTA_clipkh["start_time"], df_PTA_clipkh["kh"], s=2, c="cyan", label="Kh")
ax1.secondary_yaxis("right")
plt.ylabel("Kh")
# Second graph - will add the correct data to this once first graph fixed
ax2.scatter(x, y, s=5, c="Red", label="P*")
ax2.scatter(x, z, s=5, c="Green", label="Kh")
ax2.secondary_yaxis("right")
plt.tight_layout()
plt.legend()
plt.show()
目前的进展:
解决方案
您可以.twinx()
在每个 ax 对象上使用方法,这样您就可以在同一个 ax 对象上拥有两个共享 x 轴的图:
import matplotlib.pyplot as plt
import numpy as np
# Create axes
fig, (ax1, ax2) = plt.subplots(1, 2)
## First subplot
x = np.random.random_sample(100)
y = np.random.random_sample(100)
ax1.set_xlim(0, 2)
ax1.scatter(x, y,
s=5,
c="black")
ax11 = ax1.twinx()
x = 1 + x
y = 1 + np.random.random_sample(100)
ax11.scatter(x, y,
s=5,
c="red")
## Second subplot
x = 2 * np.random.random_sample(100) - 1
y = np.random.random_sample(100)
ax2.set_xlim(-1, 2)
ax2.scatter(x, y,
s=5,
c="blue")
ax21 = ax2.twinx()
x = 1 + x
y = 10 + np.random.random_sample(100)
ax21.scatter(x, y,
s=5,
c="orange")
plt.show()
推荐阅读
- javascript - 有没有办法在 ASP.NET Core 的控制器中读取由 JavaScript 代码创建的 cookie?
- python - Visual Studio Code:如何在新选项卡而不是新窗口中打开新的 Python 文件
- java - 如何使用调试外壳创建新变量
- r - 从 R 中下载和打开 xlsx 文件的问题
- python - 如何在子类中的方法之间引用
- angular - 离子限制 API 调用
- c# - 为什么 xamarin 表单列表视图中的组标题在发布模式下不可见?
- python-3.x - 在终端中安装 pytables 失败
- python - Python - while 循环 - 初学者
- php - 包装 darryldecode/cart laravel Cart::add() 功能不起作用