第六回:场景案例显神通

第六回:场景案例显神通
学习参考:
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')

在这里插入图片描述

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值