0

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.

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. I tried converting my columns to numpy arrays and doing this, but it looks terrible.

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. Obviously, I drew the line by hand so it is not accurate. enter image description here

4
  • Hi, I edited you question but still your example is not replicable as npdate, nppredictions, npactuals and npmape are missing. Anyway have you checked this documentation? Commented Jan 30, 2018 at 18:11
  • Ok, I added some code. Yes, I have checked the documentation. Commented Jan 30, 2018 at 18:35
  • Could you format the code such that it can be copied and run? Also, showing an image of the output and clearly stating what is wrong with it could motivate people to answer here. Commented Jan 30, 2018 at 22:18
  • @ImportanceOfBeingErnest Yeah good idea. I formatted the code so you can easily load it into a dataframe. Commented Jan 30, 2018 at 23:00

1 Answer 1

1

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

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.