使用matplotlib生成交互式绘图时的图形问题



我试图用滑块生成一个交互式绘图。但当我移动滑块时,图形的峰值高度停止变为1。根据Desmos的说法,图形应该是这样的:函数图

在不改变峰值高度的情况下,当余弦内的值改变时,只改变峰值的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编程的新手,所以如果有任何错误,请原谅我。谢谢

np.cos(amp)更改为np.cos(np.deg2rad(amp))(这是因为np.cos期望输入角度以弧度为单位(

此外,我认为在基于您在帖子中链接到的图像计算的公式中,(1/t)之后应该有一个+

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
# 0 radians or 0 degress doesn't matter so you can neglect conversion here
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(np.deg2rad(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()

相关内容

  • 没有找到相关文章

最新更新