1

This is my input dataframe:

labels  percentile_5  percentile_25  median  percentile_75  percentile_98
0       0            21             25      27             30             34
1       1            49             52      56             61             71
2       2            34             36      39             43             48

I have tried transform using the below code:

t = df.T.reset_index()
t.columns = t.iloc[0]
t = t.iloc[1:]

Output:

     labels         0   1   2
1   percentile_5    21  49  34
2   percentile_25   25  52  36
3   median          27  56  39
4   percentile_75   30  61  43
5   percentile_98   34  71  48

Using this I can create line chart with only one line.

fig = px.line(t, x="labels", y=0)
fig.show()

I am trying to plot like this, x-axis = labels, legend = 0,1,2

enter image description here

What are the changes I need to do in dataframe or is there any easy way to plot using the first dataframe?

1 Answer 1

2

If I understand correctly you don't have to do any changes. Just run:

fig = px.line(df, x='labels', y = df.columns)

enter image description here

This should be what you're looking for since you've specified x=labels and legend = 0, 1, 2.

Complete code

import pandas as pd
import plotly.express as px


df = pd.DataFrame({'labels': {1: 'percentile_5',
                              2: 'percentile_25',
                              3: 'median',
                              4: 'percentile_75',
                              5: 'percentile_98'},
                             '0': {1: 21, 2: 25, 3: 27, 4: 30, 5: 34},
                             '1': {1: 49, 2: 52, 3: 56, 4: 61, 5: 71},
                             '2': {1: 34, 2: 36, 3: 39, 4: 43, 5: 48}})
fig = px.line(df, x='labels', y = df.columns)
fig.show()
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.