Spaces:
Sleeping
Sleeping
File size: 1,989 Bytes
ce38001 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
def add_stats_to_figure(fig, df, y_axis, chart_type):
# Calculate statistics
min_val = df[y_axis].min()
max_val = df[y_axis].max()
avg_val = df[y_axis].mean()
median_val = df[y_axis].median()
std_dev_val = df[y_axis].std()
# Stats summary text
stats_text = (
f"π **Statistics**\n\n"
f"- **Min:** ${min_val:,.2f}\n"
f"- **Max:** ${max_val:,.2f}\n"
f"- **Average:** ${avg_val:,.2f}\n"
f"- **Median:** ${median_val:,.2f}\n"
f"- **Std Dev:** ${std_dev_val:,.2f}"
)
# Charts suitable for stats annotations
if chart_type in ["bar", "line", "scatter"]:
# Add annotation box
fig.add_annotation(
text=stats_text,
xref="paper", yref="paper",
x=1.05, y=1,
showarrow=False,
align="left",
font=dict(size=12, color="black"),
bordercolor="black",
borderwidth=1,
bgcolor="rgba(255, 255, 255, 0.8)"
)
# Add horizontal lines for min, median, avg, max
fig.add_hline(y=min_val, line_dash="dot", line_color="red", annotation_text="Min", annotation_position="bottom right")
fig.add_hline(y=median_val, line_dash="dash", line_color="orange", annotation_text="Median", annotation_position="top right")
fig.add_hline(y=avg_val, line_dash="dashdot", line_color="green", annotation_text="Avg", annotation_position="top right")
fig.add_hline(y=max_val, line_dash="dot", line_color="blue", annotation_text="Max", annotation_position="top right")
elif chart_type == "box":
# Box plots already show distribution (no extra stats needed)
pass
elif chart_type == "pie":
# Pie charts don't need statistical overlays
st.info("π Pie charts focus on proportions. No additional stats displayed.")
else:
st.warning(f"β οΈ No stats added for unsupported chart type: {chart_type}")
return fig |