You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

30 lines
946 B

import matplotlib.pyplot as plt
import numpy as np
# 存储初始范围和当前比例
initial_limits = {'x': None, 'y': None}
current_scale = 1.0
fig, ax = plt.subplots()
x = np.linspace(0, 10, 100)
ax.plot(x, np.sin(x))
def on_press(event):
if event.button == 3: # 右键按下
initial_limits['x'] = ax.get_xlim()
initial_limits['y'] = ax.get_ylim()
def on_release(event):
global current_scale
if event.button == 3 and initial_limits['x'] is not None:
new_xlim = ax.get_xlim()
scale_x = (initial_limits['x'][1] - initial_limits['x'][0]) / \
(new_xlim[1] - new_xlim[0])
print(f"X轴缩放比例: {scale_x:.2f}")
current_scale *= scale_x
print(f"累计总缩放: {current_scale:.2f}")
if __name__ == '__main__':
fig.canvas.mpl_connect('button_press_event', on_press)
fig.canvas.mpl_connect('button_release_event', on_release)
plt.show()