我有以下代码:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
#
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
plt.close('all')
for i in range (0,3):
y = y + np.pi/2
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(10, 4))
ax1.plot(x, y)
ax1.set_title('Title: i=%s' % (i))
ax1.set_xlabel('X_plot1 title')
ax1.set_ylabel('Y_plot1 title')
ax1.set_xlim ([0.0, 5])
ax1.set_ylim ([0.0, 6])
ax2.scatter(x, y)
ax2.set_xlabel('Y_plot2 title')
ax2.set_ylabel('X_plot2 title')
plt.show()
我想在子图 1 中显示所有 3 个线图,在子图 2 中显示所有 3 个散点图。
请您参考如下方法:
这是你想做的吗?
如果您不想创建多个绘图图形,则必须将 plt.subplots(...) 调用拉出 for 循环的范围。
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(10, 4))
for i in range (0,3):
y = y + np.pi/2
ax1.plot(x, y, label="i=%s" % i)
#ax1.set_title('Title: i=%s' % (i))
ax1.set_xlabel('X_plot1 title')
ax1.set_ylabel('Y_plot1 title')
ax1.set_xlim ([0.0, 5])
ax1.set_ylim ([0.0, 9])
ax2.scatter(x, y, label="i=%s" % i)
ax2.set_xlabel('Y_plot2 title')
ax2.set_ylabel('X_plot2 title')
ax1.legend()
ax2.legend()
