0

I have two different DataFrames in Python, one is the actual revenue values and the second one is the values of the prediction with the accumulative per day (index of the rows). Both DataFrames have the same length.

enter image description here

I want to compare them on the same plot, row by row. If I want to plot only one row from each DataFrame, I use this code:

df_actual.loc[71].T.plot(figsize=(14,10), kind='line')
df_preds.loc[71].T.plot(figsize=(14,10), kind='line')

The output is this:

enter image description here

However, the ideal output is to have all the rows for each DataFrame in a grid so I can compare all the results:

enter image description here

I have tried to create a for loop to itinerate each row but it is not working:

for i in range(20):
  df_actual.loc[i].T.plot(figsize=(14,10), kind='line')
  df_preds.loc[i].T.plot(figsize=(14,10), kind='line')

Is there any way to do this that is not manual? Thanks!

1 Answer 1

1

it would be helpful if you provided a sample of your dfs.

assuming both dfs have the same length & assuming you want 2 columns, try this:

fig, ax = plt.subplots(round(len(df_actual)/2),2)
ax.ravel()
for i in range(len(ax)):
   sns.lineplot(df_actual.loc[i].T, ax=ax[i], color="navy")
   sns.lineplot(df_preds.loc[i].T, ax=ax[i], color="orange")

edit: this works for me (you just have to add your .T):

import matplotlib.pyplot as plt 
import seaborn as sns
import pandas as pd 

df_actual = pd.DataFrame(data=[[1,2,3,4,5], [6,7,8,9,10]], columns = ["col1","col2", "col3", "col4", "col5"])
df_pred = pd.DataFrame(data=[[3,4,5,6,7], [8,9,10,11,12]], columns = ["col1", "col2", "col3", "col4", "col5"])

fig, ax = plt.subplots(round(len(df_actual)/2),2) 
ax.ravel() 
for i in range(len(ax)): 
    ax[i].plot(df_actual.loc[i], color="navy") 
    ax[i].plot(df_pred.loc[i], color="orange")
Sign up to request clarification or add additional context in comments.

5 Comments

Hi! Thanks for your answer. I provided a sample on the main message. Both DataFrames have the same length. I tried your code but it is not working. It says: 'numpy.ndarray' object has no attribute 'xaxis'. Thanks a lot!
by sample I mean a code snippet that can be used to create a similar df. check my updated comment
Hey! Ok, I'll take your advice for the next questions! I still not working for me. It says: AttributeError: 'numpy.ndarray' object has no attribute 'plot'. Anyway, thanks! I will plot them manually, :)
did you use ax.ravel() and ax[i] ? can you post your code?
I did, but I think there is something wrong on my dataframe. My code: fig, ax = plt.subplots(round(len(df_actual)/2),2) ax.ravel() for i in range(len(ax)): ax[i].plot(df_actual.loc[i].T, color="navy") ax[i].plot(df_pred.loc[i].T, color="orange")

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.