Matplotlib不同的缩放Y轴

时间:2018-01-30 17:59:59

标签: python pandas matplotlib

我有一个包含以下数据的数据框。

ex_dict = {'revenue': [613663,  1693667,  2145183,  2045065,  2036406,  
1708862,  1068232,  1196899,  2185852,  2165778,  2144738,  2030337,  
1784067],
'abs_percent_diff': [0.22279211315310588,  0.13248909660765254,  
0.12044821447874667,  0.09438674840975962,  0.1193588387687364,  
0.062100921139322744,  0.05875297161175445,  0.06240362963749895,  
0.05085338590212515,  0.034877614941165744,  0.012263947005671703,  
0.029227374323993634,  0.023411816504907524],
'ds': [dt.date(2017,1,1),  dt.date(2017,1,2),  dt.date(2017,1,3),  
dt.date(2017,1,4),  dt.date(2017,1,5),  dt.date(2017,1,6),  
dt.date(2017,1,7),  dt.date(2017,1,8),  dt.date(2017,1,9),  
dt.date(2017,1,10),  dt.date(2017,1,11),  dt.date(2017,1,12),  
dt.date(2017,1,13)], 
'yhat_normal': [501853.9074623253,  1952329.3521464923,  1914575.7673396615,  
1868685.8215084015,  1819261.1068672044,  1608945.031482406,  
1008953.0123101478,  1126595.36037955,  2302965.598289115,  
2244044.9351591542,  2171367.536396199,  2091465.0313570146,  
1826836.562382966]}

df_vis=pd.DataFrame.from_dict(ex_dict)

我想在同一y轴上绘制yhat_normalrevenue,在y轴上绘制abs_percent_diff,但缩放比例不同。

df_vis = df_vis.set_index('ds')
df_vis[['rev', 'yhat_normal']].plot(figsize=(20, 12))

我可以使用上面的代码轻松地绘制revyhat_normal,但我很难在不同的y轴刻度上获得abs_percent_diff。我尝试将我的列转换为numpy数组并执行此操作,但它看起来很糟糕。

npdate = df_vis.as_matrix(columns= ['ds'])
nppredictions = df_vis.as_matrix(columns= ['yhat_normal'])
npactuals = df_vis.as_matrix(columns= ['rev'])
npmape = df_vis.as_matrix(columns=['abs_percent_diff'])

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()
fig.set_size_inches(20,10)
ax1.plot_date(npdate, nppredictions, ls= '-', color= 'b')
ax1.plot_date(npdate, npactuals, ls='-', color='g')
ax2.plot_date(npdate, npmape, 'r-')

ax1.set_xlabel('X data')
ax1.set_ylabel('Y1 data', color='g')
ax2.set_ylabel('Y2 data', color='b')

plt.show()

这就是我想要的。其中红线是abs_percent_diff。显然,我手工划线,所以不准确。 enter image description here

1 个答案:

答案 0 :(得分:1)

我不确定问题是否存在,但您似乎只想在绘图区底部绘制一个数据框列。

import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt

ex_dict = {'revenue': [613663,  1693667,  2145183,  2045065,  2036406,  
1708862,  1068232,  1196899,  2185852,  2165778,  2144738,  2030337,  
1784067],
'abs_percent_diff': [0.22279211315310588,  0.13248909660765254,  
0.12044821447874667,  0.09438674840975962,  0.1193588387687364,  
0.062100921139322744,  0.05875297161175445,  0.06240362963749895,  
0.05085338590212515,  0.034877614941165744,  0.012263947005671703,  
0.029227374323993634,  0.023411816504907524],
'ds': [dt.date(2017,1,1),  dt.date(2017,1,2),  dt.date(2017,1,3),  
dt.date(2017,1,4),  dt.date(2017,1,5),  dt.date(2017,1,6),  
dt.date(2017,1,7),  dt.date(2017,1,8),  dt.date(2017,1,9),  
dt.date(2017,1,10),  dt.date(2017,1,11),  dt.date(2017,1,12),  
dt.date(2017,1,13)], 
'yhat_normal': [501853.9074623253,  1952329.3521464923,  1914575.7673396615,  
1868685.8215084015,  1819261.1068672044,  1608945.031482406,  
1008953.0123101478,  1126595.36037955,  2302965.598289115,  
2244044.9351591542,  2171367.536396199,  2091465.0313570146,  
1826836.562382966]}

df_vis=pd.DataFrame.from_dict(ex_dict)

df_vis = df_vis.set_index('ds')
ax = df_vis[['revenue','yhat_normal']].plot(figsize=(13, 8))
ax2 = df_vis['abs_percent_diff'].plot(secondary_y=True, ax=ax)
ax2.set_ylim(0,1)


plt.show()

enter image description here