diff --git a/__init__.py b/__init__.py index 3a8bc5a..b9c0e72 100644 --- a/__init__.py +++ b/__init__.py @@ -12,6 +12,7 @@ from .modules.cda.py_kv_int_create import PyKvIntCreate from .modules.cda.py_kv_string_create import PyKvStringCreate from .modules.matplotlib.mpl_bar import MPLBar from .modules.matplotlib.mpl_line import MPLLine +from .modules.matplotlib.mpl_pie_chart import MPLPieChart from .modules.matplotlib.mpl_scatter import MPLScatter from .modules.numpy_wrapper.numpy_float_create import NumpyFloatCreate from .modules.numpy_wrapper.numpy_int_create import NumpyIntCreate @@ -153,6 +154,7 @@ NODE_CLASS_MAPPINGS: Dict[str, Type[T]] = { "CDATextCreate": CDATextCreate, "MPLBar": MPLBar, "MPLLine": MPLLine, + "MPLPieChart": MPLPieChart, "MPLScatter": MPLScatter, "NumpyFloatCreate": NumpyFloatCreate, "NumpyIntCreate": NumpyIntCreate, @@ -297,6 +299,7 @@ NODE_DISPLAY_NAME_MAPPINGS: Dict[str, str] = { "CDATextCreate": "CDA Text Create", "MPLBar": "MPL Bar Chart", "MPLLine": "MPL Line Plot", + "MPLPieChart": "MPL Pie Chart", "MPLScatter": "MPL Scatter Plot", "NumpyFloatCreate": "Numpy Float Create", "NumpyIntCreate": "Numpy Int Create", diff --git a/docs/images/pie_chart.png b/docs/images/pie_chart.png new file mode 100644 index 0000000..b8045ea Binary files /dev/null and b/docs/images/pie_chart.png differ diff --git a/docs/reference/mpl_pie_chart.md b/docs/reference/mpl_pie_chart.md new file mode 100644 index 0000000..ea3ee55 --- /dev/null +++ b/docs/reference/mpl_pie_chart.md @@ -0,0 +1,24 @@ +# M P L Pie Chart +:MPL Pie Chart +Generates a pie chart from a pandas DataFrame. + +Below screenshot shows a workflow to generate a pie chart from categorical data. +![Pie chart](../images/pie_chart.png) + +## Input +| Name | Data type | +|---|---| +| dataframe | DataFrame | +| label_column_name | String | +| data_column_name | String | +| title | String | + +## Output +| Data type | +|---| +| Image | + +
+Category: Plot + +ComfyUI Data Analysis Node Reference. © 2025 Hide Inada (HowToSD.com). All rights reserved. diff --git a/docs/reference/node_reference.md b/docs/reference/node_reference.md index 0d41364..8ebaa65 100644 --- a/docs/reference/node_reference.md +++ b/docs/reference/node_reference.md @@ -131,6 +131,7 @@ ## Plot | Node | Description | | --- | --- | +| [M P L Pie Chart](mpl_pie_chart.md) | :MPL Pie Chart | | [MPL Bar Chart](mpl_bar.md) | Generates a bar chart from a Pandas DataFrame. | | [MPL Line Plot](mpl_line.md) | Generates a line plot from a pandas DataFrame. | | [MPL Scatter Plot](mpl_scatter.md) | Generates a scatter plot from a pandas DataFrame. | diff --git a/examples/workflows/pie_chart.json b/examples/workflows/pie_chart.json new file mode 100644 index 0000000..d6e3eae --- /dev/null +++ b/examples/workflows/pie_chart.json @@ -0,0 +1 @@ +{"last_node_id":23,"last_link_id":39,"nodes":[{"id":2,"type":"PandasCreate","pos":[2874.72607421875,520.831298828125],"size":[270.51373291015625,144.48915100097656],"flags":{},"order":0,"mode":0,"inputs":[],"outputs":[{"name":"DATAFRAME","type":"DATAFRAME","links":[1],"slot_index":0}],"properties":{"Node name for S&R":"PandasCreate"},"widgets_values":["Fruits\napple\napple\napple\nbanana\nbanana\ncoconut"]},{"id":1,"type":"PandasValueCounts","pos":[2878.360107421875,709.3477172851562],"size":[269.8277587890625,66.52304077148438],"flags":{},"order":1,"mode":0,"inputs":[{"name":"dataframe","type":"DATAFRAME","link":1}],"outputs":[{"name":"DATAFRAME","type":"DATAFRAME","links":[2,38],"slot_index":0}],"properties":{"Node name for S&R":"PandasValueCounts"},"widgets_values":["Fruits"]},{"id":23,"type":"PreviewImage","pos":[3220.225341796875,853.7537841796875],"size":[470.0382995605469,449.1227722167969],"flags":{},"order":4,"mode":0,"inputs":[{"name":"images","type":"IMAGE","link":39}],"outputs":[],"properties":{"Node name for S&R":"PreviewImage"}},{"id":22,"type":"MPLPieChart","pos":[2881.26220703125,852.089111328125],"size":[263.0093688964844,109.4092025756836],"flags":{},"order":3,"mode":0,"inputs":[{"name":"dataframe","type":"DATAFRAME","link":38}],"outputs":[{"name":"IMAGE","type":"IMAGE","links":[39],"slot_index":0}],"properties":{"Node name for S&R":"MPLPieChart"},"widgets_values":["Fruits","count","Fruit distribution"]},{"id":3,"type":"PandasShowDataFrame","pos":[3227.885498046875,700.7517700195312],"size":[210,107.45079040527344],"flags":{},"order":2,"mode":0,"inputs":[{"name":"dataframe","type":"DATAFRAME","link":2}],"outputs":[{"name":"STRING","type":"STRING","links":null,"shape":6}],"properties":{"Node name for S&R":"PandasShowDataFrame"},"widgets_values":[" Fruits count\n0 apple 3\n1 banana 2\n2 coconut 1"]}],"links":[[1,2,0,1,0,"DATAFRAME"],[2,1,0,3,0,"DATAFRAME"],[38,1,0,22,0,"DATAFRAME"],[39,22,0,23,0,"IMAGE"]],"groups":[],"config":{},"extra":{"ds":{"scale":0.9777396879878871,"offset":[-2464.9461010126247,-450.0436961304677]},"node_versions":{"data-analysis":"bebf4760bc61e4ce5326061b2f56e96c2c9f5d60","comfy-core":"v0.3.10-40-gd0f3752"},"ue_links":[]},"version":0.4} \ No newline at end of file diff --git a/modules/matplotlib/mpl_pie_chart.py b/modules/matplotlib/mpl_pie_chart.py new file mode 100644 index 0000000..7ed6c17 --- /dev/null +++ b/modules/matplotlib/mpl_pie_chart.py @@ -0,0 +1,68 @@ +from typing import Any, Dict +from matplotlib import pyplot as plt +import pandas as pd +from .util import plot_post_steps + +class MPLPieChart: + """ + :MPL Pie Chart + Generates a pie chart from a pandas DataFrame. + + Below screenshot shows a workflow to generate a pie chart from categorical data. + ![Pie chart](../images/pie_chart.png) + + category: Plot + """ + + @classmethod + def INPUT_TYPES(cls) -> Dict[str, Any]: + """ + Defines the input types for the `line_plot` function. + + Returns: + Dict[str, Any]: A dictionary specifying required input types. + """ + return { + "required": { + "dataframe": ("DATAFRAME", {}), + "label_column_name": ("STRING", {"default": ""}), + "data_column_name": ("STRING", {"default": ""}), + "title": ("STRING", {"default": ""}) + } + } + + RETURN_TYPES: tuple = ("IMAGE",) + FUNCTION: str = "f" + CATEGORY: str = "Data Analysis" + + def f(self, + dataframe: pd.DataFrame, + label_column_name: str, + data_column_name: str, + title: str) -> tuple: + """ + Generates a pie chart from a Pandas DataFrame. + + Args: + dataframe (DataFrame): The DataFrame. + label_column_name (str): The column name for the labels. + data_column_name (str): The column name for the data. + title (str): The title of the plot. + + Returns: + tuple: A tuple containing the image tensor representation of the plot. + """ + + df = dataframe + + # Create the plot + fig, ax = plt.subplots() + ax.pie(df[data_column_name], labels=df[label_column_name], autopct='%1.1f%%', startangle=140) + return plot_post_steps( + fig, + ax, + plt, + title, + "", + "", + False) diff --git a/pyproject.toml b/pyproject.toml index 02e7b42..b390053 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "data-analysis" description = "Data analysis custom modules for ComfyUI - Use Pandas & Matplotlib within ComfyUI" -version = "1.3.4" +version = "1.3.5" license = {file = "LICENSE"} [project.urls]