多个y刻度,但只有一个启用平移和缩放

时间:2013-03-08 21:05:55

标签: python matplotlib plot zoom

考虑以下用于绘制matplotlib图的python代码:

import matplotlib.pylab as pp
import numpy as np

alpha = np.linspace(0, 2 * np.pi, 400)
sig1 = np.sin(alpha)
sig2 = np.sin(2 * alpha) + 2 * (alpha > np.pi)

ax1 = pp.subplot(111)
ax2 = ax1.twinx()

ax1.plot(alpha, sig1, color='b')
ax2.plot(alpha, sig2, color='r')
ax1.set_ylabel('sig1 value', color='b')
ax2.set_ylabel('sig2 value', color='r')
pp.grid()
pp.show()

给我一​​个好的情节

enter image description here

我想了解如何禁用其中一个轴进行平移/缩放,因此当我使用平移/缩放工具时,只有ax2会重新缩放。有没有办法做到这一点?我想以编程方式进行。

2 个答案:

答案 0 :(得分:4)

您可以使用ax2.set_navigate(False)

执行此操作
from matplotlib.pyplot import *
import numpy as np

fig,ax1 = subplots(1,1)
ax2 = ax1.twinx()
ax2.set_navigate(False)
x = np.linspace(0,2*np.pi,100)
ax1.plot(x,np.sin(x),'b')
ax1.set_xlabel('Scaleable axis')
ax1.set_ylabel('Scaleable axis')
ax2.plot(x,np.sin(x+1),'r')
ax2.set_ylabel('Static axis',weight='bold')

答案 1 :(得分:0)

A slightly more complex example with two plot areas and three vertical axes. Only the common horizontal axis and the left vertical axis of the lower subplot are interactive.

fig, ax_left = plt.subplots()
ax_right = ax_left.twinx()
ax_status = make_axes_locatable(ax_left).append_axes('top', size=1.2, pad=0., sharex=ax_left)
ax_status.xaxis.set_tick_params(labelbottom=False)
ax_right.set_navigate(False)
ax_status.set_navigate(False)

Before I added set_navigate(False) according to ali_m's answer, the two vertical axes of the lower plot were both affected by dragging the mouse vertically in the lower plot, while the status axis was unaffected as it should but only after the first mouse gesture. Dragging the mouse for the first time, all axes are affected. This seems to be a bug in matplotlib, just reported as #12613.