简体   繁体   中英

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? Thanks for any hints

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM