【发布时间】:2021-03-25 00:44:20
【问题描述】:
我试图用滑块制作交互式绘图。 但是当我移动滑块时,图表的峰值高度不再变为 1。 根据 Desmos,图表应该是这样的: Graph of the function
不改变峰高,只有当余弦内的值发生变化时,峰的 x 值才会发生变化。 图的编码有问题吗?因为功能比较复杂。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
fig, ax = plt.subplots()
ax.set_ylabel("L")
ax.set_xlabel("Energy in keV")
ax.set_title("Output graph for change of spectrum with the detection angle")
plt.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(10, 20.0, 0.01)
a0 = 0
s = 0.01/(0.01+2*np.power(((1/((1/t)-((1-np.cos(a0)))/512))-17),2))
l, = plt.plot(t, s, lw=2)
ax.margins(x=0)
axcolor = 'lightgoldenrodyellow'
axamp = plt.axes([0.25, 0.09, 0.65, 0.03], facecolor=axcolor)
samp = Slider(axamp, 'Detection angle',0.0, 180.0,valstep=5, valinit=a0)
def update(val):
amp = samp.val
l.set_ydata(0.1/(0.1+2*np.power(((1/((1/t)-(1-np.cos(amp))/512))-17),2)))
fig.canvas.draw_idle()
samp.on_changed(update)
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
def reset(event):
samp.reset()
button.on_clicked(reset)
rax = plt.axes([0.025, 0.5, 0.13, 0.15], facecolor=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0)
def colorfunc(label):
l.set_color(label)
fig.canvas.draw_idle()
radio.on_clicked(colorfunc)
colorfunc(radio.value_selected)
plt.show()
我对 Python 编程很陌生,如果有任何错误,请原谅我。 谢谢。
【问题讨论】:
标签: python numpy matplotlib graph