Similar to seaborn's hue function in plotly

Hello everyone, newbie here.

I asked a stupid question in Stackflow and got no answers so I hope I can get the answers here.
https://stackoverflow.com/questions/52047884/similar-to-seaborns-hue-function-in-plotly

I found most tutorials of plotly is about typing individual traces for each data which is time exhausting and not necessary. I know there must be a solution to simplify this problem.

Hi @Zakk,

No, there’s nothing directly analogous to hue in plotly.py. This is something you could accomplish in ~3 lines with a pandas groupby though. If you’re interested in that approach please post some sample data and I can show you what that would look like.

-Jon

Hi, thank you for your support. Here is my one drive link for this dataset.
https://1drv.ms/u/s!AqCVxdu-hfBSgakprttgWHDVQTtprw

Hi @Zakk,

Here’s that pandas groupby approach I was referring to:

for region, geo_region in geo.groupby('Geographical region'):
    fig.add_scatter(x=geo_region.Year, y=geo_region.Number, name=region, mode='lines')

And here’s the complete code block:

import plotly.graph_objs as go
import pandas as pd
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode()

geo = pd.read_csv('my_output')

fig = go.Figure()
for region, geo_region in geo.groupby('Geographical region'):
    fig.add_scatter(x=geo_region.Year, y=geo_region.Number, name=region, mode='lines')

iplot(fig)

Note that in the live version the legend has a scroll bar, that’s why not all of the regions show up in this static image. If you need all of the regions to show up statically, then just increase the layout.height property of the figure.

Hope that helps!
-Jon

Hi Jon,

Thank you so much for your reply but still, some error pop up:

Hi @Zakk, I forgot to ask what version of plotly.py you’re using. My answer above is for version 3 only. Here’s a version that’s compatible with both versions 2 and 3.

traces = []
for region, geo_region in geo.groupby('Geographical region'):
    traces.append(go.Scatter(x=geo_region.Year, y=geo_region.Number, name=region, mode='lines'))

fig = go.Figure(data=traces)

-Jon

Problem solved!! To me, the old version looks better. And I have to admit that I should understand more about the for loop. Thank you again, Jon.

1 Like

Hi @jmmease ,
Thanks for your solution. How can I use this for subplots? For instances, I have traces_1, traces_2.

traces = []
for region, geo_region in geo.groupby('Geographical region'):
    traces.append(go.Scatter(x=geo_region.Year, y=geo_region.Number, name=region, mode='lines'))

fig = go.Figure(data=traces)

I am trying to add like

fig = tools.make_subplots(rows=1, cols=2)

fig.append_trace(traces_1, 1, 1)
fig.append_trace(traces_2, 1, 2)

but it is not working because traces_1 is a list of traces. Is there anyway to make it work? Thanks.

Hi @thoo,

I think you should be able to loop over your traces_1 and traces_2 lists.

fig = tools.make_subplots(rows=1, cols=2)

for trace in traces_1:
    fig.append_trace(traces, 1, 1)

for trace in traces_2:
    fig.append_trace(traces, 1, 2)

Or, you could specify the lists to add_traces and pass the row/col indices as lists as well.

fig.add_traces(traces_1, rows=[1]*len(traces_1), cols=[1]*len(traces_1)
fig.add_traces(traces_2, rows=[1]*len(traces_2), cols=[2]*len(traces_2)

Hope that helps!
-Jon

Hi @jmmease,
It works!!! Thanks for your help.

1 Like

Would you please send the sample data used in this post, (it was removed from the one drive link provided) Thank you

Would you please send the sample data used in this post, (it was removed from the one drive link provided) Thank you

A post was split to a new topic: Help with a grouped bar chart