Hi all!
I’m trying to create a ridgeline plot and histograms stacked line by line to visualise distribution over time of multiple categories.
I can plot the ridgeline as I’d like to as below code except the legend. How can I get the legend for Zone A and Zone B only? I tried without ‘name’ input, but then I end up another half violin plot for every single trace.
random_data = {'meeting_num':np.random.randint(1, 4, 100), 'account_age':np.round(np.random.uniform(0, 10, 100), 2), 'zone':np.random.choice(['a','b'], 100)}
random_df = pd.DataFrame(random_data)
fig = go.Figure()
for meeting in [1,2,3]:
zone_a = random_df[(random_df['meeting_num']==meeting)&(random_df['zone']=='a')]['account_age']
zone_b = random_df[(random_df['meeting_num']==meeting)&(random_df['zone']=='b')]['account_age']
fig.add_trace(go.Violin(x=zone_a, line_color='blue', name=f'Meeting {meeting}'))
fig.add_trace(go.Violin(x=zone_b, line_color='orange', name=f'Meeting {meeting}'))
fig.update_traces(orientation='h', side='positive', width=2, points=False)
fig.update_layout(xaxis_showgrid=False, xaxis_zeroline=False, xaxis_title='Account Age', xaxis = dict(tickmode='linear', tick0=0, dtick=1), showlegend=True, width=1000, height=600, violinmode='overlay')
fig.show()
I also want to create histograms just like above but instead of the half violin plots. Trying the same way as the ridgeline, I end up with all traces overlayed on top of each other with a similar legend issue. How could I get histograms for both Zones in each meeting plotted each row?
fig = go.Figure()
for meeting in [1,2,3]:
zone_a = random_df[(random_df['meeting_num']==meeting)&(random_df['zone']=='a')]['account_age']
zone_b = random_df[(random_df['meeting_num']==meeting)&(random_df['zone']=='b')]['account_age']
fig.add_trace(go.Histogram(x=zone_a, histnorm='probability', name=f'Zone A, Meeting Number: {meeting}'))
fig.add_trace(go.Histogram(x=zone_b, histnorm='probability', name=f'Zone B, Meeting Number: {meeting}'))
fig.update_layout(barmode='overlay', xaxis_showgrid=False, xaxis_zeroline=False, width=1000, height=600)
fig.update_traces(opacity=.5)
fig.show()
Been reading and trying a lot but couldn’t solve this. Could you please help me understand how I can achieve these?
Thank you!