[英]Delete plotted function in python
問題是只要我在slider上設置一個新的function,舊的功能並沒有被刪除,但仍然顯示。 我試圖用clf和cla刪除之前的function,但是坐標系不再顯示(這里是clf和cla https的網站鏈接://www.activestate.com/resources/quick-reads/how -to-clear-a-plot-in-python/ )。 謝謝你的回答
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
# 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=(10, 10))
#//////////////////////////////////////////////////////////////////////////////
# x range
x = np.arange(-5, 5., 0.025)
# start value
m1 = 1
t1 = 0
m2 = 1
t2 = 0
# Define the sliders position
ax_m1 = plt.axes([0.2, 0.075, 0.25, 0.03],)
ax_t1 = plt.axes([0.2, 0.125, 0.25, 0.03],)
ax_m2 = plt.axes([0.55, 0.075, 0.25, 0.03],)
ax_t2 = plt.axes([0.55, 0.125, 0.25, 0.03],)
# Define Slider value
mSlider1 = Slider(ax_m1, 'm1', -5.0, 5.0, valinit=m1, valstep=.01)
tSlider1 = Slider(ax_t1, 't1', -5.0, 5.0, valinit=t1, valstep=.01)
mSlider2 = Slider(ax_m2, 'm2', -5.0, 5.0, valinit=m2, valstep=.01)
tSlider2 = Slider(ax_t2, 't2', -5.0, 5.0, valinit=t2, valstep=.01)
#//////////////////////////////////////////////////////////////////////////////update
def update_m1(mSlider1):
global m1
m1 = mSlider1
def update_t1(tSlider1):
global t1
t1 = tSlider1
def update_m2(mSlider2):
global m2
m2 = mSlider2
def update_t2(tSlider2):
global t2
t2 = tSlider2
def update_f1(y1):
y1 = m1*x + t1
ax.plot(x, y1, lw = 3, alpha = 0.5, color="blue")
def update_f2(y2):
y2 = m2*x + t2
ax.plot(x, y2, lw = 3, alpha = 1, color = "orange")
mSlider1.on_changed(update_m1,)
tSlider1.on_changed(update_t1,)
mSlider1.on_changed(update_f1,)
tSlider1.on_changed(update_f1,)
mSlider2.on_changed(update_m2,)
tSlider2.on_changed(update_t2,)
mSlider2.on_changed(update_f2,)
tSlider2.on_changed(update_f2,)
#//////////////////////////////////////////////////////////////////////////////
# 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()
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
# 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=(10, 10))
#//////////////////////////////////////////////////////////////////////////////
# x range
x = np.arange(-5, 5., 0.025)
# start value
m1 = 1
t1 = 0
m2 = 1
t2 = 0
# Define the sliders position
ax_m1 = plt.axes([0.2, 0.075, 0.25, 0.03],)
ax_t1 = plt.axes([0.2, 0.125, 0.25, 0.03],)
ax_m2 = plt.axes([0.55, 0.075, 0.25, 0.03],)
ax_t2 = plt.axes([0.55, 0.125, 0.25, 0.03],)
# Define Slider value
mSlider1 = Slider(ax_m1, 'm1', -5.0, 5.0, valinit=m1, valstep=.01)
tSlider1 = Slider(ax_t1, 't1', -5.0, 5.0, valinit=t1, valstep=.01)
mSlider2 = Slider(ax_m2, 'm2', -5.0, 5.0, valinit=m2, valstep=.01)
tSlider2 = Slider(ax_t2, 't2', -5.0, 5.0, valinit=t2, valstep=.01)
#//////////////////////////////////////////////////////////////////////////////update
y1 = m1*x + t1
a1 = ax.plot(x, y1, lw = 3, alpha = 0.5, color="blue")
y2 = m2*x + t2
a2 = ax.plot(x, y2, lw = 3, alpha = 1, color = "orange")
def update_m1(mSlider1):
global m1
m1 = mSlider1
def update_t1(tSlider1):
global t1
t1 = tSlider1
def update_m2(mSlider2):
global m2
m2 = mSlider2
def update_t2(tSlider2):
global t2
t2 = tSlider2
def update_f1(y1):
global a1
line = a1.pop(0)
line.remove()
y1 = m1*x + t1
a1 = ax.plot(x, y1, lw = 3, alpha = 0.5, color="blue")
def update_f2(y2):
global a2
line = a2.pop(0)
line.remove()
y2 = m2*x + t2
a2 = ax.plot(x, y2, lw = 3, alpha = 1, color = "orange")
mSlider1.on_changed(update_m1,)
tSlider1.on_changed(update_t1,)
mSlider1.on_changed(update_f1,)
tSlider1.on_changed(update_f1,)
mSlider2.on_changed(update_m2,)
tSlider2.on_changed(update_t2,)
mSlider2.on_changed(update_f2,)
tSlider2.on_changed(update_f2,)
#//////////////////////////////////////////////////////////////////////////////
# 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.