Python使用Matplotlib创建带滑块的绘图函数。

huangapple go评论99阅读模式
英文:

Python function that draws a plot with matplotlib slider

问题

Matplotlib plot using slider widget

我目前正在进行数字调制信号的解调工作,以完成一个实际的任务,并希望能够使我的一些代码可重用。我想创建一个在单独文件中的函数,我可以调用它来绘制一个类似上面那个示例的带有滑块的图形。

我尝试过将我用于生成上面图形的代码直接放入外部函数中,但是滑块不起作用,因此我无法实现图形的实时更新。是否有办法可以实现我想要的效果,或者是不可能的?

这是我当前在外部文件中定义的代码:

#functions.py

def Find_Sampling_indexes(phase_shift:float,period:float, sample_rate:int):
    Clk = np.cos(2*np.pi*sample_rate*period-phase_shift)

    indexes = signal.find_peaks(Clk)
    index = indexes[0]

    return index

fig, ax = plt.subplots()

def update(complex_data,period,symbol_rate,val):
        index = Find_Sampling_indexes(val,period,symbol_rate)
        ax.clear()
        ax.plot(np.real(complex_data[index]),np.imag(complex_data[index]), 'g o')
        plt.draw()

def slider_phase_shift(complex_data,period,symbol_rate):
    
    index = Find_Sampling_indexes(0,period,symbol_rate)
    ax.plot(np.real(complex_data[index]),np.imag(complex_data[index]), 'g o')
    plt.xlim(-1,1)
    plt.xlim(-1,1)
    
    plt.subplots_adjust(bottom=0.25, left=0.25)
    ax_Slider = plt.axes([0.25, 0.1, 0.65, 0.03])
    sliders = Slider(ax_Slider, ' Phase', -2, 2, 0)

    sliders.on_changed(update(complex_data, period, symbol_rate, sliders.val))
    show()
    return

然后在我的主文件中导入并调用它,如下所示:

#main.py
esc.slider_phase_shift(OOK_Complex, t1, 93696)
英文:

Matplotlib plot using slider widget

I am currently working on demodulating digitally modulated signals for a practical assignement and wish to make some of my code reuseable. I would like to create a function in a seperate file that I can call that will draw me a graph of some sort that has a slider like the one above.

I have tried just using the code I used to generate the plot above in said external function but then the slider does not work and therefore I can't live update teh figure. Is there any way I can achive what I wish to achive or is it not possible?

Here is my current set of code. defined in a external file.

#functions.py
    
def Find_Sampling_indexes(phase_shift:float,period:float, sample_rate:int):
    Clk = np.cos(2*np.pi*sample_rate*period-phase_shift)

    indexes = signal.find_peaks(Clk)
    index = indexes[0]

    return index

fig, ax = plt.subplots()



def update(complex_data,period,symbol_rate,val):
        index = Find_Sampling_indexes(val,period,symbol_rate)
        ax.clear()
        ax.plot(np.real(complex_data[index]),np.imag(complex_data[index]), 'g o')
        plt.draw()

def slider_phase_shift(complex_data,period,symbol_rate):
    
    index = Find_Sampling_indexes(0,period,symbol_rate)
    ax.plot(np.real(complex_data[index]),np.imag(complex_data[index]), 'g o')
    plt.xlim(-1,1)
    plt.xlim(-1,1)
    
    plt.subplots_adjust(bottom=0.25, left =0.25)
    ax_Slider = plt.axes([0.25, 0.1, 0.65, 0.03])
    sliders = Slider(ax_Slider, ' Phase', -2, 2,0)

    sliders.on_changed(update(complex_data,period,symbol_rate,sliders.val))
    show()
    return

it is then imported into my main file and called as such.

#main.py
esc.slider_phase_shift(OOK_Complex,t1,93696)

答案1

得分: 0

