我正在尝试如何缩放整个图形,而不仅仅是图中的子图。以下代码是略微修改的matplotlib示例,并显示可以手动调整子图轴限制。同样,框缩放仅允许放大子图(或链接的子图)。如何缩放整个图形?
from matplotlib.pyplot import figure, show
import numpy
figsrc = figure()
axsrc = figsrc.add_subplot(121, xlim=(0,1), ylim=(0,1), autoscale_on=False)
axzoom = figsrc.add_subplot(122, xlim=(0.45,0.55), ylim=(0.4,.6),
autoscale_on=False)
axsrc.set_title('Click to zoom')
axzoom.set_title('zoom window')
x,y,s,c = numpy.random.rand(4,200)
s *= 200
axsrc.scatter(x,y,s,c)
axzoom.scatter(x,y,s,c)
def onpress(event):
if event.button!=1: return
x,y = event.xdata, event.ydata
axzoom.set_xlim(x-0.1, x+0.1)
axzoom.set_ylim(y-0.1, y+0.1)
figsrc.canvas.draw()
figsrc.canvas.mpl_connect('button_press_event', onpress)
show()
答案 0 :(得分:0)
执行此操作的一种方法是使用canvas.resize resize方法。
请注意,主页按钮不会恢复原始视图。我无法跟踪toolbar.home()方法,而不是对QtCore.Qt.Key_Home的引用:backend_qt5.py中的'home':
from matplotlib.pyplot import figure, show
import numpy
figsrc = figure()
axsrc = figsrc.add_subplot(121, xlim=(0,1), ylim=(0,1), autoscale_on=False)
axzoom = figsrc.add_subplot(122, xlim=(0.45,0.55), ylim=(0.4,.6),
autoscale_on=False)
axsrc.set_title('LMB to zoom in, RMB to zoom out')
axzoom.set_title('zoom window')
x,y,s,c = numpy.random.rand(4,200)
s *= 200
axsrc.scatter(x,y,s,c)
axzoom.scatter(x,y,s,c)
def onpress(event):
if event.button==1:
zoomIn()
elif event.button==3:
zoomOut()
def zoomIn():
aw, ah = figsrc.canvas.get_width_height()
aw *= 1.2
ah *= 1.2
figsrc.canvas.resize(aw, ah)
figsrc.canvas.draw()
def zoomOut():
aw, ah = figsrc.canvas.get_width_height()
if (aw !=0) and (ah != 0):
aw /= 1.2
ah /= 1.2
figsrc.canvas.resize(aw, ah)
figsrc.canvas.draw()
figsrc.canvas.mpl_connect('button_press_event', onpress)
show()