【问题标题】:Plotting sum of two sinusoids in Python在 Python 中绘制两个正弦曲线的总和
【发布时间】:2019-01-26 08:14:56
【问题描述】:

我想在 Python 中绘制两个正弦曲线的总和,就像附加的屏幕截图一样。 您能否推荐我如何在 matplotlib 中做到这一点?

【问题讨论】:

  • 能否在适用的情况下提供您的代码以及您自己尝试过的内容?

标签: python matplotlib graph jupyter-notebook


【解决方案1】:

查看此解决方案

  • 为了在 Python 中绘制两个正弦曲线的总和,我使用 MatplotlibNumPy 来生成动画正弦波并将输出导出为 gif 文件。
  • 这是一个可自定义的代码,其中 X 轴和 Y 轴的方程和变量可以在 animate() 函数中更改。

Python 代码

    import numpy as np
    from matplotlib import pyplot as plt
    from matplotlib.animation import FuncAnimation

    plt.style.use('seaborn-pastel')

    fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True, sharey=True)
    fig.suptitle('Sine waves')

    ax1.set_xlim(0, 4)
    ax1.set_ylim(-4, 4)

    line1, = ax1.plot([], [], color='r', lw=3)
    line2, = ax2.plot([], [], color='g', lw=3)
    line3, = ax3.plot([], [], color='b', lw=6)
    plt.legend([line1, line2, line3],['sin(x1)', 'sin(x2)', 'sin(x1)+sin(x2)'])


    def init():
        line1.set_data([], [])
        line2.set_data([], [])
        line3.set_data([], [])
        return line1, line2, line3


    def animate(i):
        x1 = np.linspace(0, 4, 1000)
        y1 = np.sin(2 * np.pi * (1.1*x1 - 0.05 * i))
        line1.set_data(x1, y1)

        x2 = np.linspace(0, 4, 1000)
        y2 = np.sin(2 * np.pi * (1.21 * x2 - 0.04 * i))
        line2.set_data(x2, y2)

        x3 = np.linspace(0, 4, 1000)
        y3 = np.sin(2 * np.pi * (1.1*x3 - 0.05 * i)) + np.sin(2 * np.pi * (1.21 * x3 - 0.04 * i))
        line3.set_data(x3, y3)
    
        return line1, line2, line3


    anim1 = FuncAnimation(fig, animate, init_func=init,
                     frames=200, interval=20, blit=True)

    anim1.save('sine_wave.gif', writer='imagemagick')

输出(gif 文件)

独特性

  • 这个 Python 代码是独一无二的,因为它使用基本的 Python 库 NumPyMatplotlib 以及用于图像处理的 pillow 库来绘制动画正弦波即导出动画 gif。

【讨论】:

  • 这个答案绘制了两个正弦波以及合成波并将其导出到 gif 文件中。
  • 上面提供的代码可以自定义为任何给定的正弦方程。
  • 因此您还可以更改给定方程 y1、y2、y3 中的值
【解决方案2】:

您已经有了两个解决方案。这个为您提供了与您想要的非常相似的东西。我本可以让它看起来完全像你的输出,但我把剩下的部分留作你的练习。如果您有任何疑问,请随时问我。本方案基于https://matplotlib.org/examples/pylab_examples/finance_work2.html

import numpy as np
import matplotlib.pyplot as plt
left, width = 0.1, 0.8
rect1 = [left, 0.65, width, 0.25]  # left, bottom, width, height
rect2 = [left, 0.4, width, 0.25]
rect3 = [left, 0.1, width, 0.3]

fig = plt.figure(figsize=(10, 6))

ax1 = fig.add_axes(rect1) 
ax2 = fig.add_axes(rect2, sharex=ax1)
ax3 = fig.add_axes(rect3, sharex=ax1)

x = np.linspace(0, 6.5*np.pi, 200)
y1 = np.sin(x)
y2 = np.sin(2*x)

ax1.plot(x, y1, color='b', lw=2)
ax2.plot(x, y2, color='g', lw=2)
ax3.plot(x, y1+y2, color='r', lw=2)

ax3.get_xaxis().set_ticks([])

for ax in [ax1, ax2, ax3]:
    ax.hlines(0, 0, 6.5*np.pi, color='black')
    for key in ['right', 'top', 'bottom']:
        ax.spines[key].set_visible(False)

plt.xlim(0, 6.6*np.pi)
ax3.text(2, 0.9, 'Sum signal', fontsize=14)

输出

【讨论】:

    【解决方案3】:

    你可以用这个:

    %matplotlib inline
    from matplotlib.pyplot import figure
    import matplotlib.pyplot as plt
    from numpy import arange, sin, pi
    
    t = arange(0.0, 2.0, 0.01)
    
    fig = figure(1)
    
    ax1 = fig.add_subplot(311)
    ax1.plot(t, sin(2*pi*t))
    ax2 = fig.add_subplot(312)
    ax2.plot(t, sin(4*pi*t))
    ax3 = fig.add_subplot(313)
    ax3.plot(t, sin(4*pi*t)+sin(2*pi*t))
    plt.show()
    

    【讨论】:

      【解决方案4】:

      或者一些“更简单”的东西:

      import matplotlib.pyplot as plt
      import numpy as np
      
      x = np.arange(0,10,0.01)
      x2 = np.arange(0,20,0.02)
      
      sin1 = np.sin(x)
      sin2 = np.sin(x2)
      
      x2 /= 2
      
      sin3 = sin1+sin2
      
      plt.plot(x,sin3)
      plt.show()
      

      【讨论】:

      • 很遗憾,这并不能真正回答最初的问题。
      猜你喜欢
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 2015-10-26
      相关资源
      最近更新 更多