简体   繁体   English

Matplotlib不同比例的Y轴

[英]Matplotlib Different Scaled Y-Axes

I have a dataframe with the data below. 我有一个数据框,下面的数据。

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)

I want to graph yhat_normal and revenue on the same y-axis and abs_percent_diff on a y-axis with a different scale. 我想在同一y轴上绘制yhat_normalrevenue ,在y轴上abs_percent_diff ,比例不同。

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

I can easily graph rev and yhat_normal with the code above, but I am struggling to get abs_percent_diff on a different y-axis scale. 我可以使用上面的代码轻松地绘制revyhat_normal图形,但是我正努力在不同的y轴比例上获得abs_percent_diff I tried converting my columns to numpy arrays and doing this, but it looks terrible. 我尝试将列转换为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()

This is what I want. 这就是我要的。 Where the red line is the abs_percent_diff. 红线是abs_percent_diff。 Obviously, I drew the line by hand so it is not accurate. 显然,我是手工绘制的,所以不准确。 在此处输入图片说明

I'm not sure if I got the problem correclty, but it seems you simply want to draw one of the dataframe columns at the bottom of the plot area. 我不确定问题是否解决,但您似乎只是想在绘图区域的底部绘制数据框列之一。

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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM