-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
346 lines (278 loc) · 12 KB
/
app.py
File metadata and controls
346 lines (278 loc) · 12 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import io
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import streamlit as st
st.set_page_config(page_title="Interactive Plotter", layout="wide")
st.title("📈 Interactive Excel Plotter")
# --- File upload ---
uploaded_file = st.file_uploader("Upload an Excel file", type=["xls", "xlsx"])
if uploaded_file is not None:
# --- Sheet selection ---
try:
xlsx = pd.ExcelFile(uploaded_file)
sheet_names = xlsx.sheet_names
st.sidebar.header("Excel Sheet")
if len(sheet_names) > 1:
sheet_name = st.sidebar.selectbox("Select Sheet", sheet_names, index=0)
else:
sheet_name = sheet_names[0]
df = pd.read_excel(xlsx, sheet_name=sheet_name)
except Exception as e:
st.error(f"Error reading file: {e}")
st.stop()
# --- Data preview ---
st.subheader("Data Preview")
st.caption(f"Showing data from sheet: **{sheet_name}**")
st.dataframe(df.head())
# --- Plot type selection ---
st.sidebar.header("Plot Type")
plot_type = st.sidebar.radio(
"Select the plot type",
["Scatter Plot", "Stacked Bar Chart", "Step Line Plot"],
index=0
)
# --- Column selection ---
st.sidebar.header("Column Selection")
all_columns = df.columns.tolist()
selected_columns = st.sidebar.multiselect(
"Select columns to use",
all_columns,
default=all_columns
)
if len(selected_columns) < 1:
st.warning("Please select at least one column.")
st.stop()
numeric_cols = df[selected_columns].select_dtypes(include=["number"]).columns.tolist()
if len(numeric_cols) == 0:
st.error("No numeric columns available.")
st.stop()
# --- Shared settings ---
st.sidebar.header("Plot Settings")
if plot_type == "Scatter Plot":
if len(numeric_cols) < 2:
st.error("Scatter plot requires at least TWO numeric columns.")
st.stop()
x_col = st.sidebar.selectbox("X-axis column", numeric_cols, key="scatter_x")
y_col = st.sidebar.selectbox("Y-axis column", numeric_cols, key="scatter_y")
x_label = st.sidebar.text_input("X-axis label", value=x_col, key="scatter_xlab")
y_label = st.sidebar.text_input("Y-axis label", value=y_col, key="scatter_ylab")
title = st.sidebar.text_input("Plot title", value=f"{y_col} vs {x_col}", key="scatter_title")
elif plot_type == "Stacked Bar Chart":
x_label = st.sidebar.text_input("X-axis label", value="Row Label", key="bar_xlab")
y_label = st.sidebar.text_input("Y-axis label", value="Value", key="bar_ylab")
title = st.sidebar.text_input("Plot title", value="Stacked Bar Chart", key="bar_title")
else: # Step line plot
if len(numeric_cols) < 2:
st.error("Step line plot requires at least TWO numeric columns.")
st.stop()
st.sidebar.subheader("Step Plot Options")
sheets_to_plot = st.sidebar.multiselect(
"Sheets to include (one step line per sheet)",
options=sheet_names,
default=sheet_names,
key="step_sheets"
)
if len(sheets_to_plot) == 0:
st.warning("Please select at least one worksheet.")
st.stop()
x_col = st.sidebar.selectbox("X-axis column", numeric_cols, key="step_x")
y_col = st.sidebar.selectbox("Y-axis column", numeric_cols, key="step_y")
x_label = st.sidebar.text_input("X-axis label", value=x_col, key="step_xlab")
y_label = st.sidebar.text_input("Y-axis label", value=y_col, key="step_ylab")
title = st.sidebar.text_input("Plot title", value=f"Step: {y_col} vs {x_col}", key="step_title")
linewidth = st.sidebar.number_input("Line width", 0.5, 10.0, 2.0, 0.5, key="step_lw")
# Figure size
col1, col2 = st.sidebar.columns(2)
with col1:
fig_width = st.number_input("Figure width", 2.0, 20.0, 10.0)
with col2:
fig_height = st.number_input("Figure height", 2.0, 20.0, 6.0)
# Font size
st.sidebar.header("Font Settings")
font_size = st.sidebar.number_input("Global font size", 6, 40, 12, 1)
plt.rcParams["font.size"] = font_size
# --- Axis Scale (shared by Scatter + Step)
if plot_type in ["Scatter Plot", "Step Line Plot"]:
st.sidebar.header("Axis Scale")
x_scale = st.sidebar.selectbox("X-axis scale", ["Linear", "Log"], index=0)
y_scale = st.sidebar.selectbox("Y-axis scale", ["Linear", "Log"], index=0)
else:
x_scale = "Linear"
y_scale = "Linear"
# ============================================================
# SCATTER PLOT
# ============================================================
if plot_type == "Scatter Plot":
st.sidebar.header("Fit Options")
fit_type = st.sidebar.selectbox(
"Fit type",
["None", "Linear", "Polynomial", "Exponential"]
)
poly_degree = None
if fit_type == "Polynomial":
poly_degree = st.sidebar.number_input(
"Polynomial degree", 2, 10, 2, 1
)
x = df[x_col].values
y = df[y_col].values
fig, ax = plt.subplots(figsize=(fig_width, fig_height), dpi=300)
ax.plot(x, y, "k.", alpha=0.8)
mask = ~np.isnan(x) & ~np.isnan(y)
x_clean = x[mask]
y_clean = y[mask]
# --- Fits ---
if fit_type == "Linear" and len(x_clean) > 1:
m, b = np.polyfit(x_clean, y_clean, 1)
x_fit = np.linspace(x_clean.min(), x_clean.max(), 300)
y_fit = m * x_fit + b
ax.plot(x_fit, y_fit, "r", linewidth=2)
ax.legend([f"Linear Fit: y={m:.3g}x+{b:.3g}"])
elif fit_type == "Polynomial" and poly_degree and len(x_clean) > poly_degree:
coeffs = np.polyfit(x_clean, y_clean, poly_degree)
poly = np.poly1d(coeffs)
x_fit = np.linspace(x_clean.min(), x_clean.max(), 300)
y_fit = poly(x_fit)
ax.plot(x_fit, y_fit, "r", linewidth=2)
ax.legend([f"Poly deg {poly_degree}"])
elif fit_type == "Exponential" and len(x_clean) > 1:
pos_mask = y_clean > 0
if pos_mask.sum() > 1:
x_pos = x_clean[pos_mask]
y_pos = y_clean[pos_mask]
b, log_a = np.polyfit(x_pos, np.log(y_pos), 1)
a = np.exp(log_a)
x_fit = np.linspace(x_pos.min(), x_pos.max(), 300)
ax.plot(x_fit, a * np.exp(b * x_fit), "r", linewidth=2)
ax.legend([f"Exp Fit"])
else:
st.warning("Exponential fit requires positive y-values.")
# --- Apply labels & scaling ---
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
ax.set_title(title)
ax.grid(True, linestyle="--", alpha=0.4)
if x_scale == "Log":
ax.set_xscale("log")
if y_scale == "Log":
ax.set_yscale("log")
st.pyplot(fig)
# Export
st.subheader("Export Plot")
def get_image_bytes(fmt="png"):
buf = io.BytesIO()
fig.savefig(buf, format=fmt, dpi=300, bbox_inches="tight")
buf.seek(0)
return buf
col_png, col_eps = st.columns(2)
with col_png:
st.download_button("Download PNG", get_image_bytes("png"),
"plot.png", "image/png")
with col_eps:
st.download_button("Download EPS", get_image_bytes("eps"),
"plot.eps", "application/postscript")
# ============================================================
# STACKED BAR CHART
# ============================================================
elif plot_type == "Stacked Bar Chart":
st.subheader("Stacked Bar Chart (Each Row)")
if len(numeric_cols) < 2:
st.error("Stacked bar chart requires at least TWO numeric columns.")
st.stop()
df_bar = df[numeric_cols]
label_col = selected_columns[0]
x_labels = df[label_col].astype(str)
indices = np.arange(len(df_bar))
fig_bar, ax_bar = plt.subplots(figsize=(fig_width, fig_height), dpi=300)
cumulative = np.zeros(len(df_bar))
for col in numeric_cols:
vals = df_bar[col].fillna(0).to_numpy()
ax_bar.bar(indices, vals, bottom=cumulative, label=col)
cumulative += vals
ax_bar.set_xticks(indices)
ax_bar.set_xticklabels(x_labels, rotation=45, ha="right")
ax_bar.set_xlabel(x_label)
ax_bar.set_ylabel(y_label)
ax_bar.set_title(title)
ax_bar.grid(True, linestyle="--", alpha=0.4)
ax_bar.legend()
st.pyplot(fig_bar)
# Export
st.subheader("Export Bar Chart")
def get_bar_bytes(fmt="png"):
buf = io.BytesIO()
fig_bar.savefig(buf, format=fmt, dpi=300, bbox_inches="tight")
buf.seek(0)
return buf
col_png_bar, col_eps_bar = st.columns(2)
with col_png_bar:
st.download_button("Download PNG", get_bar_bytes("png"),
"bar_chart.png", "image/png")
with col_eps_bar:
st.download_button("Download EPS", get_bar_bytes("eps"),
"bar_chart.eps", "application/postscript")
# ============================================================
# STEP LINE PLOT
# ============================================================
else:
st.subheader("Step Line Plot (one line per worksheet)")
fig_step, ax_step = plt.subplots(figsize=(fig_width, fig_height), dpi=300)
plotted_any = False
skipped = []
for sheet in sheets_to_plot:
try:
df_s = pd.read_excel(xlsx, sheet_name=sheet)
# Numeric conversion
x_vals = pd.to_numeric(df_s[x_col], errors="coerce").to_numpy()
y_vals = pd.to_numeric(df_s[y_col], errors="coerce").to_numpy()
mask = ~np.isnan(x_vals) & ~np.isnan(y_vals)
x_clean = x_vals[mask]
y_clean = y_vals[mask]
if len(x_clean) == 0:
skipped.append(f"{sheet} (no valid data)")
continue
# Sort by X
order = np.argsort(x_clean)
x_sorted = x_clean[order]
y_sorted = y_clean[order]
ax_step.step(x_sorted, y_sorted, where="pre",
linewidth=linewidth, label=sheet)
plotted_any = True
except KeyError:
skipped.append(f"{sheet} (missing '{x_col}' or '{y_col}')")
except Exception as e:
skipped.append(f"{sheet} (error: {e})")
ax_step.set_xlabel(x_label)
ax_step.set_ylabel(y_label)
ax_step.set_title(title)
ax_step.grid(True, linestyle="--", alpha=0.4)
# Apply scaling
if x_scale == "Log":
ax_step.set_xscale("log")
if y_scale == "Log":
ax_step.set_yscale("log")
if plotted_any and len(sheets_to_plot) > 1:
ax_step.legend()
if skipped:
st.warning("Some sheets were skipped:\n- " + "\n- ".join(skipped))
if plotted_any:
st.pyplot(fig_step)
else:
st.error("No valid data to plot.")
st.stop()
# Export
st.subheader("Export Step Plot")
def get_step_bytes(fmt="png"):
buf = io.BytesIO()
fig_step.savefig(buf, format=fmt, dpi=300, bbox_inches="tight")
buf.seek(0)
return buf
col_png_s, col_eps_s = st.columns(2)
with col_png_s:
st.download_button("Download PNG", get_step_bytes("png"),
"step_plot.png", "image/png")
with col_eps_s:
st.download_button("Download EPS", get_step_bytes("eps"),
"step_plot.eps", "application/postscript")
else:
st.info("Upload an Excel file to begin.")