Pandas - How to repeat axvline through each graph

时间:2018-11-23 13:26:40

标签: python pandas matplotlib

New to pandas/python and trying to figure out how to get a repeating line throughout all of my graphs to pin point and exact location, my code is working and I have the line showing for one therefore I would imagine I need to loop through all of them, but I simply don't know how to do it.

Code is following:

crimeDataBelfastMonthByType = crimeData[['Ward Name','Month']]
crimeDataBelfastMonthByType = pd.DataFrame({'count' : crimeDataBelfastMonthByType.groupby([ "Ward Name", "Month"] ).size()}).reset_index()
fig3 = pd.pivot_table(crimeDataBelfastMonthByType, values='count', index=['Month'], columns=['Ward Name'], fill_value=0)
fig3.index = pd.DatetimeIndex(fig2.index)
fig3.plot(figsize=(30, 30), subplots=True, layout=(-1, 6), sharex=False, sharey=False);
plt.axvline(x = ['2016-01'], color='red',linestyle='--');

Graph is the following: Graphs

1 个答案:

答案 0 :(得分:0)

DataFrame.plot()方法返回其结果图中所有matplotlib子图轴的numpy数组作为其第一个参数。

解决方案::保存轴阵列,循环遍历,然后在每个子图上调用ax.axvline()

# Unchanged lines from your example
crimeDataBelfastMonthByType = crimeData[['Ward Name','Month']]
crimeDataBelfastMonthByType = pd.DataFrame({'count' : crimeDataBelfastMonthByType.groupby([ "Ward Name", "Month"] ).size()}).reset_index()
fig3 = pd.pivot_table(crimeDataBelfastMonthByType, values='count', index=['Month'], columns=['Ward Name'], fill_value=0)
fig3.index = pd.DatetimeIndex(fig2.index)

# Modified lines below
axarr = fig3.plot(figsize=(30, 30), subplots=True, layout=(-1, 6), sharex=False, sharey=False);
for ax in axarr.flat:
    ax.axvline(x = ['2016-01'], color='red',linestyle='--');