我认为用户输入数字进行缩放并不是很方便。更为标准的方式是由各种matplotlib tools提供的鼠标交互。
有用于在不同的情节变焦没有标准的工具,但是我们可以很容易地提供与显示在下面的代码使用matplotlib.widgets.RectangleSelector
此功能。
我们需要在两个子图中绘制相同的数据,并将RectangleSelector连接到其中一个子图(ax)。每次进行选择时,第一个子图中的选择的数据坐标都被简单地用作第二个子图的轴限制,有效地证明放大(或放大)功能。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import RectangleSelector
def onselect(eclick, erelease):
#http://matplotlib.org/api/widgets_api.html
xlim = np.sort(np.array([erelease.xdata,eclick.xdata ]))
ylim = np.sort(np.array([erelease.ydata,eclick.ydata ]))
ax2.set_xlim(xlim)
ax2.set_ylim(ylim)
def toggle_selector(event):
# press escape to return to non-zoomed plot
if event.key in ['escape'] and toggle_selector.RS.active:
ax2.set_xlim(ax.get_xlim())
ax2.set_ylim(ax.get_ylim())
x = np.arange(100)/(100.)*7.*np.pi
y = np.sin(x)**2
fig = plt.figure()
ax = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
#plot identical data in both axes
ax.plot(x,y, lw=2)
ax.plot([5,14,21],[.3,.6,.1], marker="s", color="red", ls="none")
ax2.plot(x,y, lw=2)
ax2.plot([5,14,21],[.3,.6,.1], marker="s", color="red", ls="none")
ax.set_title("Select region with your mouse.\nPress escape to deactivate zoom")
ax2.set_title("Zoomed Plot")
toggle_selector.RS = RectangleSelector(ax, onselect, drawtype='box', interactive=True)
fig.canvas.mpl_connect('key_press_event', toggle_selector)
plt.show()
您是否知道已经有了(http://matplotlib.org/users/navigation_toolbar.html)来实现通常的matplotlib绘制窗口中的[缩放工具]? – ImportanceOfBeingErnest
我没有!感谢您的链接。尽管我并没有试图实际放大,而是试图绘制与自身平行的更大数据集的一部分。也许缩放是不正确的词。 – neerbasu
所以,也许你正在寻找[这样的事情](http://gtk3-matplotlib-cookbook.readthedocs.io/en/latest/zooming.html)? – ImportanceOfBeingErnest