【发布时间】:2019-01-26 08:14:56
【问题描述】:
【问题讨论】:
-
能否在适用的情况下提供您的代码以及您自己尝试过的内容?
标签: python matplotlib graph jupyter-notebook
【问题讨论】:
标签: python matplotlib graph jupyter-notebook
animate() 函数中更改。 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')
【讨论】:
您已经有了两个解决方案。这个为您提供了与您想要的非常相似的东西。我本可以让它看起来完全像你的输出,但我把剩下的部分留作你的练习。如果您有任何疑问,请随时问我。本方案基于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)
输出
【讨论】:
你可以用这个:
%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()
【讨论】: