Václav Bočan
Václav Bočan

Reputation: 89

How to plot multiple columns on both axis in plotly express?

I'm trying to plot a line chart with multiple lines from a pandas dataframe. I want to use several column pairs as data for x and y axis, but plotly express lets me use just one column for x axis. The dataframe looks something like this:

df = pandas.DataFrame({"sample 1 x":[0,1,3,6,11,18,28,41,58], "sample 1 y":[0,1,2,3,5,7,10,13,17],
                       "sample 2 x":[0,1,2,4,7,10,14,17,19], "sample 2 y":[0,1,1,2,3,3,4,3,2]})

The outcome is simple multiline graph, but the problem is that the x-axis values are specific for each data series. Here's the approximate layout I'd like to get (example generated with actual data). I have a workaround where I plot individual lines first using px and then add them together using graph objects. It's however cumbersome (I didn't figure out how to add legend for example).

What would be the correct and easy way how to achieve this?

Upvotes: 1

Views: 2513

Answers (2)

Théo Badoz
Théo Badoz

Reputation: 1

In fact you could melt the dataframe

import plotly.express as px

df1 = pd.melt(df, id_vars=['sample 1 x'], value_vars=['sample 1 y']).rename(columns={"sample 1 x": "x"})
df2 = pd.melt(df, id_vars=['sample 2 x'], value_vars=['sample 2 y']).rename(columns={"sample 2 x": "x"})
df = df1.merge(df2, how="outer", on="x")

fig = px.lines(df, x="x", y="value", color="variable")
fig.show()

If you don't want to alterate the original dataframe here is an alternative solution using plotly graph_objects (repost of @r-beginners)

import plotly.graph_objects as go

fig = go.Figure()
fig.add_scatter(x=df['sample 1 x'], y=df['sample 1 y'], name='sample 1', mode='lines')
fig.add_scatter(x=df['sample 2 x'], y=df['sample 2 y'], name='sample 2', mode='lines')

fig.show()

Upvotes: 0

Václav Bočan
Václav Bočan

Reputation: 89

Soo I figured I need to melt the data in dataframe to long format instead of wide.

Upvotes: 0

Related Questions