← All ivy-nodes
IVYXSTUDIO · IVY NODE
H

Histogram Chart Generator

ivy.node.histogram-chart-generator · v0.0.0

ivyx

Generates histogram charts from pandas DataFrames using Plotly and exports them as image buffers. This node creates interactive histogram visualizations with customizable titles, axis labels, dimensions, and image formats. The output chart_image buffer can be used with minio-file-upload for storage, email-builder for email attachments, or image-show for display. Requires a DataFrame (typically from data-frame-creator) and column specifications for visualization.

#Histogram#Chart#Data Visualization#Plotting#Matplotlib#Binning#Frequency Distribution#Data Representation#Graphing#Statistics

Inputs

FieldTypeDescription
titlerequiredstringThe title of the chart.
data_framerequiredobjectThe data frame containing the data for the charts. (Node reference)
x_columnrequiredstringThe column to use for the x-axis.
x_axis_titlerequiredstringThe title of the x-axis.
y_axis_titlerequiredstringThe title of the y-axis.
widthrequirednumberThe width of the chart.
heightrequirednumberThe height of the chart.
formatrequiredstringThe format of the image (default: "png").
featuresobjectFeatures
labelsobjectLabels

Outputs

FieldTypeDescription
chart_imagerequiredobjectCustom type: io.BytesIO
featuresobjectFeatures
labelsobjectLabels

Source

python

# Input preparation
inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]
title = inp["title"]
data_frame = inp["data_frame"]
x_column = inp["x_column"]
x_axis_title = inp["x_axis_title"]
y_axis_title = inp["y_axis_title"]
width = inp["width"]
height = inp["height"]
format = inp["format"]

# Compute
import plotly.express as px
import plotly.io as pio
import io

class HistogramChartGenerator:
    """
    A class to generate Plotly charts and export them as image buffers.
    """

    def __init__(self, data_frame):
        """
        Initialize the HistogramChartGenerator with the data frame to use for charts.

        :param data_frame: The data frame containing the data for the charts.
        """
        self.data_frame=data_frame

    def generate(self, x_column: str, title: str, color_sequence=px.colors.sequential.Viridis ):
        """
        Generate a histogram chart.

        :param x_column: The column to use for the x-axis.
        :param title: The title of the chart.
        :param color_sequence: The color sequence for the chart bars.
        :return: A Plotly Figure object.
        """
        self.fig=px.histogram(
            self.data_frame,
            x=x_column,
            title=title,
            color_discrete_sequence=color_sequence
        )

        return self.fig

    def update_layout(self, x_axis_title: str, y_axis_title: str, width: int=1200, height: int=600):
        """
        Update the layout of a Plotly Figure object.

        :param x_axis_title: The title of the x-axis.
        :param y_axis_title: The title of the y-axis.
        :param width: The width of the chart.
        :param height: The height of the chart.
        """
        self.fig.update_layout(
            title_font_size=16,
            xaxis_title=x_axis_title,
            yaxis_title=y_axis_title,
            bargap=0.2,
            width=width,
            height=height
        )

    def export_to_image(self, format: str="png") -> io.BytesIO:
        """
        Export a Plotly Figure object to an image buffer.

        :param format: The format of the image (default: "png").
        :return: A BytesIO buffer containing the image.
        """
        buffer=io.BytesIO()
        pio.write_image(self.fig, buffer, format=format)
        buffer.seek(0)
        return buffer

histogram_chart_generator = HistogramChartGenerator(data_frame=data_frame)

histogram_chart_generator.generate(
    x_column=x_column,
    title=title,
    color_sequence=px.colors.sequential.Viridis
)

histogram_chart_generator.update_layout(
    x_axis_title=x_axis_title,
    y_axis_title=y_axis_title,
    width=width,
    height=height
)

chart_image = histogram_chart_generator.export_to_image(format=format)

# Output collection (runner reads __ivy_ctx__)
out = __ivy_ctx__["nodes"][__ivy_node_id__]["output"]
out["chart_image"] = chart_image

Tests

Requires: python:3.11

  • basic-chart-generation

    Generate chart with valid inputs (should succeed).

    charthistogramvisualizationbasic