lineplot not giving the desired results

Question:

Scenarios Node-0 Node-1 Node-2 Node-3 Node-4 Node-5
Original System 1 1 1 1 1 1
Leak_Node_1 1 0.043 1 1 1 0.0043
Leak_Node_2 1 1 1 1 1 0.012
Leak_Node_3 1 1 1 1 1 0.0086
Leak_Node_4 1 1 1 1 1 0.0085
Leak_Node_5 1 1 1 1 1 0.0076

How do I get the exact kind of line plot in the link below:
https://drive.google.com/file/d/19_RVA9t7YvarTmc4W1Im2DtqevHvvZG0/view?usp=sharing

I tried using the code below:
pi1.plot.line()

Asked By: Khola

||

Answers:

You have to transpose you DataFrame, because pandas evaluates columns and in your example the figure plots the rows.

from io import StringIO
import pandas as pd
t="""Scenarios  Node-0  Node-1  Node-2  Node-3  Node-4  Node-5
Original System     1   1   1   1   1   1
Leak_Node_1     1   0.043   1   1   1   0.0043
Leak_Node_2     1   1   1   1   1   0.012
Leak_Node_3     1   1   1   1   1   0.0086
Leak_Node_4     1   1   1   1   1   0.0085
Leak_Node_5     1   1   1   1   1   0.0076"""
df = pd.read_csv(StringIO(t), sep='ss+', index_col=0, engine='python')
df.T.plot.line()

Result

Answered By: mosc9575

Make sure to set ‘Scenarios’ as index in the dataframe and transpose it before plotting.

pi1.set_index('Scenarios').transpose().plot.line()

It should give you this results:
resulting plot with transposed dataframe

Note: If you need to match the colors too (since you stated exactly), use the color argument of df.plot.line() (see the documentation here)

Answered By: Andi R
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.