我已经能够找到解决我的问题的方法。我需要将滑块返回到我的主程序,然后使用on_change函数来调用也在我的外部函数中的更新函数。我还需要使用全局变量来使我的初始函数中的信息对更新函数可用。

def Update_Shifting(val):
    ax1.clear()
    frequency = freq.val
    phase_shift = phase.val
    comp = Frequency_Shift(comp_data, frequency, t_a)
    p = Phase_Shift(comp, phase_shift)
    ax1.plot(np.real(p), np.imag(p), 'o')
    draw()
    return

def Slider_Shifting(complex_data, t, freq_low_lim: int, freq_high_lim: int, phase_low_lim: int, phase_high_lim: int, Modulation_type: str):
    global comp_data, freq, phase, t_a, ax1
    t_a = t
    comp_data = complex_data
    fig, ax1 = plt.subplots()
    plt.subplots_adjust(bottom=0.35)
    plt.plot(np.real(complex_data), np.imag(complex_data), 'o')
    plt.xlim(-1, 1)
    plt.ylim(-1, 1)
    plt.xlabel('Real (I)')
    plt.ylabel('Imaginary (Q)')
    plt.title(f'{Modulation_type} Frequncy and phase shifting')

    axfreq = plt.axes([0.25, 0.15, 0.65, 0.03])
    axphase = plt.axes([0.25, 0.1, 0.65, 0.03])

    freq = Slider(axfreq, 'Frequency Shift', freq_low_lim, freq_high_lim, 0)
    phase = Slider(axphase, 'Phase Shift', phase_low_lim, phase_high_lim, (phase_low_lim + phase_high_lim) / 2)

    return freq, phase

然后,当我调用该函数时,我执行以下操作:

fig, ax = esc.Slider_Shifting(OOK_Complex, tOOK, -50, 50, -3, -2, 'OOK')

fig.on_changed(esc.Update_Shifting)
ax.on_changed(esc.Update_Shifting)
plt.show()

这允许我实现了我想要的功能。

英文:

I have been able to find a way to fix my issue. I need to return my sliders to my main program and then use the on_change function to call the update functions that are also in my external function. I also had to make use of global variables to make the information from my initial function available to the update function.

def Update_Shifting(val):
    ax1.clear()
    frequency = freq.val
    phase_shift = phase.val
    comp = Frequency_Shift(comp_data,frequency,t_a)
    p = Phase_Shift(comp, phase_shift)
    ax1.plot(np.real(p), np.imag(p), ' o')
    draw()
    return

def Slider_Shifting(complex_data, t, freq_low_lim:int,freq_high_lim:int,phase_low_lim:int,phase_high_lim:int,Modulation_type:str):
    global comp_data, freq, phase, t_a,ax1
    t_a = t
    comp_data = complex_data
    fig, ax1 = plt.subplots()
    plt.subplots_adjust(bottom=0.35)
    plt.plot(np.real(complex_data), np.imag(complex_data), ' o')
    plt.xlim(-1,1)
    plt.ylim(-1,1)
    plt.xlabel('Real (I)')
    plt.ylabel('Imaginary (Q)')
    plt.title(f'{Modulation_type} Frequncy and phase shifting')

    axfreq = plt.axes([0.25, 0.15, 0.65, 0.03])
    axphase = plt.axes([0.25, 0.1, 0.65, 0.03])

    freq = Slider(axfreq, 'Frequency Shift', freq_low_lim, freq_high_lim, 0)
    phase = Slider(axphase, 'Phase Shift', phase_low_lim, phase_high_lim, (phase_low_lim +phase_high_lim)/2)

    return freq,phase

Then when I call the function I do the following:

fig, ax = esc.Slider_Shifting(OOK_Complex, tOOK,-50,50,-3,-2,'OOK')

fig.on_changed(esc.Update_Shifting)
ax.on_changed(esc.Update_Shifting)
plt.show()

This allows me to do what I wanted.

huangapple
  • 本文由 发表于 2023年8月5日 02:31:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定