你好,我想控制一个点的位置px和y方向使用滑块。我的问题是,我的观点一直回到原点(x0 l y0(。我在网上搜索了我的问题的答案,但没有找到合适的答案。感谢回复ps:Geogebra不是一个选项,只有python
#imports
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from matplotlib.widgets import Slider, Button, RadioButtons
# x and y variable for the point
f = lambda y: y
g = lambda x: x
#start position for x and y (x0 I y0)
x = 0
y = 0
# Select length of axes and the space between tick labels
xmin, xmax, ymin, ymax = -5, 5, -5, 5
ticks_frequency = 1
# Plot point P at the position (x0 I y0)
fig, ax = plt.subplots(figsize=(50, 10))
ptplot, = plt.plot(x, y, 'ko')
# Slider position
ax_x = plt.axes([0.25, 0.15, 0.65, 0.03])
ax_y = plt.axes([0.25, 0.1, 0.65, 0.03])
# Slider array for x and y and min and max value
xSlider = Slider(ax_x, 'x', -5.0, 5.0, valinit=x, valstep=0.1)
ySlider = Slider(ax_y, 'y', -5.0, 5.0, valinit=y, valstep=0.1)
# update for x position of the Point
# if slider changed update the x position
def update_x(x):
x = g(x)
ptplot.set_data(x,y)
xSlider.eventson = False
xSlider.set_val(x)
fig.canvas.draw()
xSlider.eventson = True
# update for y position of the Point
# if slider changed update the y position
def update_y(y):
y = f(y)
ptplot.set_data(x,y)
ySlider.eventson = False
ySlider.set_val(y)
fig.canvas.draw()
ySlider.eventson = True
# update on change / if slider is changed -> Update
xSlider.on_changed(update_x)
ySlider.on_changed(update_y)
# Set identical scales for both axes
ax.set(xlim=(xmin-1, xmax+1), ylim=(ymin-1, ymax+1), aspect='equal')
# Set bottom and left spines as x and y axes of coordinate system
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')
# show the plot
plt.show()`
问题是变量x和y只是局部更改,而不是全局
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from matplotlib.widgets import Slider, Button, RadioButtons
#start pos
x = 0
y = 0
# Select length of axes and the space between tick labels
xmin, xmax, ymin, ymax = -5, 5, -5, 5
ticks_frequency = 1
# Plot points
fig, ax = plt.subplots(figsize=(50, 10))
ptplot, = plt.plot(x, y, 'ko')
ax_x = plt.axes([0.25, 0.15, 0.65, 0.03])
ax_y = plt.axes([0.25, 0.1, 0.65, 0.03])
xSlider = Slider(ax_x, 'x', -5.0, 5.0, valinit=x, valstep=0.1)
ySlider = Slider(ax_y, 'y', -5.0, 5.0, valinit=y, valstep=0.1)
def update_x(xSlider):
global x
x = xSlider
ptplot.set_data(x,y)
xSlider.eventson = False
xSlider.set_val(x)
xSlider.eventson = True
def update_y(ySlider):
global y
y = ySlider
ptplot.set_data(x,y)
ySlider.eventson = False
ySlider.set_val(y)
ySlider.eventson = True
xSlider.on_changed(update_x)
ySlider.on_changed(update_y)
# Set identical scales for both axes
ax.set(xlim=(xmin-1, xmax+1), ylim=(ymin-1, ymax+1), aspect='equal')
# Set bottom and left spines as x and y axes of coordinate system
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')
# Remove top and right spines
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# Create 'x' and 'y' labels placed at the end of the axes
ax.set_xlabel('x', size=14, labelpad=-24, x=1.03)
ax.set_ylabel('y', size=14, labelpad=-21, y=1.02, rotation=0)
# Create custom major ticks to determine position of tick labels
x_ticks = np.arange(xmin, xmax+1, ticks_frequency)
y_ticks = np.arange(ymin, ymax+1, ticks_frequency)
ax.set_xticks(x_ticks[x_ticks != 0])
ax.set_yticks(y_ticks[y_ticks != 0])
# Create minor ticks placed at each integer to enable drawing of minor grid
# lines: note that this has no effect in this example with ticks_frequency=1
ax.set_xticks(np.arange(xmin, xmax+1), minor=True)
ax.set_yticks(np.arange(ymin, ymax+1), minor=True)
# Draw major and minor grid lines
ax.grid(which='both', color='grey', linewidth=1, linestyle='-', alpha=0.2)
# Draw arrows
arrow_fmt = dict(markersize=4, color='black', clip_on=False)
ax.plot((1), (0), marker='>', transform=ax.get_yaxis_transform(), **arrow_fmt)
ax.plot((0), (1), marker='^', transform=ax.get_xaxis_transform(), **arrow_fmt)
plt.show()