pandas-plots 0.15.13__tar.gz → 0.16.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: pandas-plots
3
- Version: 0.15.13
3
+ Version: 0.16.0
4
4
  Summary: A collection of helper for table handling and visualization
5
5
  Keywords: tables,pivot,plotly,venn,plot,vizualization
6
6
  Author: smeisegeier
@@ -99,6 +99,7 @@ tbl.show_num_df(
99
99
  - `plot_quadrants()` quickly shows a 2x2 heatmap
100
100
  - `plot_facet_stacked_bars()` shows stacked bars for a facet value as subplots
101
101
  - `plot_sankey()` generates a Sankey diagram
102
+ - `plot_pie()` generates a pie chart
102
103
  <br>
103
104
 
104
105
  - `ven` offers functions for _venn diagrams_
@@ -65,6 +65,7 @@ tbl.show_num_df(
65
65
  - `plot_quadrants()` quickly shows a 2x2 heatmap
66
66
  - `plot_facet_stacked_bars()` shows stacked bars for a facet value as subplots
67
67
  - `plot_sankey()` generates a Sankey diagram
68
+ - `plot_pie()` generates a pie chart
68
69
  <br>
69
70
 
70
71
  - `ven` offers functions for _venn diagrams_
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pandas-plots"
3
- version = "0.15.13"
3
+ version = "0.16.0"
4
4
  description = "A collection of helper for table handling and visualization"
5
5
  long_description = "file: README.md"
6
6
  long_description_content_type = "text/markdown"
@@ -11,8 +11,10 @@ import seaborn as sb
11
11
  from matplotlib import pyplot as plt
12
12
  from plotly import express as px
13
13
  import plotly.graph_objects as go
14
+ import plotly.io as pio
14
15
  from plotly.subplots import make_subplots
15
16
  import plotly # needed for return types
17
+
16
18
  import re
17
19
 
18
20
  from .hlp import *
@@ -1889,6 +1891,86 @@ def plot_sankey(
1889
1891
  fig.update_layout(title_text=chart_title, font_size=font_size, width=width, height=height)
1890
1892
  fig.show(renderer=renderer or os.getenv("RENDERER"), width=width, height=height)
1891
1893
 
1894
+
1895
+ def plot_pie(
1896
+ data: pd.Series | pd.DataFrame,
1897
+ caption: str = None,
1898
+ width=800,
1899
+ height=500,
1900
+ renderer="notebook",
1901
+ donut_size=0,
1902
+ ):
1903
+ """
1904
+ Creates and displays a pie or donut chart using Plotly Express.
1905
+
1906
+ Args:
1907
+ data (pd.Series or pd.DataFrame): The data to plot.
1908
+ If a DataFrame, it must have only one column. The index will be
1909
+ used for labels and the values for the pie slice sizes.
1910
+ caption (str): The title for the plot.
1911
+ height (int, optional): The height of the plot in pixels. Defaults to 800.
1912
+ width (int, optional): The width of the plot in pixels. Defaults to 500.
1913
+ renderer (str, optional): The Plotly renderer to use (e.g., 'notebook', 'png', 'svg').
1914
+ Defaults to 'notebook'.
1915
+ donut_size (float, optional): A value between 0 and 1 to create a donut chart.
1916
+ A value of 0 results in a regular pie chart. Defaults to 0.
1917
+ """
1918
+ # Store the original renderer to restore it later
1919
+ # original_renderer = pio.renderers.default
1920
+ # ? override renderer
1921
+ original_renderer = "notebook"
1922
+
1923
+ # * 1. Check for correct data type first
1924
+ if not isinstance(data, (pd.Series, pd.DataFrame)):
1925
+ print("Error: Data must be a pandas Series or DataFrame.")
1926
+ return
1927
+
1928
+ # * 2. **CONVERT SERIES TO DATAFRAME**
1929
+ if isinstance(data, pd.Series):
1930
+ # * Get the name of the Series
1931
+ label = data.name
1932
+ # * Convert the Series to a DataFrame with a column named 'values'
1933
+ # * The index will automatically become the DataFrame index
1934
+ data = data.to_frame(name="values")
1935
+ else:
1936
+ # * Get the name of the first column
1937
+ label = data.columns[0]
1938
+
1939
+ # * 3. Ensure the DataFrame has only one column
1940
+ if len(data.columns) != 1:
1941
+ print("Error: DataFrame must have exactly one column for this function.")
1942
+ return
1943
+
1944
+ # * Set the temporary renderer for the plot
1945
+ pio.renderers.default = renderer
1946
+
1947
+ # * Get the number of observations
1948
+ n = len(data)
1949
+
1950
+ # * take 1st (only) column and use value counts to get distribution
1951
+ data = data.iloc[:, 0].value_counts()
1952
+
1953
+ # * 4. Create the figure
1954
+ fig = px.pie(
1955
+ data,
1956
+ values=data,
1957
+ names=data.index,
1958
+ title=f"{_set_caption(caption)}{label}, n = {n:_}",
1959
+ height=height,
1960
+ width=width,
1961
+ hole=donut_size,
1962
+ template="plotly_dark" if os.getenv("THEME") == "dark" else "plotly",
1963
+ )
1964
+
1965
+ # * Display the plot
1966
+ fig.show()
1967
+
1968
+ # finally:
1969
+ # * 5. Restore the original renderer, ensuring it's always done
1970
+ pio.renderers.default = original_renderer
1971
+
1972
+
1973
+
1892
1974
  # * extend objects to enable chaining
1893
1975
  pd.DataFrame.plot_bars = plot_bars
1894
1976
  pd.DataFrame.plot_stacked_bars = plot_stacked_bars
@@ -1899,3 +1981,4 @@ pd.DataFrame.plot_quadrants = plot_quadrants
1899
1981
  pd.DataFrame.plot_histogram = plot_histogram
1900
1982
  pd.DataFrame.plot_joint = plot_joint
1901
1983
  pd.DataFrame.plot_sankey = plot_sankey
1984
+ pd.DataFrame.plot_pie = plot_pie