【问题标题】:Graph problems when producing interactive plots using matplotlib使用 matplotlib 生成交互式绘图时的图形问题
【发布时间】: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


    【解决方案1】:

    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()
    

    【讨论】:

    • 问题依旧。
    • 预期的行为是什么?信号应该只是沿着 x 轴移动吧?
    • 或者您希望在保持位置不变的情况下改变峰高?
    • 抱歉加减号,但这并没有对行为产生太大影响,只是峰值移动的 x 方向。对我来说,移动滑块后峰值消失了,图表只是一条接近零的水平线。出现随机峰,但不在 1 的高度。
    • 这很奇怪。我认为我的行为是正确的,峰值在 x=17 和 x=18 之间移动。尝试运行我的代码
    猜你喜欢
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2017-10-26
    • 2021-08-20
    • 2021-01-31
    相关资源
    最近更新 更多