Beeswarm Plot
Cafe Revenue by Time Slot
Transaction amounts throughout the day
Output
Python
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(313)
BG_COLOR = '#ffffff'
TEXT_COLOR = '#1f2937'
COLORS = ['#F5B027', '#F5276C', '#27D3F5', '#4927F5']
def simple_beeswarm(y, nbins=None, width=1.):
y = np.asarray(y)
if nbins is None:
nbins = np.ceil(len(y) / 6).astype(int)
nn, ybins = np.histogram(y, bins=nbins)
nmax = nn.max()
x = np.zeros(len(y))
ibs = []
for ymin, ymax in zip(ybins[:-1], ybins[1:]):
i = np.nonzero((y > ymin) * (y <= ymax))[0]
ibs.append(i)
dx = width / (nmax // 2) if nmax > 1 else width
for i in ibs:
yy = y[i]
if len(i) > 1:
j = len(i) % 2
i = i[np.argsort(yy)]
a = i[j::2]
b = i[j+1::2]
x[a] = (0.5 + j / 3 + np.arange(len(b))) * dx
x[b] = (0.5 + j / 3 + np.arange(len(b))) * -dx
return x
periods = ['Morning', 'Lunch', 'Afternoon', 'Evening']
data = {
'Morning': np.random.lognormal(1.5, 0.4, 70),
'Lunch': np.random.lognormal(1.8, 0.5, 50),
'Afternoon': np.random.lognormal(1.4, 0.35, 45),
'Evening': np.random.lognormal(1.7, 0.45, 35)
}
fig, ax = plt.subplots(figsize=(10, 6), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)
boxplot_data = []
for i, (period, values) in enumerate(data.items()):
x = simple_beeswarm(values, width=0.3)
ax.scatter(x + i + 1, values, c=COLORS[i], alpha=0.7, s=45, edgecolors='white', linewidth=0.5)
boxplot_data.append(values)
bp = ax.boxplot(boxplot_data, positions=range(1, len(periods)+1), widths=0.5, patch_artist=True)
for patch in bp['boxes']:
patch.set_facecolor('none')
patch.set_edgecolor('#9ca3af')
for element in ['whiskers', 'caps', 'medians']:
for item in bp[element]:
item.set_color('#9ca3af')
ax.set_xticks(range(1, len(periods)+1))
ax.set_xticklabels(periods, color=TEXT_COLOR)
ax.set_xlabel('Time Slot', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Transaction ($)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('Cafe Revenue by Time Slot', fontsize=14, color=TEXT_COLOR, fontweight='bold', pad=15)
ax.tick_params(colors='#374151', labelsize=10)
for spine in ax.spines.values():
spine.set_color('#e5e7eb')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
☕