2011-10-05 41 views
7

enter image description here在右下角(以红色突出显示)使用twinx

跟踪器控制时跟踪器报告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') 

回答

3

啊,发现它: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

3

请注意,如果您创建AX1和AX2后ax3= ax1.twiny()轴,跟踪器去AX3和您再次将其报告Y1值。

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() 
ax3 = ax1.twiny() 
ax3.set_xticks([]) 
plt.show() 
相关问题