Matplotlib缩放整个figu

2024-10-01 02:36:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试如何缩放整个图形,而不仅仅是图形中的子图。下面的代码是对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()

Tags: importnumpyeventadd图形matplotlibshowfigure
1条回答
网友
1楼 · 发布于 2024-10-01 02:36:11

一种方法是使用画布.调整大小调整大小方法。在

请注意,“主页”按钮不会恢复原始视图。我没能追踪到工具栏.home()方法QtCore.Qt.Key_主页:后端中的“home”_qt5.py:

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

相关问题 更多 >