第六回:场景案例显神通
学习参考:
https://github.com/datawhalechina/fantastic-matplotlib
作业
1.用Drugs数据集,做出面积图的多子图形式。
注意,需要添加如下要素:
①添加每个子图标题,在子图右上方;
②添加整个画布的总标题,在画布左上方;
③添加X和Y轴的标签。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
#导入数据集并转成方便作图的格式
Dataset = pd.read_csv('data/Drugs.csv')
Dataset.head()
group = Dataset.groupby(['YYYY','State']).agg('sum').reset_index()
group.head()
df = group.pivot(index='YYYY', columns='State', values='DrugReports').reset_index()
df.head()
plt.style.use('seaborn-darkgrid')
palette = plt.get_cmap('Set2') # 颜色卡
plt.figure(figsize=(15, 10))
#绘制
num=0
for column in df.drop('YYYY', axis=1):
num+=1
# 设定子图在画布的位置
plt.subplot(3,3, num)
# 画线图
plt.fill_between(df['YYYY'], df[column], alpha=0.5, label=column)
# 设定子图的X轴和Y轴的范围
plt.xlim(2009.3,2017.3)
plt.ylim(0,50000)
# 添加每个子图的标题
plt.title(column, loc='right', fontsize=12, color=palette(num))
#添加整个画布的标题
plt.suptitle("How many DrugReports the 5 states have in past few years?", fontsize=13, fontweight=0, color='black', style='italic', x=0.25,y=0.95)
#添加整个画布的横纵坐标的名称
plt.text(2014, -9500, 'Year', ha='center', va='center') # 在此使用子图坐标而非画布坐标
plt.text(1998, 60000, 'DrugReports', ha='center', va='center', rotation='vertical')