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.
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.
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.
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!
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, ... ?
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:
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
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.