Skip to content

Instantly share code, notes, and snippets.

@Thiagobc23
Created November 3, 2021 01:53
Show Gist options
  • Save Thiagobc23/9c1051bfee9160efc9b2f44ea59446a0 to your computer and use it in GitHub Desktop.
Save Thiagobc23/9c1051bfee9160efc9b2f44ea59446a0 to your computer and use it in GitHub Desktop.
Circular progress bar with Matplotlib
from math import pi
import numpy as np
from matplotlib.patches import Patch
from matplotlib.lines import Line2D
# data and variables
data = [82, 75, 91]
startangle = 90
colors = ['#4393E5', '#43BAE5', '#7AE6EA']
# convert data to fit the polar axis
xs = [(i * pi *2)/ 100 for i in data]
# distance from the center (y limits are adjusted to -4 to 4)
ys = [-0.2, 1, 2.2]
# add blank space so the bar start at 90 degrees
left = (startangle * pi *2)/ 360 #this is to control where the bar starts
# figure n polar axis
fig, ax = plt.subplots(figsize=(6, 6))
ax = plt.subplot(projection='polar')
# plot bars and points at the end to make them round
for i, x in enumerate(xs):
ax.barh(ys[i], x, left=left, height=1, color=colors[i])
ax.scatter(x+left, ys[i], s=350, color=colors[i], zorder=2)
ax.scatter(left, ys[i], s=350, color=colors[i], zorder=2)
plt.ylim(-4, 4)
# custom legend
legend_elements = [Line2D([0], [0], marker='o', color='w', label='Group A', markerfacecolor='#4393E5', markersize=10),
Line2D([0], [0], marker='o', color='w', label='Group B', markerfacecolor='#43BAE5', markersize=10),
Line2D([0], [0], marker='o', color='w', label='Group C', markerfacecolor='#7AE6EA', markersize=10)]
ax.legend(handles=legend_elements, loc='center', frameon=False)
# clear ticks and spines
plt.xticks([])
plt.yticks([])
ax.spines.clear()
plt.savefig('circular_progress_bar.png')
@Thiagobc23
Copy link
Author

circular_progress_bar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment