pandas-plots 0.12.3__py3-none-any.whl → 0.12.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pandas_plots/pls.py +88 -16
- {pandas_plots-0.12.3.dist-info → pandas_plots-0.12.5.dist-info}/METADATA +3 -3
- pandas_plots-0.12.5.dist-info/RECORD +10 -0
- pandas_plots-0.12.3.dist-info/RECORD +0 -10
- {pandas_plots-0.12.3.dist-info → pandas_plots-0.12.5.dist-info}/LICENSE +0 -0
- {pandas_plots-0.12.3.dist-info → pandas_plots-0.12.5.dist-info}/WHEEL +0 -0
- {pandas_plots-0.12.3.dist-info → pandas_plots-0.12.5.dist-info}/top_level.txt +0 -0
pandas_plots/pls.py
CHANGED
@@ -78,7 +78,7 @@ def assign_column_colors(columns, color_palette, null_label):
|
|
78
78
|
raise ValueError(f"Invalid color palette: {color_palette}")
|
79
79
|
|
80
80
|
colors = {col: palette[i % len(palette)] for i, col in enumerate(sorted(columns))}
|
81
|
-
colors[null_label] = "
|
81
|
+
colors[null_label] = "lightgray"
|
82
82
|
return colors
|
83
83
|
|
84
84
|
### main functions
|
@@ -192,12 +192,32 @@ def plot_stacked_bars(
|
|
192
192
|
) -> object:
|
193
193
|
"""
|
194
194
|
Generates a stacked bar plot using the provided DataFrame.
|
195
|
-
Updated to assign colors using `assign_column_colors` with nulls colored grey.
|
196
195
|
|
197
196
|
Parameters:
|
198
|
-
|
199
|
-
-
|
200
|
-
-
|
197
|
+
- df (pd.DataFrame): The input DataFrame with at least two categorical columns and one numerical column.
|
198
|
+
- top_n_index (int): Limit the number of categories displayed on the index axis.
|
199
|
+
- top_n_color (int): Limit the number of categories displayed in the color legend.
|
200
|
+
- dropna (bool): If True, removes rows with missing values; otherwise, replaces them with `null_label`.
|
201
|
+
- swap (bool): If True, swaps the first two columns.
|
202
|
+
- normalize (bool): If True, normalizes numerical values between 0 and 1.
|
203
|
+
- relative (bool): If True, normalizes the bars to a percentage scale.
|
204
|
+
- orientation (Literal["h", "v"]): Defines the orientation of the bars ("v" for vertical, "h" for horizontal).
|
205
|
+
- height (int): Height of the plot.
|
206
|
+
- width (int): Width of the plot.
|
207
|
+
- title (str): Custom title for the plot.
|
208
|
+
- renderer (Literal["png", "svg", None]): Defines the output format.
|
209
|
+
- caption (str): Optional caption for additional context.
|
210
|
+
- sort_values (bool):
|
211
|
+
- If True, sorts bars by the sum of their values (descending).
|
212
|
+
- If False, sorts bars alphabetically.
|
213
|
+
- show_total (bool): If True, adds a row with the total sum of all categories.
|
214
|
+
- precision (int): Number of decimal places for numerical values.
|
215
|
+
- png_path (Path | str): If specified, saves the plot as a PNG file.
|
216
|
+
- color_palette (str): Name of the color palette to use.
|
217
|
+
- null_label (str): Label for null values.
|
218
|
+
|
219
|
+
Returns:
|
220
|
+
- A Plotly figure object representing the stacked bar chart.
|
201
221
|
"""
|
202
222
|
BAR_LENGTH_MULTIPLIER = 1.05
|
203
223
|
|
@@ -211,6 +231,8 @@ def plot_stacked_bars(
|
|
211
231
|
print("❌ first 2 columns must be str")
|
212
232
|
return
|
213
233
|
|
234
|
+
df = df.copy() # Copy the input DataFrame to avoid modifying the original
|
235
|
+
|
214
236
|
# * add count column[2] as a service if none is present
|
215
237
|
if len(df.columns) == 2:
|
216
238
|
df["cnt"] = 1
|
@@ -251,6 +273,18 @@ def plot_stacked_bars(
|
|
251
273
|
.sum()
|
252
274
|
.reset_index()
|
253
275
|
)
|
276
|
+
|
277
|
+
# * Sorting logic based on sort_values
|
278
|
+
if sort_values:
|
279
|
+
sort_order = (
|
280
|
+
df.groupby(col_index)[df.columns[2]].sum().sort_values(ascending=False).index
|
281
|
+
)
|
282
|
+
else:
|
283
|
+
sort_order = sorted(df[col_index].unique()) # Alphabetical order
|
284
|
+
|
285
|
+
# * Convert to categorical with explicit ordering
|
286
|
+
df[col_index] = pd.Categorical(df[col_index], categories=sort_order, ordered=True)
|
287
|
+
|
254
288
|
|
255
289
|
# * calculate n
|
256
290
|
divider = 2 if show_total else 1
|
@@ -264,7 +298,7 @@ def plot_stacked_bars(
|
|
264
298
|
caption = _set_caption(caption)
|
265
299
|
|
266
300
|
# * plot
|
267
|
-
|
301
|
+
fig = px.bar(
|
268
302
|
df,
|
269
303
|
x=col_index if orientation == "v" else df.columns[2],
|
270
304
|
y=df.columns[2] if orientation == "v" else col_index,
|
@@ -277,20 +311,58 @@ def plot_stacked_bars(
|
|
277
311
|
width=width,
|
278
312
|
height=height,
|
279
313
|
color_discrete_map=column_colors, # Use assigned colors
|
314
|
+
category_orders={col_index: list(df[col_index].cat.categories)}, # <- Add this line
|
280
315
|
)
|
316
|
+
# * get longest bar
|
317
|
+
bar_max = (
|
318
|
+
df.groupby(col_index)[df.columns[2]].sum().sort_values(ascending=False).iloc[0]
|
319
|
+
* BAR_LENGTH_MULTIPLIER
|
320
|
+
)
|
321
|
+
# * ignore if bar mode is on
|
322
|
+
if not relative:
|
323
|
+
if orientation == "v":
|
324
|
+
fig.update_yaxes(range=[0, bar_max])
|
325
|
+
else:
|
326
|
+
fig.update_xaxes(range=[0, bar_max])
|
327
|
+
else:
|
328
|
+
fig.update_layout(barnorm="percent")
|
329
|
+
|
330
|
+
# * set title properties
|
331
|
+
fig.update_layout(
|
332
|
+
title={
|
333
|
+
# 'x': 0.1,
|
334
|
+
"y": 0.95,
|
335
|
+
"xanchor": "left",
|
336
|
+
"yanchor": "top",
|
337
|
+
"font": {
|
338
|
+
"size": 24,
|
339
|
+
},
|
340
|
+
},
|
341
|
+
)
|
342
|
+
|
343
|
+
# * set dtick
|
344
|
+
if orientation == "h":
|
345
|
+
if relative:
|
346
|
+
fig.update_xaxes(dtick=5)
|
347
|
+
elif normalize:
|
348
|
+
fig.update_xaxes(dtick=0.05)
|
349
|
+
else:
|
350
|
+
if relative:
|
351
|
+
fig.update_yaxes(dtick=5)
|
352
|
+
elif normalize:
|
353
|
+
fig.update_yaxes(dtick=0.05)
|
281
354
|
|
282
355
|
# * show grids, set to smaller distance on pct scale
|
283
|
-
|
284
|
-
|
356
|
+
fig.update_xaxes(showgrid=True, gridwidth=1)
|
357
|
+
fig.update_yaxes(showgrid=True, gridwidth=1)
|
285
358
|
|
286
359
|
# * save to png if path is provided
|
287
360
|
if png_path is not None:
|
288
|
-
|
289
|
-
|
290
|
-
_fig.show(renderer)
|
361
|
+
fig.write_image(Path(png_path).as_posix())
|
291
362
|
|
292
|
-
|
363
|
+
fig.show(renderer=renderer)
|
293
364
|
|
365
|
+
return fig
|
294
366
|
|
295
367
|
|
296
368
|
def plot_bars(
|
@@ -1151,17 +1223,17 @@ def plot_facet_stacked_bars(
|
|
1151
1223
|
unique_rows = len(aggregated_df)
|
1152
1224
|
axis_details = []
|
1153
1225
|
if top_n_index > 0:
|
1154
|
-
axis_details.append(f"
|
1226
|
+
axis_details.append(f"TOP {top_n_index} [{original_column_names[0]}]")
|
1155
1227
|
else:
|
1156
1228
|
axis_details.append(f"[{original_column_names[0]}]")
|
1157
1229
|
|
1158
1230
|
if top_n_columns > 0:
|
1159
|
-
axis_details.append(f"
|
1231
|
+
axis_details.append(f"TOP {top_n_columns} [{original_column_names[1]}]")
|
1160
1232
|
else:
|
1161
1233
|
axis_details.append(f"[{original_column_names[1]}]")
|
1162
1234
|
|
1163
1235
|
if top_n_facet > 0:
|
1164
|
-
axis_details.append(f"
|
1236
|
+
axis_details.append(f"TOP {top_n_facet} [{original_column_names[2]}]")
|
1165
1237
|
else:
|
1166
1238
|
axis_details.append(f"[{original_column_names[2]}]")
|
1167
1239
|
|
@@ -1180,6 +1252,6 @@ def plot_facet_stacked_bars(
|
|
1180
1252
|
png_path = Path(png_path)
|
1181
1253
|
fig.write_image(str(png_path))
|
1182
1254
|
|
1183
|
-
fig.show(renderer)
|
1255
|
+
fig.show(renderer=renderer)
|
1184
1256
|
|
1185
1257
|
return fig
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: pandas-plots
|
3
|
-
Version: 0.12.
|
3
|
+
Version: 0.12.5
|
4
4
|
Summary: A collection of helper for table handling and visualization
|
5
5
|
Home-page: https://github.com/smeisegeier/pandas-plots
|
6
6
|
Author: smeisegeier
|
@@ -20,7 +20,7 @@ Requires-Python: >=3.10
|
|
20
20
|
Description-Content-Type: text/markdown
|
21
21
|
License-File: LICENSE
|
22
22
|
Requires-Dist: pandas>=2.0.0
|
23
|
-
Requires-Dist: plotly
|
23
|
+
Requires-Dist: plotly<6
|
24
24
|
Requires-Dist: matplotlib>=3.8.2
|
25
25
|
Requires-Dist: matplotlib-venn==0.11.10
|
26
26
|
Requires-Dist: seaborn>=0.13.2
|
@@ -96,7 +96,7 @@ tbl.show_num_df(
|
|
96
96
|
- `plot_histogram()` histogram for one or more **numerical** columns
|
97
97
|
- `plot_joints()` a joint plot for **exactly two numerical** columns
|
98
98
|
- `plot_quadrants()` quickly shows a 2x2 heatmap
|
99
|
-
- 🆕 `
|
99
|
+
- 🆕 `plot_facet_stacked_bars()` shows stacked bars for a facet value as subplots
|
100
100
|
<br>
|
101
101
|
|
102
102
|
- `ven` offers functions for _venn diagrams_
|
@@ -0,0 +1,10 @@
|
|
1
|
+
pandas_plots/hlp.py,sha256=N6NrbFagVMMX-ZnV0rIBEz82SeSoOkksfMcCap55W7E,16588
|
2
|
+
pandas_plots/pii.py,sha256=2WKE-W9s285jPdsTqCgt1uxuW4lj1PYCVOYB2fYDNwQ,2195
|
3
|
+
pandas_plots/pls.py,sha256=L10KeIvN1sNWtC6wK5IehinIQzcDVDrgx3DHxTy3cnU,42136
|
4
|
+
pandas_plots/tbl.py,sha256=4VvjLisPT1gSvgsLClcrhC7LIJ-_FPNla8nomGflGag,30509
|
5
|
+
pandas_plots/ven.py,sha256=2x3ACo2vSfO3q6fv-UdDQ0h1SJyt8WChBGgE5SDCdCk,11673
|
6
|
+
pandas_plots-0.12.5.dist-info/LICENSE,sha256=6KQ5KVAAhRaB-JJKpX4cefKvRZRgI7GUPc92_2d31XY,1051
|
7
|
+
pandas_plots-0.12.5.dist-info/METADATA,sha256=_rqHgKQ3vHsRFb57i0i5ZYG3SugslGUeVudP0pLtl2w,7358
|
8
|
+
pandas_plots-0.12.5.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
9
|
+
pandas_plots-0.12.5.dist-info/top_level.txt,sha256=XnaNuIHBqMmCeh_U7nKOYTwFue_SIA0wxuDgdPmnnSk,13
|
10
|
+
pandas_plots-0.12.5.dist-info/RECORD,,
|
@@ -1,10 +0,0 @@
|
|
1
|
-
pandas_plots/hlp.py,sha256=N6NrbFagVMMX-ZnV0rIBEz82SeSoOkksfMcCap55W7E,16588
|
2
|
-
pandas_plots/pii.py,sha256=2WKE-W9s285jPdsTqCgt1uxuW4lj1PYCVOYB2fYDNwQ,2195
|
3
|
-
pandas_plots/pls.py,sha256=nNpAPIxONcoUb_E6U6oGEEjva6MFgycc3pBq0fb1TQ0,39268
|
4
|
-
pandas_plots/tbl.py,sha256=4VvjLisPT1gSvgsLClcrhC7LIJ-_FPNla8nomGflGag,30509
|
5
|
-
pandas_plots/ven.py,sha256=2x3ACo2vSfO3q6fv-UdDQ0h1SJyt8WChBGgE5SDCdCk,11673
|
6
|
-
pandas_plots-0.12.3.dist-info/LICENSE,sha256=6KQ5KVAAhRaB-JJKpX4cefKvRZRgI7GUPc92_2d31XY,1051
|
7
|
-
pandas_plots-0.12.3.dist-info/METADATA,sha256=B_W5POVonGswuIrADMZGWiKNVuwWkXBKQN9CQj8-wP8,7358
|
8
|
-
pandas_plots-0.12.3.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
9
|
-
pandas_plots-0.12.3.dist-info/top_level.txt,sha256=XnaNuIHBqMmCeh_U7nKOYTwFue_SIA0wxuDgdPmnnSk,13
|
10
|
-
pandas_plots-0.12.3.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|