1

I have a Pandas DataFrame which resembles:

pd.DataFrame({
    'el1': {
        'steps': [0,1,2], 
        'values': [10, 9, 8]
    },
    'el2': {
        'steps': [0,1,2], 
        'values': [1,  2, 8]
    },
    'el3': {
        'steps': [0,1,2], 
        'values': [5,  9, 4]
    }
})

        el1         el2         el3
steps   [0, 1, 2]   [0, 1, 2]   [0, 1, 2]
values  [10, 9, 8]  [1, 2, 8]   [5, 9, 4]

what would be the best way to try and use Panda's DataFrame plot to get some simple line plots with values on the y axis and steps on the x axis? (e.g. there should be three lines)

2 Answers 2

1

Use matplotlib

c = ['r', 'g', 'b']
for i in range(df.shape[1]):
    plt.plot(df.iloc[0, i], df.iloc[1, i], c=c[i], label=df.columns[i])
plt.legend(df.columns)
plt.xlabel('Steps')
plt.ylabel('Values')
plt.show()

plt

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

2 Comments

Sorry, I meant to edit my own answer but edited yours instead. Not sure how to reject it xD
@kerwei I've taken care of it.
1

Here's a different construction. Largely because I'm more comfortable with transforming the underlying data frame first before plotting. The plotting of the graph is pretty much the same so, credits go to @meW for the lines of code.

import pandas as pd
import matplotlib.pyplot as plt


df = pd.DataFrame({
    'el1': {
        'steps': [0,1,2], 
        'values': [10, 9, 8]
    },
    'el2': {
        'steps': [0,1,2], 
        'values': [1,  2, 8]
    },
    'el3': {
        'steps': [0,1,2], 
        'values': [5,  9, 4]
    }
})

ndf = pd.DataFrame({v:df[v].values[1] for v in df.columns})
ndf.index = df.el1.steps
ndf.columns = df.columns

>>>ndf
   el1  el2  el3
0   10    1    5
1    9    2    9
2    8    8    4

plt.plot(ndf)
plt.legend(ndf.columns)
plt.xlabel('Steps')
plt.ylabel('Values')
plt.show()

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.