Created
October 30, 2019 20:04
-
-
Save JoaoCarabetta/28ecd10e8658093815c75613ad8b00d1 to your computer and use it in GitHub Desktop.
Subplot with cufflinks with unique legend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import plotly | |
import plotly.graph_objs as go | |
import cufflinks as cf | |
plotly.offline.init_notebook_mode(connected=True) | |
cf.go_offline() | |
cf.set_config_file(offline=False, world_readable=True) | |
# Add figures | |
figs = [df.query(f"group == '{group}'").figure() | |
for i, group in enumerate(groups)] | |
# Add legend group to same traces | |
for i in range(len(groups)): | |
for j in range(len(figs[i]['data'])): | |
figs[i]['data'][j]['legendgroup'] = str(j) | |
figs[i]['data'][j]['showlegend'] = i == 0 | |
# Instantiate figure as subplot | |
figure = cf.subplots(figs, shape=(len(groups), 1), | |
subplot_titles=[ 'Roadtype ' + roadtype_dict[int(group)] + f' (Rolling Window {rolling[i]})' | |
for i, group in enumerate(groups)], | |
vertical_spacing=0.03) | |
# Adjust height and add overall title | |
figure['layout']['height'] = len(groups) * 250 | |
figure['layout']['title'] = 'Accuracy x Sample Size by Level (Weighted)' | |
# Edits layout of each subplot | |
for i in range(1, len(groups) + 1): | |
figure['layout'][f'yaxis{i}']['title'] = 'Accuracy' | |
figure['layout'][f'yaxis{i}']['range'] = [0.4, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment