# Setting multiple error bars with new plotly express 'Wide data' feature

**URL:** <https://community.plotly.com/t/setting-multiple-error-bars-with-new-plotly-express-wide-data-feature/40382>\
**Category:** 📊 Plotly Python\
**Created:** [June 1, 2020, 12:52pm UTC](https://community.plotly.com/t/setting-multiple-error-bars-with-new-plotly-express-wide-data-feature/40382 "2020-06-01T12:52:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoryL](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@RoryL](https://community.plotly.com/u/RoryL)\
**Post date:** [June 1, 2020, 12:52pm UTC](https://community.plotly.com/t/setting-multiple-error-bars-with-new-plotly-express-wide-data-feature/40382/1 "2020-06-01T12:52:44Z")

</div>

Hello,

I’ve been enjoying the Plotly update, especially the wide-form support for plotly express. My question relates to the new functionality - if I define a plot with;

px.scatter(dataframe, x = ‘xaxis\_column\_name’ , y= [‘yaxis\_col\_1’, ‘y\_axis\_col\_2’])

It doesn’t appear to be possible to add error bars in a similar, columnar fashion, i.e.

px.scatter(dataframe, x = ‘xaxis\_column\_name’ , y= [‘yaxis\_col\_1’, ‘y\_axis\_col\_2’], error\_y = [‘yaxis\_col\_1\_error’, ‘y\_axis\_col\_2\_error’])

Since error\_y must have the same length as the x axis.  
If I do set error\_y with a single column or errors, it is applied to all traces simultaneously, which is not always appropriate.

Are there any plans to add the ability to define columns of errors in the wide-form input? I think this would be very convenient. What are the suggested workarounds at present? My workflow is such that I’d prefer to call the figure first, inspect it, and then add error bars to the traces post declaration.

I understand that traces could be added one by one with .add\_trace(), but I like the px API as it seems much more elegant and less verbose.

Thanks,  
Rory

---

<div class="post-metadata">

**Author:** ![nicolaskruchten](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nicolaskruchten/32/7018_2.png) [@nicolaskruchten](https://community.plotly.com/u/nicolaskruchten)\
**Post date:** [June 1, 2020, 3:18pm UTC](https://community.plotly.com/t/setting-multiple-error-bars-with-new-plotly-express-wide-data-feature/40382/2 "2020-06-01T15:18:36Z")

</div>

Unfortunately there’s no way to do this kind of “correlated wide-form” at the moment… Your best bet would be to try do use a long-form input here.

---

<div class="post-metadata">

**Author:** ![nicolaskruchten](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nicolaskruchten/32/7018_2.png) [@nicolaskruchten](https://community.plotly.com/u/nicolaskruchten)\
**Post date:** [June 1, 2020, 3:20pm UTC](https://community.plotly.com/t/setting-multiple-error-bars-with-new-plotly-express-wide-data-feature/40382/3 "2020-06-01T15:20:46Z")

</div>

It’s a good idea though! I’ve logged an issue here for further thought and discussion: [https://github.com/plotly/plotly.py/issues/2522](https://github.com/plotly/plotly.py/issues/2522)

---

<div class="post-metadata">

**Author:** ![RoryL](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@RoryL](https://community.plotly.com/u/RoryL)\
**Post date:** [June 1, 2020, 7:42pm UTC](https://community.plotly.com/t/setting-multiple-error-bars-with-new-plotly-express-wide-data-feature/40382/4 "2020-06-01T19:42:17Z")

</div>

Hi Nicolas! Thanks for taking a look. I’ve tried a bunch of different data transformations to get this working, but the most elegant solution I could find for now was to simply create the go.Scatter objects with the correct link between data and error bars.

I’m glad to hear you like the suggestion however. I’d love to see this feature added as it would simplify the whole process greatly. Anyway, the new plotly express wide data format has been hugely helpful, great update!

---

<div class="post-metadata">

**Author:** ![nicolaskruchten](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nicolaskruchten/32/7018_2.png) [@nicolaskruchten](https://community.plotly.com/u/nicolaskruchten)\
**Post date:** [June 1, 2020, 8:46pm UTC](https://community.plotly.com/t/setting-multiple-error-bars-with-new-plotly-express-wide-data-feature/40382/5 "2020-06-01T20:46:29Z")

</div>

I’ll see if I can come up with a little recipe for you… Just to confirm, your data is in a Pandas data frame and has something like `x, y1, error_y1, y2, error_y2, ...` ?

---

<div class="post-metadata">

**Author:** ![RoryL](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@RoryL](https://community.plotly.com/u/RoryL)\
**Post date:** [June 1, 2020, 9:03pm UTC](https://community.plotly.com/t/setting-multiple-error-bars-with-new-plotly-express-wide-data-feature/40382/6 "2020-06-01T21:03:22Z")

</div>

That’s very kind. Yes, that’s exactly right

---

<div class="post-metadata">

**Author:** ![nicolaskruchten](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nicolaskruchten/32/7018_2.png) [@nicolaskruchten](https://community.plotly.com/u/nicolaskruchten)\
**Post date:** [June 1, 2020, 9:22pm UTC](https://community.plotly.com/t/setting-multiple-error-bars-with-new-plotly-express-wide-data-feature/40382/7 "2020-06-01T21:22:45Z")

</div>

It feels like something like this would work: [https://stackoverflow.com/questions/55403008/pandas-partial-melt-or-group-melt](https://stackoverflow.com/questions/55403008/pandas-partial-melt-or-group-melt)

---

<div class="post-metadata">

**Author:** ![RoryL](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@RoryL](https://community.plotly.com/u/RoryL)\
**Post date:** [June 2, 2020, 7:49am UTC](https://community.plotly.com/t/setting-multiple-error-bars-with-new-plotly-express-wide-data-feature/40382/8 "2020-06-02T07:49:29Z")

</div>

Maybe I am missing something, but I don’t understand how the long form data format gets me closer to plotting the data. Here’s an MWE I’ve been playing with to understand your SO link:

```auto
import numpy as np
import pandas as pd
import plotly.express as px

def f(x, m, c):
    return m*x + c

x = np.arange(10)
y = f(x, 3, 1)
y2 = f(x, 5, 2)

y_err, y2_err = [np.random.random(len(x)) for _ in [y, y2]]

df=pd.DataFrame(data=[x,y,y2,y_err, y_err]).T
df.columns= ['x','y','y2','y_err','y2_err']

# this is basically the graph I want, just with the correct error bars
# setting y_error applies it to both
f1 = px.scatter(df, x='x', y=['y', 'y2'], error_y='y_err')

df_long = df.stack().reset_index().rename(columns = {'level_1': 'Variable'})

#how best to achieve the plot from here is still unclear 

```

---

<div class="post-metadata">

**Author:** ![nicolaskruchten](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nicolaskruchten/32/7018_2.png) [@nicolaskruchten](https://community.plotly.com/u/nicolaskruchten)\
**Post date:** [June 2, 2020, 12:28pm UTC](https://community.plotly.com/t/setting-multiple-error-bars-with-new-plotly-express-wide-data-feature/40382/9 "2020-06-02T12:28:36Z")

</div>

Here’s how I would approach this: first you `melt()` just the `y` data, the usual way. Then you unstack the error values into a single column, which should still be well-aligned with the main dataset. Then you add the unstacked error values to the main dataset, and you plot the data in long-form, so you don’t pass any lists to `y`.

```python
import pandas as pd
import numpy as np

df = pd.DataFrame(dict(
    x=range(20),
    y1=np.random.rand(20).cumsum(),
    y_error1=np.random.rand(20),
    y2=np.random.rand(20).cumsum(),
    y_error2=np.random.rand(20)
))

print(df.head())

long_df = df.melt(id_vars="x", value_vars=["y1", "y2"], value_name="y", var_name="y_name")
long_df["y_error"] = df[["y_error1", "y_error2"]].unstack().values

print(long_df.head())

px.scatter(long_df, x="x", y="y", error_y="y_error", color="y_name")

```

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/6/69280abc9307f3f03504ba3fab4ed1e80a96febf.png)

---

<div class="post-metadata">

**Author:** ![RoryL](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@RoryL](https://community.plotly.com/u/RoryL)\
**Post date:** [June 4, 2020, 7:51am UTC](https://community.plotly.com/t/setting-multiple-error-bars-with-new-plotly-express-wide-data-feature/40382/10 "2020-06-04T07:51:15Z")

</div>

Thanks for this solution. I’m sure this will work perfectly for my data

Appreciate you taking the time to have a look at this. I was struggling to reformat the data correctly, but this is a great example!

Cheers!
