matplotlib:将两个x-y图放在一个中

时间:2017-10-13 03:45:52

标签: python-3.x matplotlib curve-fitting data-fitting

我使用以下代码绘制几个数据点(xi,yi)

import numpy as np
import matplotlib.pyplot as plt

xi = np.array(data_df[['col_A']])
yi = np.array(data_df[['col_B']])

plt.figure()
plt.plot(xi, yi)

x = np.linspace(0, 30, 30)
y= np.exp(x*0.16)
plt.plot(x, y)
plt.show()

enter image description here

我希望情节看起来像这样:

enter image description here

谢谢!

1 个答案:

答案 0 :(得分:1)

用户子图在1个图中绘制超过1个图。您只需要调用plt.show()一次。

import numpy as np
import matplotlib.pyplot as plt

xi = np.array(data_df[['col_A']])
yi = np.array(data_df[['col_B']])

plt.figure()
plt.subplot(2,1,1)
plt.plot(xi, yi)

plt.subplot(2,1,2)
x = np.linspace(0, 30, 30)
y= np.exp(x*0.16)
plt.plot(x, y)
plt.show()