gpt4 book ai didi

python - 为什么在某些情况下绘图未正确更新为新值

转载 作者:行者123 更新时间:2023-12-01 04:37:14 25 4
gpt4 key购买 nike

我有以下代码:

#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
# MODULE WHICH BEHAVES LIKE SLIDER MODULE BORROWED FROM FOLLOWING ANSWER:
# http://stackoverflow.com/questions/25934279/add-a-vertical-slider-with-matplotlib
from vertical_slider_module import VertSlider
########################################
t = np.arange(0.0, 1.0, 0.001)
amplitude_initial = 5
frequency_initial = 3
########################################
# min_factor, max_factor, initial_value
amplitude_factor = (0.1, 10.0, amplitude_initial)
frequency_factor = (0.1, 10.0, frequency_initial)
phase_factor = (-np.pi/4, np.pi/4)
vshift_factor = (0.1, 10.0)
########################################
w1 = amplitude_factor[2]*np.sin(2*np.pi*frequency_factor[2]*t)
w2 = amplitude_factor[2]*np.cos(2*np.pi*frequency_factor[2]*t)
wsum = w1 + w2
########################################
plt.close('all')
# fig, ax = plt.subplots(nrows=3, ncols=1)
fig, ax = plt.subplots(nrows=3, ncols=1, sharex=True)
########################################
line0, = ax[0].plot(t,w1, lw=2, color='red', label="red")
line1, = ax[1].plot(t,w2, lw=2, color='green', label="green")
line2, = ax[2].plot(t,wsum, lw=2, color='blue', label="blue")
########################################
ax[0].set_xlim([0, 1])
ax[0].set_ylim([-amplitude_factor[1], amplitude_factor[1]])

ax[1].set_xlim([0, 1])
ax[1].set_ylim([-amplitude_factor[1], amplitude_factor[1]])

# ADJUST POSITION
plt.subplots_adjust(left=0.5)

# SET GRID FOR ALL SUBPLOTS
max = ax[0].get_xticks()[-1]
xticks_doubled = np.linspace(0,max,num=11)
for (x,y) in np.ndenumerate(ax):
y.grid()
y.set_xticks(xticks_doubled)
########################################
# A = 1 # amplitude
# D = 0 # vertical_shift
# B = x_max # frequency
# C = 0 # phase

# SET SLIDER POSITION PARAMETERS
axcolor = 'lightgoldenrodyellow'
left = 0.05
bottom = 0.14
width = 0.01
height = fig.get_size_inches()[1] * 0.1 # Q: DIFFERENT SCALE
hgap = 0.02

# plt.axes([left, bottom, width, height], axisbg=axcolor)
ax_w1_A = plt.axes([left+0*(width+hgap), bottom, width, height], axisbg=axcolor)
ax_w1_B = plt.axes([left+1*(width+hgap), bottom, width, height], axisbg=axcolor)
ax_w1_C = plt.axes([left+2*(width+hgap), bottom, width, height], axisbg=axcolor)
ax_w1_D = plt.axes([left+3*(width+hgap), bottom, width, height], axisbg=axcolor)

vs_w1_A = VertSlider(ax_w1_A, 'w1_A', amplitude_factor[0], amplitude_factor[1], valinit=amplitude_factor[2])
vs_w1_B = VertSlider(ax_w1_B, 'w1_B', frequency_factor[0], frequency_factor[1], valinit=frequency_factor[2])
vs_w1_C = VertSlider(ax_w1_C, 'w1_C', phase_factor[0], phase_factor[1])
vs_w1_D = VertSlider(ax_w1_D, 'w1_D', vshift_factor[0], vshift_factor[1])

ax_w2_A = plt.axes([left+4*(width+hgap), bottom, width, height], axisbg=axcolor)
ax_w2_B = plt.axes([left+5*(width+hgap), bottom, width, height], axisbg=axcolor)
ax_w2_C = plt.axes([left+6*(width+hgap), bottom, width, height], axisbg=axcolor)
ax_w2_D = plt.axes([left+7*(width+hgap), bottom, width, height], axisbg=axcolor)

vs_w2_A = VertSlider(ax_w2_A, 'w2_A', amplitude_factor[0], amplitude_factor[1], valinit=amplitude_factor[2])
vs_w2_B = VertSlider(ax_w2_B, 'w2_B', frequency_factor[0], frequency_factor[1], valinit=frequency_factor[2])
vs_w2_C = VertSlider(ax_w2_C, 'w2_C', phase_factor[0], phase_factor[1])
vs_w2_D = VertSlider(ax_w2_D, 'w2_D', vshift_factor[0], vshift_factor[1])
########################################
def w1_update(val):

global w1
# global wsum
A = vs_w1_A.val
B = vs_w1_B.val

w1 = A*np.sin(2*np.pi*B*t)
line0.set_ydata(w1)

wsum = w1 + w2
line2.set_ydata(wsum)

ax[2].relim()
ax[2].autoscale_view()

print w1[50]
print w2[50]
print "#############"

vs_w1_A.on_changed(w1_update)
vs_w1_B.on_changed(w1_update)


def w2_update(val):

global w2
# global wsum
A = vs_w2_A.val
B = vs_w2_B.val

w2 = A*np.cos(2*np.pi*B*t)
line1.set_ydata(w2)

wsum = w1 + w2
line2.set_ydata(wsum)

ax[2].relim()
ax[2].autoscale_view()

print w1[50]
print w2[50]
print "#############"

vs_w2_A.on_changed(w2_update)
vs_w2_B.on_changed(w2_update)
########################################
plt.show()

此代码创建三个绘图。第三幅图是第一幅图和第二幅图的总和。第一个和第二个图的参数可以通过 slider 修改。当通过 slider 修改给定参数时,绘图会动态更改。以下是参数列表(C 和 D 尚未实现):

  • A = 振幅
  • B = 频率
  • C = 水平位移(相位)
  • D = 垂直移动

问题就在这里;有时,当我通过 slider 修改值时,绘图似乎未正确更新。对我来说,这似乎是 matplotlib 错误,因为当我通过 ctr+s 保存绘图并检查它时,值是正确的。以下是一些图片,可以解释我的意思:

以下图片是通过打印屏幕按钮拍摄的。正如您在 slider 上看到的,第二个图的振幅设置为 8.27 (w2_A),但从图中可以明显看出,振幅仍保持在值 5: enter image description here

这是同一张图片,但使用 ctrl+s 保存后。这里的值是正确的: enter image description here

稍作更改(w2_B)后,绘图具有正确的值,打印屏幕版本: enter image description here

“ctrl+s”版本: enter image description here

我还注意到,当我单击 C 和 D slider 时,它有时会稍微修改波浪。对于这些 slider 没有注册更新功能,为什么会发生这种情况?

最佳答案

您更改了回调函数中的行,但没有告诉 matplotlib 重绘到图形。将此行添加到回调函数的末尾:

fig.canvas.draw()

并且您的绘图应该按预期更新。

关于python - 为什么在某些情况下绘图未正确更新为新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31513691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com