pandas-plots 0.12.4__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 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] = "gray"
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
- All parameters are similar to the original function, with the addition of:
199
- - color_palette: str - Name of the color palette.
200
- - null_label: str - Label for null values.
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
- _fig = px.bar(
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,6 +311,7 @@ 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
  )
281
316
  # * get longest bar
282
317
  bar_max = (
@@ -286,14 +321,14 @@ def plot_stacked_bars(
286
321
  # * ignore if bar mode is on
287
322
  if not relative:
288
323
  if orientation == "v":
289
- _fig.update_yaxes(range=[0, bar_max])
324
+ fig.update_yaxes(range=[0, bar_max])
290
325
  else:
291
- _fig.update_xaxes(range=[0, bar_max])
326
+ fig.update_xaxes(range=[0, bar_max])
292
327
  else:
293
- _fig.update_layout(barnorm="percent")
328
+ fig.update_layout(barnorm="percent")
294
329
 
295
330
  # * set title properties
296
- _fig.update_layout(
331
+ fig.update_layout(
297
332
  title={
298
333
  # 'x': 0.1,
299
334
  "y": 0.95,
@@ -308,27 +343,26 @@ def plot_stacked_bars(
308
343
  # * set dtick
309
344
  if orientation == "h":
310
345
  if relative:
311
- _fig.update_xaxes(dtick=5)
346
+ fig.update_xaxes(dtick=5)
312
347
  elif normalize:
313
- _fig.update_xaxes(dtick=0.05)
348
+ fig.update_xaxes(dtick=0.05)
314
349
  else:
315
350
  if relative:
316
- _fig.update_yaxes(dtick=5)
351
+ fig.update_yaxes(dtick=5)
317
352
  elif normalize:
318
- _fig.update_yaxes(dtick=0.05)
353
+ fig.update_yaxes(dtick=0.05)
319
354
 
320
355
  # * show grids, set to smaller distance on pct scale
321
- _fig.update_xaxes(showgrid=True, gridwidth=1)
322
- _fig.update_yaxes(showgrid=True, gridwidth=1)
356
+ fig.update_xaxes(showgrid=True, gridwidth=1)
357
+ fig.update_yaxes(showgrid=True, gridwidth=1)
323
358
 
324
359
  # * save to png if path is provided
325
360
  if png_path is not None:
326
- _fig.write_image(Path(png_path).as_posix())
327
-
328
- _fig.show(renderer)
361
+ fig.write_image(Path(png_path).as_posix())
329
362
 
330
- return _fig
363
+ fig.show(renderer=renderer)
331
364
 
365
+ return fig
332
366
 
333
367
 
334
368
  def plot_bars(
@@ -1189,17 +1223,17 @@ def plot_facet_stacked_bars(
1189
1223
  unique_rows = len(aggregated_df)
1190
1224
  axis_details = []
1191
1225
  if top_n_index > 0:
1192
- axis_details.append(f"top {top_n_index} [{original_column_names[0]}]")
1226
+ axis_details.append(f"TOP {top_n_index} [{original_column_names[0]}]")
1193
1227
  else:
1194
1228
  axis_details.append(f"[{original_column_names[0]}]")
1195
1229
 
1196
1230
  if top_n_columns > 0:
1197
- axis_details.append(f"top {top_n_columns} [{original_column_names[1]}]")
1231
+ axis_details.append(f"TOP {top_n_columns} [{original_column_names[1]}]")
1198
1232
  else:
1199
1233
  axis_details.append(f"[{original_column_names[1]}]")
1200
1234
 
1201
1235
  if top_n_facet > 0:
1202
- axis_details.append(f"top {top_n_facet} [{original_column_names[2]}]")
1236
+ axis_details.append(f"TOP {top_n_facet} [{original_column_names[2]}]")
1203
1237
  else:
1204
1238
  axis_details.append(f"[{original_column_names[2]}]")
1205
1239
 
@@ -1218,6 +1252,6 @@ def plot_facet_stacked_bars(
1218
1252
  png_path = Path(png_path)
1219
1253
  fig.write_image(str(png_path))
1220
1254
 
1221
- fig.show(renderer)
1255
+ fig.show(renderer=renderer)
1222
1256
 
1223
1257
  return fig
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: pandas-plots
3
- Version: 0.12.4
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>=5.18.0
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
- - 🆕 `plot_stacked_bars()` shows stacked bars for a facet value as subplots
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=isveg6_frLZC3Gt3VEsdOLiLw7aTf3riUahmJLHiEq8,40265
4
- pandas_plots/tbl.py,sha256=4VvjLisPT1gSvgsLClcrhC7LIJ-_FPNla8nomGflGag,30509
5
- pandas_plots/ven.py,sha256=2x3ACo2vSfO3q6fv-UdDQ0h1SJyt8WChBGgE5SDCdCk,11673
6
- pandas_plots-0.12.4.dist-info/LICENSE,sha256=6KQ5KVAAhRaB-JJKpX4cefKvRZRgI7GUPc92_2d31XY,1051
7
- pandas_plots-0.12.4.dist-info/METADATA,sha256=WZUfWOid_eYMtuS2V_P_C_ChaD1dTqDfuectlxzAJe8,7358
8
- pandas_plots-0.12.4.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
9
- pandas_plots-0.12.4.dist-info/top_level.txt,sha256=XnaNuIHBqMmCeh_U7nKOYTwFue_SIA0wxuDgdPmnnSk,13
10
- pandas_plots-0.12.4.dist-info/RECORD,,