Skip to content

Instantly share code, notes, and snippets.

@naiborhujosua
Created July 26, 2022 03:35
Show Gist options
  • Save naiborhujosua/4553f4d9c7a942c73e699332716fae7a to your computer and use it in GitHub Desktop.
Save naiborhujosua/4553f4d9c7a942c73e699332716fae7a to your computer and use it in GitHub Desktop.
Encircle Plot
# As many colors as there are unique midwest['category']
categories = np.unique(data['Seasons'])
colors = [plt.cm.tab10(i/float(len(categories)-1)) for i in range(len(categories))]
# Step 2: Draw Scatterplot with unique color for each category
fig = plt.figure(figsize=(16, 10), dpi= 80, facecolor='w', edgecolor='k')
for i, category in enumerate(categories):
plt.scatter('Temperature(°C)', 'Rented Bike Count', data=data.loc[data.Seasons==category, :], s=20, c=colors[i], label=str(category), edgecolors='black', linewidths=.5)
# Step 3: Encircling
# https://stackoverflow.com/questions/44575681/how-do-i-encircle-different-data-sets-in-scatter-plot
def encircle(x,y, ax=None, **kw):
if not ax: ax=plt.gca()
p = np.c_[x,y]
hull = ConvexHull(p)
poly = plt.Polygon(p[hull.vertices,:], **kw)
ax.add_patch(poly)
# Select data to be encircled
data_encircle = data.loc[data.Seasons=='Summer', :]
# Draw polygon surrounding vertices
encircle(data_encircle["Temperature(°C)"], data_encircle['Rented Bike Count'], ec="k", fc="gold", alpha=0.1)
encircle(data_encircle["Temperature(°C)"], data_encircle['Rented Bike Count'], ec="firebrick", fc="none", linewidth=1.5)
# Step 4: Decorations
plt.gca().set(xlim=(np.min(data['Temperature(°C)']), np.max(data['Temperature(°C)'])), ylim=(np.min(data["Rented Bike Count"]), np.max(data["Rented Bike Count"])),
xlabel='Temperature(°C) ', ylabel='Rented Bike Count')
plt.xticks(fontsize=12);
plt.yticks(fontsize=12)
plt.title("Bubble Plot with Encircling", fontsize=22)
plt.legend(fontsize=12)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment