Hi,
I have a Pandas data frame (variable name is data) as follows with dynamic number of rows and columns:
[244 rows x 8 columns]
2021/1/7 22:30:21 2021/1/7 22:30:32
0 -0.128906 -0.125000
1 -0.125000 -0.128906
2 -0.125000 -0.128906
3 -0.121094 -0.128906
4 -0.125000 -0.125000
.. ... ...
595 -0.125000 -0.125000
596 -0.125000 -0.125000
597 -0.125000 -0.125000
598 -0.128906 -0.125000
599 -0.128906 -0.128906
I was able to plot this data as a box plot in plotly dash app using the following code:
df = pd.DataFrame.from_dict(data, orient='index')
df = df.transpose()
figure = px.box(
df
)
I’m trying to change the box plot where the color of each trace depends on the column name.
Example: The trace color should be red if the column name ends in a ‘1’ or green if it ends in a ‘2’.
I’ve had success in updating the color using figure.for_each_trace() as follows:
figure.for_each_trace(
lambda trace: trace.update(fillcolor='red')
)
I thought of adding a named row at the end of the dataframe and appending the color name manually while creating the dataframe, to use it as the trace color.
df.loc['colors']=['red', 'yellow']
New dataframe with this added row is shown below:
[600 rows x 2 columns]
2021/1/7 22:30:21 2021/1/7 22:30:32
0 -0.128906 -0.125
1 -0.125 -0.128906
2 -0.125 -0.128906
3 -0.121094 -0.128906
4 -0.125 -0.125
... ... ...
596 -0.125 -0.125
597 -0.125 -0.125
598 -0.128906 -0.125
599 -0.128906 -0.128906
colors red yellow
I’ve been trying to access the colors appended manually from within the for_each_trace method but been wildly unsuccessful. See my horrible attempt at trying this shown below:
figure.for_each_trace(
lambda trace: trace.update(fillcolor=df.loc['colors'][trace.uid])
)
Am I going in the right path? Any help is greatly appreciated!