【发布时间】:2020-05-05 17:43:11
【问题描述】:
我想为每个 Planning Authority 创建 5 x 7 子图以容纳 34 个折线图,其中 a 轴为 Permission Financial Year,y 值为 sum 的 Net additional units
我已经使用groupby.sum 来找出每个当局每年的净增加单位总和,并手动创建了一些子图以进行说明。但是,我确信必须有一些更有效的方法,例如使用loop 来自动化/简化所有图表的生成。
希望有人可以提供帮助。非常感谢。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import randint
from numpy.random import randint
pd.set_option('display.max_colwidth', 0)
df = pd.read_csv('LDD Permissions for Datastore - Final selected updated 19.04.2020 - including checked05.05 -used.csv', encoding='mac_roman')
#Aggregate the sum of net additional units by Permission Fianacial Year and Planning Authority
pd.set_option('display.max_rows', 1000)
total_permitted_unit_per_year = df.groupby(['Permission Financial Year','Planning Authority']).sum()['Net additional units'].unstack(-1)
#Generate subplots of net additional units from 2009 to 2019 for each Planning Authority
plt.rc('xtick',labelsize=10)
plt.rc('ytick',labelsize=10)
fig = plt.figure()
plt.figure(figsize=(200, 800))
ax1 = fig.add_subplot(7,5,1)
ax2 = fig.add_subplot(7,5,2)
ax3 = fig.add_subplot(7,5,3)
ax4 = fig.add_subplot(7,5,4)
ax5 = fig.add_subplot(7,5,5)
ax6 = fig.add_subplot(7,5,6)
ax7 = fig.add_subplot(7,5,7)
ax8 = fig.add_subplot(7,5,8)
ax9 = fig.add_subplot(7,5,9)
ax10 = fig.add_subplot(7,5,10)
ax11 = fig.add_subplot(7,5,11)
ax12 = fig.add_subplot(7,5,12)
total_permitted_unit_per_year.plot(y='Barking and Dagenham', ax=ax1, legend=False, marker='.', markersize=5, figsize=(30,15))
ax1.set_title('Barking and Dagenham')
ax1.set_ylim([0, 350])
total_permitted_unit_per_year.plot(y='Barnet', ax=ax2, legend=False, marker='.', markersize=5, figsize=(30,15))
ax2.set_title('Barnet')
ax2.set_ylim([0, 350])
total_permitted_unit_per_year.plot(y='Bexley', ax=ax3, legend=False, marker='.', markersize=5, figsize=(30,15))
ax3.set_title('Bexley')
ax3.set_ylim([0, 350])
total_permitted_unit_per_year.plot(y='Sutton', ax=ax4, legend=False, marker='.', markersize=5, figsize=(30,15))
ax4.set_title('Sutton')
ax4.set_ylim([0, 350])
total_permitted_unit_per_year.plot(y='Merton', ax=ax5, legend=False, marker='.', markersize=5)
ax5.set_title("Merton")
ax5.set_ylim([0, 350])
【问题讨论】:
标签: python matplotlib pandas-groupby