← All ivy-nodes
IVYXSTUDIO · IVY NODE
H
Histogram Chart Generator
ivy.node.histogram-chart-generator · v0.1.0
ivyx✓
Draws a histogram of one DataFrame column with plotly and returns the image as a buffer, with its size and format. Needs plotly and kaleido in the kernel.
#Histogram#Chart#Data Visualization#Plotting#Matplotlib#Binning#Frequency Distribution#Data Representation#Graphing#Statistics
Inputs
| Field | Type | Description |
|---|---|---|
| data_framerequired | Not declared | The data to chart. |
| x_columnrequired | string | The column whose values are counted. |
| title | string | The chart title. |
| x_axis_title | string | The x-axis label. Defaults to the column name. |
| y_axis_title | string | The y-axis label. |
| width | integer | Image width in pixels. |
| height | integer | Image height in pixels. |
| format | string | Image format. |
Outputs
| Field | Type | Description |
|---|---|---|
| chart_imagerequired | Not declared | The rendered chart image. |
| size_bytesrequired | integer | The image's size in bytes. |
| formatrequired | string | The image format written. |
Source
python
# Input preparation
inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]
title = inp.get("title", "")
data_frame = inp["data_frame"]
x_column = inp["x_column"]
x_axis_title = inp.get("x_axis_title") or inp["x_column"]
y_axis_title = inp.get("y_axis_title", "count")
width = int(inp.get("width", 1200))
height = int(inp.get("height", 600))
format = inp.get("format", "png")
# 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
out["size_bytes"] = len(chart_image.getvalue())
out["format"] = formatTests
Requires: python:3.9, plotly, kaleido
- png-histogram
A small frame renders to a PNG of a few kilobytes.
- unknown-column
A column the frame does not have is refused by plotly.