import matplotlib.pyplot as plt import numpy as np import pandas as pd # Ensures reproducibility of random numbers rng = np.random.default_rng(123) # Build a dataset # df = pd.DataFrame({ # "name": [f"item {i}" for i in range(1, 51)], # "value": rng.integers(low=30, high=100, size=50), # "group": ["A"] * 10 + ["B"] * 20 + ["C"] * 12 + ["D"] * 8 # }) unfiltered_spaces_with_outliers = pd.read_csv('hugging_face_spaces.csv') spaces = unfiltered_spaces_with_outliers[unfiltered_spaces_with_outliers['likes'] >= 3] df = spaces.sort_values('likes', ascending=False).iloc[1:51][['repo_id', 'likes', 'sdk']] df['likes'] = df['likes'] / 5 df = df.sample(frac=1) # df.to_csv('x.csv') def get_label_rotation(angle, offset): # Rotation must be specified in degrees :( rotation = np.rad2deg(angle + offset) if angle <= np.pi: alignment = "right" rotation = rotation + 180 else: alignment = "left" return rotation, alignment def add_labels(angles, values, labels, offset, ax): padding = 4 for angle, value, label, in zip(angles, values, labels): angle = angle rotation, alignment = get_label_rotation(angle, offset) ax.text(x=angle, y=value + padding, s=label, size=11, ha=alignment, va="center", rotation=rotation, rotation_mode="anchor") def get_plot(): ANGLES = np.linspace(0, 2 * np.pi, len(df), endpoint=False) VALUES = df["likes"].values LABELS = df["repo_id"].values # Determine the width of each bar. # The circumference is '2 * pi', so we divide that total width over the number of bars. WIDTH = 2 * np.pi / len(VALUES) # Determines where to place the first bar. # By default, matplotlib starts at 0 (the first bar is horizontal) # but here we say we want to start at pi/2 (90 deg) OFFSET = np.pi / 2 # Initialize Figure and Axis fig, ax = plt.subplots(figsize=(20, 22), subplot_kw={"projection": "polar"}) # Specify offset ax.set_theta_offset(OFFSET) # Set limits for radial (y) axis. The negative lower bound creates the whole in the middle. ax.set_ylim(-100, df['likes'].max()) # Remove all spines ax.set_frame_on(False) # Remove grid and tick marks ax.xaxis.grid(False) ax.yaxis.grid(False) ax.set_xticks([]) ax.set_yticks([]) # Add bars ax.bar( ANGLES, VALUES, width=WIDTH, linewidth=2, color="#61a4b2", edgecolor="white" ) # Add labels add_labels(ANGLES, VALUES, LABELS, OFFSET, ax) return fig