您对 matplotlib 绘图函数和 pandas 绘图包装器感到困惑。
你遇到的问题是ax.plot
没有任何x
or y
争论。
Use ax.plot
在这种情况下,可以这样称呼它ax.plot(df0['Date'], df0[['y1','y2']])
, 没有x
, y
and title
。可能单独设置标题。
例子:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
countries = np.random.choice(list("ABCDE"),size=25)
df = pd.DataFrame({"Date" : range(200),
'Country' : np.repeat(countries,8),
'y1' : np.random.rand(200),
'y2' : np.random.rand(200)})
fig = plt.figure()
for c,num in zip(countries, xrange(1,26)):
df0=df[df['Country']==c]
ax = fig.add_subplot(5,5,num)
ax.plot(df0['Date'], df0[['y1','y2']])
ax.set_title(c)
plt.tight_layout()
plt.show()
![enter image description here](https://i.stack.imgur.com/y4tHe.png)
使用 pandas 绘图包装器
在这种情况下,通过绘制您的数据df0.plot(x="Date",y =['y1','y2'])
.
Example:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
countries = np.random.choice(list("ABCDE"),size=25)
df = pd.DataFrame({"Date" : range(200),
'Country' : np.repeat(countries,8),
'y1' : np.random.rand(200),
'y2' : np.random.rand(200)})
fig = plt.figure()
for c,num in zip(countries, xrange(1,26)):
df0=df[df['Country']==c]
ax = fig.add_subplot(5,5,num)
df0.plot(x="Date",y =['y1','y2'], title=c, ax=ax, legend=False)
plt.tight_layout()
plt.show()
![enter image description here](https://i.stack.imgur.com/yVRNv.png)