Skip to content

Instantly share code, notes, and snippets.

@rayheberer
Created July 29, 2020 18:39
Show Gist options
  • Save rayheberer/9815b4dd99e675d906c83d5a356da23d to your computer and use it in GitHub Desktop.
Save rayheberer/9815b4dd99e675d906c83d5a356da23d to your computer and use it in GitHub Desktop.
import plotly.graph_objects as go
import plotly.express as px
def make_barpolar(vals, labels=None, colors=None, layout_options = None, **fig_kwargs):
# infer slice angles
num_slices = len(vals)
theta = [(i + 1.5) * 360 / num_slices for i in range(num_slices)]
width = [360 / num_slices for _ in range(num_slices)]
# optionally infer colors
if colors is None:
color_seq = px.colors.qualitative.Safe
color_indices = range(0, len(color_seq), len(color_seq) // num_slices)
colors = [color_seq[i] for i in color_indices]
if layout_options is None:
layout_options = {}
if labels is None:
labels = ["" for _ in range(num_slices)]
layout_options["showlegend"] = False
# make figure
barpolar_plots = [go.Barpolar(r=[r], theta=[t], width=[w], name=n, marker_color=[c], **fig_kwargs)
for r, t, w, n, c in zip(vals, theta, width, labels, colors)]
fig = go.Figure(barpolar_plots)
# align background
angular_tickvals = [(i + 1) * 360 / num_slices for i in range(num_slices)]
fig.update_layout(polar_angularaxis_tickvals=angular_tickvals)
# additional layout parameters
fig.update_layout(**layout_options)
return fig
layout_options = {"title": "My Stats",
"title_font_size": 24,
"title_x": 0.5,
"legend_x": 0.85,
"legend_y": 0.5,
"polar_radialaxis_ticks": "",
"polar_radialaxis_showticklabels": False,
"polar_radialaxis_range": [0, max(vals)],
"polar_angularaxis_ticks": "",
"polar_angularaxis_showticklabels": False}
fig = make_barpolar(vals, labels, layout_options=layout_options, opacity = 0.7)
fig.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment