使用 twinx 时控制跟踪器 [英] Controlling the tracker when using twinx

查看:59
本文介绍了使用 twinx 时控制跟踪器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The tracker in the lower-right corner (highlighted in red) reports y-values relative to the y-axis on the right.

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

How can I get the tracker to report y-values relative to the y-axis on the left instead?

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 勾号放在右侧,这不是我想要的.

I know swapping y1 with y2 will make the tracker report y1-values, but this also places the y1 tickmarks on the right-hand side, which is not what I want to happen.

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

推荐答案

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

Ah, found it: ax.yaxis.set_ticks_position("right"). Instead of trying to "control the tracker", you can swap the location of the y-axes.

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')

AFAIK,使用 twinx 时,跟踪器始终遵循 ax2 .

AFAIK, the tracker always follows ax2 when using twinx.

这篇关于使用 twinx 时控制跟踪器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