为什么在某些条件下绘图没有正确更新新值

时间:2015-07-20 10:04:18

标签: python matplotlib

我已经关注了代码:

#!/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()

此代码创建三个图。第3个图是第1和第2个图的总和。可以通过滑块修改第1和第2图的参数。通过滑块修改给定参数时,将动态更改绘图。以下是参数列表(C和D尚未实现):

  • A =幅度
  • B =频率
  • C =水平移位(阶段)
  • D =垂直移位

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

以下图片是通过打印屏幕按钮拍摄的。正如您在滑块上看到的那样,将第二个绘图的幅度设置为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滑块时,它有时会略微修改波形。对于那些滑块没有注册更新功能,为什么会发生这种情况?

1 个答案:

答案 0 :(得分:2)

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

fig.canvas.draw()

并且您的情节应该按预期更新。