There is a problem with displaying values on a pyplot chart in reverse order.
import matplotlib.pyplot as plt
import pandas as pd
merged_month_df = pd.DataFrame({
'month': ['january', 'february', 'march'],
'reg_month': [100, 80, 60],
'application': [50, 40, 30],
'game': [30, 20, 10],
'visit_site_month': [1000, 800, 600]
})
# Calculate ratios for each stage of the funnel
merged_month_df['reg_ratio'] = merged_month_df['reg_month'] / merged_month_df['visit_site_month'] * 100
merged_month_df['app_ratio'] = merged_month_df['application'] / merged_month_df['visit_site_month'] * 100
merged_month_df['game_ratio'] = merged_month_df['game'] / merged_month_df['visit_site_month'] * 100
# Create plot
fig, ax = plt.subplots(figsize=(10, 6))
# Draw bars for each stage of the funnel
ax.bar(merged_month_df['month'], merged_month_df['visit_site_month'], label='Website visits')
ax.bar(merged_month_df['month'], merged_month_df['reg_month'], label='Registrations', alpha=0.7)
ax.bar(merged_month_df['month'], merged_month_df['application'], label='Applications', alpha=0.7)
ax.bar(merged_month_df['month'], merged_month_df['game'], label='Visited game', alpha=0.7)
# Add percentage values for each stage of the funnel
for i, v in enumerate(merged_month_df['visit_site_month']):
if v > 40:
ax.text(i, v + 5, f"{round(100, 1)}%", ha='center')
else:
ax.text(i, v + 80, f"{round(100, 1)}%", ha='center')
for i, v in enumerate(merged_month_df['reg_month']):
if v > 20:
ax.text(i, v + 5, f"{round(v/merged_month_df['visit_site_month'][i]*100, 1)}%", ha='center')
else:
ax.text(i, v + 65, f"{round(v/merged_month_df['visit_site_month'][i]*100, 1)}%", ha='center')
for i, v in enumerate(merged_month_df['application']):
if v > 20:
ax.text(i, v + 15, f"{round(v/merged_month_df['reg_month'][i]*100, 1)}%", ha='center')
else:
ax.text(i, v + 40, f"{round(v/merged_month_df['reg_month'][i]*100, 1)}%", ha='center')
for i, v in enumerate(merged_month_df['game']):
if v > 20:
ax.text(i, v + 5, f"{round(v/merged_month_df['application'][i]*100, 1)}%", ha='center')
else:
ax.text(i, v + 10, f"{round(v/merged_month_df['application'][i]*100, 1)}%", ha='center')
# Set plot settings
ax.set_xticklabels(merged_month_df['month'], rotation=90)
ax.set_ylabel('Number of customers')
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
ax.grid()
plt.show()
The result of executing the code is this graph:
How to make the order as follows: Website visits - registrations - Applications - Visited Game. That is, the order is reversed