【问题标题】:why is plot not updated with new values correctly under some conditions为什么在某些情况下绘图未正确更新为新值
【发布时间】:2015-07-20 10:04:17
【问题描述】:

我有以下代码:

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

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

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

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

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

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

稍作更改(w2_B)后,该图具有正确的值,打印屏幕版本:

"ctrl+s" 版本:

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

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

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

    fig.canvas.draw()
    

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

    【讨论】:

    • 谢谢。两个问题:第一个:如果我没有告诉matplotlib 重绘到该图,那怎么可能,例如第一个情节被重绘了?第二:当函数应该以某种方式修改绘图时,将fig.canvas.draw()放在回调函数的末尾是否是一般规则?
    猜你喜欢
    • 2020-05-04
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 2018-06-12
    • 1970-01-01
    • 2021-08-07
    相关资源
    最近更新 更多