gpt4 book ai didi

python - 使用 twinx 时控制跟踪器

转载 作者:太空狗 更新时间:2023-10-29 21:47:37 24 4
gpt4 key购买 nike

enter image description here

右下角的跟踪器(以红色突出显示)报告相对于右侧 y 轴的 y 值。

如何让跟踪器报告相对于左侧 y 轴的 y 值?

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(6)
numdata = 100
t = np.linspace(0.05, 0.11, numdata)
y1 = np.cumsum(np.random.random(numdata) - 0.5) * 40000
y2 = np.cumsum(np.random.random(numdata) - 0.5) * 0.002

fig = plt.figure()

ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()

ax1.plot(t, y1, 'r-', label='y1')
ax2.plot(t, y2, 'g-', label='y2')

ax1.legend()
plt.show()

我知道将 y1y2 交换将使跟踪器报告 y1 值,但这也将 y1 标记放在右侧,这不是我想要的。

ax1.plot(t, y2, 'g-', label='y2')
ax2.plot(t, y1, 'r-', label='y1')

最佳答案

啊,找到了:ax.yaxis.set_ticks_position("right")。您可以交换 y 轴的位置,而不是试图“控制跟踪器”。

ax1.yaxis.set_ticks_position("right")
ax2.yaxis.set_ticks_position("left")

ax1.plot(t, y2, 'g-', label='y1')
ax2.plot(t, y1, 'r-', label='y2')

据我所知,使用 twinx 时,跟踪器始终遵循 ax2

enter image description here

关于python - 使用 twinx 时控制跟踪器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7660951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com