创建子图
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()
绘制不同颜色,不同数据的折线
# 绘制不同颜色,不同数据的折线
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()
标签:loc,plt,--,ML,子图,Month,unrate,flg
From: https://www.cnblogs.com/guowenrui/p/17713438.html