【问题标题】:How to plot square function with matplotlib如何使用 matplotlib 绘制平方函数
【发布时间】:2016-02-23 13:55:33
【问题描述】:

我有一个在 0 和 1 之间交替的值列表,例如 [0,1,0,1,0],我想用 matplotlib for python 将它们绘制成方波。到目前为止我有这个:

input_amp = [1,0,1,0,1,0,1,0,1,0]
plt.plot(input_amp, marker='d', color='blue')
plt.title("Waveform")
plt.ylabel('Amplitude')
plt.xlabel("Time")
plt.savefig("waveform.png")
plt.show()

这给了我这样的输出 :

我该如何做到这一点,而不是在直线保持平坦的点之间形成一个角度?

我找到了这个post,但它更多地处理动画,而不仅仅是绘制函数。

【问题讨论】:

    标签: python matplotlib graphing


    【解决方案1】:

    您引用的那篇帖子中的相关位是drawstyle

     plt.plot(input_amp, marker='d', color='blue', drawstyle='steps-pre')
    

    【讨论】:

    • drawstyle='steps-post' 比较常用。
    【解决方案2】:

    “初级,华生!”

    import matplotlib.pyplot as plot
    import numpy as np
    
    # Sampling rate 1000 hz / second
    t = np.linspace(0, 1, 100, endpoint=True)
    
    # Plot the square wave signal
    plot.plot(t, signal.square(2 * np.pi * 1 * t))
    
    # A title for the square wave plot
    plot.title('1Hz square wave sampled at 100 Hz /second')
    
    # x axis label for the square wave plot
    plot.xlabel('Time')
        
    # y axis label for the square wave plot
    plot.ylabel('Amplitude')
    plot.grid(True, which='both')
    
    # Provide x axis and line color
    plot.axhline(y=0, color='k')
    
    # Set the max and min values for y axis
    plot.ylim(-1.2, 1.2)
    
    # Display the square wave
    plot.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多