pandas-plots 0.15.0__py3-none-any.whl → 0.15.1__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
@@ -244,7 +244,9 @@ def plot_stacked_bars(
244
244
  color_palette: str = "Plotly",
245
245
  null_label: str = "<NA>",
246
246
  show_other: bool = False,
247
- ) -> plotly.graph_objects:
247
+ show_pct_all: bool = False,
248
+ show_pct_bar: bool = False,
249
+ ) -> None:
248
250
  """
249
251
  Generates a stacked bar plot using the provided DataFrame.
250
252
 
@@ -273,9 +275,10 @@ def plot_stacked_bars(
273
275
  - show_other (bool): If True, shows the "Other" category in the legend.
274
276
  - sort_values_index (bool): If True, sorts the index categories by group sum
275
277
  - sort_values_color (bool): If True, sorts the columns categories by group sum
278
+ - show_pct_all (bool): If True, formats the bar text with percentages from the total n.
279
+ - show_pct_bar (bool): If True, formats the bar text with percentages from the bar's total.
276
280
 
277
- Returns:
278
- - A Plotly figure object representing the stacked bar chart.
281
+ Returns: None
279
282
  """
280
283
  BAR_LENGTH_MULTIPLIER = 1.05
281
284
 
@@ -366,18 +369,32 @@ def plot_stacked_bars(
366
369
 
367
370
  df = aggregated_df.copy()
368
371
 
372
+ # * calculate bar totals
373
+ bar_totals = df.groupby("index")["value"].transform("sum")
374
+
369
375
  caption = _set_caption(caption)
370
376
 
371
377
  # * after grouping add cols for pct and formatting
372
- df["cnt_pct_only"] = df["value"].apply(lambda x: f"{(x / n) * 100:.{precision}f}%")
378
+ df["cnt_pct_all_only"] = df["value"].apply(lambda x: f"{(x / n) * 100:.{precision}f}%")
379
+ df["cnt_pct_bar_only"] = (df["value"] / bar_totals * 100).apply(lambda x: f"{x:.{precision}f}%")
373
380
 
374
381
  # * format output
375
382
  df["cnt_str"] = df["value"].apply(lambda x: f"{x:_.{precision}f}")
376
383
 
377
384
  divider2 = "<br>" if orientation == "v" else " "
378
- df["cnt_pct_str"] = df.apply(
379
- lambda row: f"{row['cnt_str']}{divider2}({row['cnt_pct_only']})", axis=1
385
+
386
+ df["cnt_pct_all_str"] = df.apply(
387
+ lambda row: f"{row['cnt_str']}{divider2}({row['cnt_pct_all_only']})", axis=1
380
388
  )
389
+ df["cnt_pct_bar_str"] = df.apply(
390
+ lambda row: f"{row['cnt_str']}{divider2}({row['cnt_pct_bar_only']})", axis=1
391
+ )
392
+
393
+ text_to_show = "cnt_str"
394
+ if show_pct_all:
395
+ text_to_show = "cnt_pct_all_str"
396
+ elif show_pct_bar:
397
+ text_to_show = "cnt_pct_bar_str"
381
398
 
382
399
  if sort_values_color:
383
400
  colors_unique = (
@@ -418,7 +435,7 @@ def plot_stacked_bars(
418
435
  y="value" if orientation == "v" else "index",
419
436
  # color=columns,
420
437
  color="col",
421
- text="cnt_pct_str" if normalize else "cnt_str",
438
+ text=text_to_show,
422
439
  orientation=orientation,
423
440
  title=title
424
441
  or f"{caption}{_title_str_top_index}[{col_index}] by {_title_str_top_color}[{col_color}]{_title_str_null}{_title_str_n}",
@@ -487,8 +504,7 @@ def plot_stacked_bars(
487
504
  height=height,
488
505
  )
489
506
 
490
- return fig
491
-
507
+ return
492
508
 
493
509
  def plot_bars(
494
510
  df_in: pd.Series | pd.DataFrame,
@@ -507,7 +523,7 @@ def plot_bars(
507
523
  precision: int = 0,
508
524
  renderer: Literal["png", "svg", None] = "png",
509
525
  png_path: Path | str = None,
510
- ) -> object:
526
+ ) -> None:
511
527
  """
512
528
  A function to plot a bar chart based on a *categorical* column (must be string or bool) and a numerical value.
513
529
  Accepts:
@@ -535,8 +551,7 @@ def plot_bars(
535
551
  - renderer: A string indicating the renderer to use for displaying the chart. It can be "png", "svg", or None. Default is "png".
536
552
  - png_path (Path | str, optional): The path to save the image as a png file. Defaults to None.
537
553
 
538
- Returns:
539
- - plot object
554
+ Returns: None
540
555
  """
541
556
  # * if series, apply value_counts, deselect use_ci
542
557
  if isinstance(df_in, pd.Series):
@@ -766,7 +781,7 @@ def plot_bars(
766
781
  if png_path is not None:
767
782
  _fig.write_image(Path(png_path).as_posix())
768
783
 
769
- return _fig
784
+ return
770
785
 
771
786
 
772
787
  def plot_histogram(
@@ -785,7 +800,7 @@ def plot_histogram(
785
800
  caption: str = None,
786
801
  title: str = None,
787
802
  png_path: Path | str = None,
788
- ) -> object:
803
+ ) -> None:
789
804
  """
790
805
  A function to plot a histogram based on *numeric* columns in a DataFrame.
791
806
  Accepts:
@@ -808,8 +823,7 @@ def plot_histogram(
808
823
  png_path (Path | str, optional): The path to save the image as a png file. Defaults to None.
809
824
 
810
825
 
811
- Returns:
812
- plot object
826
+ Returns: None
813
827
  """
814
828
 
815
829
  # * convert to df if series
@@ -865,7 +879,7 @@ def plot_histogram(
865
879
  if png_path is not None:
866
880
  fig.write_image(Path(png_path).as_posix())
867
881
 
868
- return fig
882
+ return
869
883
 
870
884
 
871
885
  def plot_joint(
@@ -877,7 +891,7 @@ def plot_joint(
877
891
  caption: str = "",
878
892
  title: str = "",
879
893
  png_path: Path | str = None,
880
- ) -> object:
894
+ ) -> None:
881
895
  """
882
896
  Generate a seaborn joint plot for *two numeric* columns of a given DataFrame.
883
897
 
@@ -891,8 +905,7 @@ def plot_joint(
891
905
  - title: The title of the plot.
892
906
  - png_path (Path | str, optional): The path to save the image as a png file. Defaults to None.
893
907
 
894
- Returns:
895
- plot object
908
+ Returns: None
896
909
  """
897
910
 
898
911
  if df.shape[1] != 2:
@@ -964,7 +977,7 @@ def plot_joint(
964
977
  if png_path is not None:
965
978
  fig.savefig(Path(png_path).as_posix())
966
979
 
967
- return fig
980
+ return
968
981
 
969
982
 
970
983
  def plot_box(
@@ -983,7 +996,7 @@ def plot_box(
983
996
  use_log: bool = False,
984
997
  png_path: Path | str = None,
985
998
  renderer: Literal["png", "svg", None] = "png",
986
- ) -> object:
999
+ ) -> None:
987
1000
  """
988
1001
  Plots a horizontal box plot for the given pandas Series.
989
1002
 
@@ -1004,8 +1017,7 @@ def plot_box(
1004
1017
  png_path (Path | str, optional): The path to save the image as a png file. Defaults to None.
1005
1018
  renderer (Literal["png", "svg", None], optional): The renderer to use for saving the image. Defaults to "png".
1006
1019
 
1007
- Returns:
1008
- plot object
1020
+ Returns: None
1009
1021
  """
1010
1022
  ser = to_series(ser)
1011
1023
  if ser is None:
@@ -1131,7 +1143,7 @@ def plot_box(
1131
1143
  if png_path is not None:
1132
1144
  fig.write_image(Path(png_path).as_posix())
1133
1145
 
1134
- return fig
1146
+ return
1135
1147
 
1136
1148
 
1137
1149
  def plot_boxes(
@@ -1148,7 +1160,7 @@ def plot_boxes(
1148
1160
  box_width: float = 0.5,
1149
1161
  png_path: Path | str = None,
1150
1162
  renderer: Literal["png", "svg", None] = "png",
1151
- ) -> object:
1163
+ ) -> None:
1152
1164
  """
1153
1165
  [Experimental] Plot vertical boxes for each unique item in the DataFrame and add annotations for statistics.
1154
1166
 
@@ -1165,8 +1177,7 @@ def plot_boxes(
1165
1177
  png_path (Path | str, optional): The path to save the image as a png file. Defaults to None.
1166
1178
  renderer (Literal["png", "svg", None], optional): The renderer to use for saving the image. Defaults to "png".
1167
1179
 
1168
- Returns:
1169
- plot object
1180
+ Returns: None
1170
1181
  """
1171
1182
 
1172
1183
  if (
@@ -1290,7 +1301,7 @@ def plot_boxes(
1290
1301
  if png_path is not None:
1291
1302
  fig.write_image(Path(png_path).as_posix())
1292
1303
 
1293
- return fig
1304
+ return
1294
1305
 
1295
1306
 
1296
1307
  def plot_facet_stacked_bars(
@@ -1314,7 +1325,7 @@ def plot_facet_stacked_bars(
1314
1325
  sort_values_facet: bool = False,
1315
1326
  relative: bool = False,
1316
1327
  show_pct: bool = False,
1317
- ) -> go.Figure:
1328
+ ) -> None:
1318
1329
 
1319
1330
  """
1320
1331
  A function to plot multiple (subplots_per_row) stacked bar charts, facetted by the third column, with the first column as the index and the second column as the colors.
@@ -1341,8 +1352,7 @@ def plot_facet_stacked_bars(
1341
1352
  - relative (bool): Whether to show the bars as relative values (0-1 range). Default is False.
1342
1353
  - show_pct (bool): Whether to show the annotations as percentages. Default is False.
1343
1354
 
1344
- Returns:
1345
- - go.Figure: The chart object.
1355
+ Returns: None
1346
1356
  """
1347
1357
  # ENFORCE show_pct RULES ---
1348
1358
  if not relative:
@@ -1501,7 +1511,7 @@ def plot_facet_stacked_bars(
1501
1511
  * (-(-len(aggregated_df["facet"].unique()) // subplots_per_row)),
1502
1512
  )
1503
1513
 
1504
- return fig
1514
+ return
1505
1515
 
1506
1516
 
1507
1517
  def plot_sankey(
@@ -1,11 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pandas-plots
3
- Version: 0.15.0
3
+ Version: 0.15.1
4
4
  Summary: A collection of helper for table handling and visualization
5
5
  Project-URL: Homepage, https://github.com/smeisegeier/pandas-plots
6
6
  Project-URL: Repository, https://github.com/smeisegeier/pandas-plots
7
7
  Project-URL: Bug Tracker, https://github.com/smeisegeier/pandas-plots/issues
8
- Author-email: smeisegeier <meisegeiers@rki.de>
8
+ Author-email: smeisegeier <dexterDSD@googlemail.com>
9
9
  License-File: LICENSE
10
10
  Keywords: pivot,plot,plotly,tables,venn,vizualization
11
11
  Classifier: Development Status :: 4 - Beta
@@ -1,9 +1,9 @@
1
1
  pandas_plots/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  pandas_plots/hlp.py,sha256=z8rrVNbH9qMohdXPT-FksP-VkTOjI0bGFj47Sw5p3aY,21141
3
- pandas_plots/pls.py,sha256=dPs9TosCmEvScKdZADRwCJwh-u40BmG4AgCOX8Cpul8,63623
3
+ pandas_plots/pls.py,sha256=k3btK4TWHUJCyHEzu3yLh40G9SuFlW84dYP2RLS5lWY,64118
4
4
  pandas_plots/tbl.py,sha256=mzrUif2TUZ8JJmkgzNpVYApBZS8L0MS1Yjpx9KZN7Vs,32920
5
5
  pandas_plots/ven.py,sha256=2x3ACo2vSfO3q6fv-UdDQ0h1SJyt8WChBGgE5SDCdCk,11673
6
- pandas_plots-0.15.0.dist-info/METADATA,sha256=CexYKyjaJy2O00V2GLjG2yZcg2Y9kDQHK3SBzMaW0BY,7461
7
- pandas_plots-0.15.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
- pandas_plots-0.15.0.dist-info/licenses/LICENSE,sha256=ltLbQWUCs-GBQlTPXbt5nHNBE9U5LzjjoS1Y8hHETM4,1051
9
- pandas_plots-0.15.0.dist-info/RECORD,,
6
+ pandas_plots-0.15.1.dist-info/METADATA,sha256=xQ1FomsfZp38k4o_7J-Bp8dIkW3PvHM_wq4qK8QnWFU,7467
7
+ pandas_plots-0.15.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
+ pandas_plots-0.15.1.dist-info/licenses/LICENSE,sha256=ltLbQWUCs-GBQlTPXbt5nHNBE9U5LzjjoS1Y8hHETM4,1051
9
+ pandas_plots-0.15.1.dist-info/RECORD,,