房产网站制作找哪家,wordpress内网访问,合适的网站建设明细报价表,网站程序 seo继上一篇在 Java 中缩放拖动图片后#xff0c;在python matplotlib中也来实现一个自由缩放拖动的例子#xff1a; python matplotlib 中缩放#xff0c;较为简单#xff0c;只需要通过设置要显示的 x y坐标的显示范围即可。基于此#xff0c;实现一个鼠标监听回调#xf…继上一篇在 Java 中缩放拖动图片后在python matplotlib中也来实现一个自由缩放拖动的例子 python matplotlib 中缩放较为简单只需要通过设置要显示的 x y坐标的显示范围即可。基于此实现一个鼠标监听回调在回调中计算滚轮缩放或者鼠标拖动之后的坐标范围即可。 效果 上代码
import matplotlib.pyplot as plt
import matplotlib as mplfrom matplotlib.text import Text, Annotation
from matplotlib.patches import Polygon, Rectangle, Circle, Arrow, ConnectionPatch,Ellipse,FancyBboxPatch
from matplotlib.widgets import Button, Slider, Widget# https://www.python100.com/html/85915.html
# patches 是matplotlib里面的一个库里面有基本图形绘制 Polygon多边形 Rectangle矩形 Circle圆 Arrow箭头 ConnecctionPatch链接线 Ellipse:椭圆fig plt.figure()
ax fig.add_subplot(111)rect Rectangle((0.1,0.1),1,1,coloryellow)
ax.add_patch(rect)rect2 Circle((1.5,1.5),0.2,colorred)
ax.add_patch(rect2)arrow ConnectionPatch((1,3),(1.8,1.8), data, data, clip_onTrue,arrowstyle-|, shrinkA5, shrinkB5, mutation_scale20, fcw)
arrow.set_annotation_clip(False)
ax.add_patch(arrow)fancybox FancyBboxPatch((2,2),width1,height1, boxstylempl.patches.BoxStyle(Round, pad0.2),colorgreen)
ax.add_patch(fancybox)ax.text(2, 0.2, Hello World)startx0
starty0
mPressFalse
def call_move(event):# print(event.name)global mPressglobal startxglobal starty# print(mPress)if event.namebutton_press_event:axtempevent.inaxesif axtemp and event.button1:print(event)mPressTruestartxevent.xdatastartyevent.ydataelif event.namebutton_release_event:axtempevent.inaxesif axtemp and event.button1:mPressFalseelif event.namemotion_notify_event:axtempevent.inaxesif axtemp and event.button1 and mPress:x_min, x_max axtemp.get_xlim()y_min, y_max axtemp.get_ylim()wx_max-x_minhy_max-y_min# print(event)# 移动mxevent.xdata-startxmyevent.ydata-starty# 注意这里 -mx, 因为下一次 motion事件的坐标已经是在本次做了移动之后的坐标系了所以要体现出来# startxevent.xdata-mx startxevent.xdata-(event.xdata-startx)startx, 没必要再赋值了# startyevent.ydata-my# print(mx,my,x_min,y_min,w,h)axtemp.set(xlim(x_min-mx, x_min-mxw))axtemp.set(ylim(y_min-my, y_min-myh))fig.canvas.draw_idle() # 绘图动作实时反映在图像上returndef call_scroll(event):print(event.name)axtempevent.inaxesprint(event:,event)print(event.xdata,event.ydata)# 计算放大缩小后 xlim 和ylimif axtemp:x_min, x_max axtemp.get_xlim()y_min, y_max axtemp.get_ylim()w x_max - x_minh y_max - y_mincurxevent.xdatacuryevent.ydatacurXposition(curx - x_min) / wcurYposition(cury - y_min) / hif event.button down:print(befor:,w,h)w w*1.1h h*1.1print(down,w,h)elif event.button up:print(befor:,w,h)w w/1.1h h/1.1print(up,w,h)print(curXposition,curYposition)newxcurx - w*curXpositionnewycury - h*curYpositionaxtemp.set(xlim(newx, newxw))axtemp.set(ylim(newy, newyh))fig.canvas.draw_idle() # 绘图动作实时反映在图像上
fig.canvas.mpl_connect(scroll_event, call_scroll)
fig.canvas.mpl_connect(button_press_event, call_move)
fig.canvas.mpl_connect(button_release_event, call_move)
# fig.canvas.mpl_connect(draw_event, call_move)
fig.canvas.mpl_connect(motion_notify_event, call_move)# 我们可以最后来设置 x y 轴的初始大小范围
ax.set_xlim(0,10)
ax.set_ylim(0,10)plt.show()
注意上面demo监听的是 鼠标左键拖动 event.button1 这个会导致和原版的工具栏 放大镜 工具冲突所以也可以 把 event.button 3 用鼠标右键来判断 1 是左键2是中间滚轮按下去键3是右键。