创建子图
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
unrate = pd.read_csv('UNRATE.csv')
unrate['DATE'] = pd.to_datetime(unrate['DATE']) #时间日期转换
unrate['Month'] = unrate['DATE'].dt.month
# flg = plt.figure() # 创建画图区间
flg = plt.figure(figsize=(10,6)) # 创建画图区间,设置长高度
ax1 = flg.add_subplot(2,2,1) # 增加子图
ax2 = flg.add_subplot(2,2,2)
ax3 = flg.add_subplot(2,2,4)
ax1.plot(np.random.randint(1,5,5),np.arange(5))
# 在同一个图中画两条折线
ax2.plot(unrate.loc[0:11]['Month'],unrate.loc[0:11]["VALUE"],c='red')
ax2.plot(unrate.loc[12:23]['Month'],unrate.loc[12:23]["VALUE"],c='green')
plt.show()
![](https://img2023.cnblogs.com/blog/1597781/202309/1597781-20230918232826531-1573194996.png)
绘制不同颜色,不同数据的折线
# 绘制不同颜色,不同数据的折线
flg2 = plt.figure(figsize=(10,6)) # 创建画图区间,设置长高度
colors = ['red','blue','green','orange','black']
for i in range(5):
start_index = i*12
end_index = (i+1)*12
subset = unrate[start_index:end_index]
label = str(1948+i)
plt.plot(subset['Month'],subset['VALUE'],c=colors[i],label=label) # 设置标签、颜色等
plt.legend(loc='upper left') # 显示标签并定位
plt.xlabel('Month, Integer')
plt.ylabel('Unemployment Rate, Percent')
plt.title('Month Unemployment Trends, 1948-1952')
plt.show()
![](https://img2023.cnblogs.com/blog/1597781/202309/1597781-20230918232812552-1000477545.png)