Skip to content

Utilities API Reference

Utility functions for saving plots and controlling their appearance.

Plot Size

ggplotly.utils.ggsize

A class to set the size of a ggplot figure.

Parameters:

Name Type Description Default
width int

The width of the plot in pixels.

required
height int

The height of the plot in pixels.

required

apply(plot)

Apply the size to the given plot by setting the width and height.

Saving Plots

ggplotly.utils.ggsave

Bases: Utils

A class to save ggplot figures as either HTML or PNG.

Parameters:

Name Type Description Default
filename str

The name of the file to save the figure (including extension).

required
width int

The width of the image in pixels (for PNG). If None, the current figure width is used.

None
height int

The height of the image in pixels (for PNG). If None, the current figure height is used.

None
scale float

Scale the image by this factor (for PNG).

1.0

apply(plot)

Apply the save operation to the given plot.

save_html(plot)

Save the plot as an HTML file.

Uses CDN references for Plotly.js and MathJax, resulting in small file sizes (~8KB) but requiring internet for viewing. MathJax CDN enables LaTeX rendering (e.g., geom_text with parse=True).

save_png(plot, width, height)

Save the plot as a PNG file. Requires the kaleido package to be installed.

Axis Limits

ggplotly.limits.xlim

Bases: Coord

Set x-axis limits.

This zooms the plot to the specified range without clipping data. Equivalent to coord_cartesian(xlim=...).

Parameters:

Name Type Description Default
*args

Either two positional arguments (min, max) or a tuple/list.

()

Examples:

ggplot(df, aes(x='x', y='y')) + geom_point() + xlim(0, 10) ggplot(df, aes(x='x', y='y')) + geom_point() + xlim((0, 10))

apply(fig)

Apply x-axis limits to the figure.

Parameters:

Name Type Description Default
fig Figure

The Plotly figure to modify.

required

Returns:

Name Type Description
None

Modifies the figure in place.

ggplotly.limits.ylim

Bases: Coord

Set y-axis limits.

This zooms the plot to the specified range without clipping data. Equivalent to coord_cartesian(ylim=...).

Parameters:

Name Type Description Default
*args

Either two positional arguments (min, max) or a tuple/list.

()

Examples:

ggplot(df, aes(x='x', y='y')) + geom_point() + ylim(0, 100) ggplot(df, aes(x='x', y='y')) + geom_point() + ylim((0, 100))

apply(fig)

Apply y-axis limits to the figure.

Parameters:

Name Type Description Default
fig Figure

The Plotly figure to modify.

required

Returns:

Name Type Description
None

Modifies the figure in place.

ggplotly.limits.lims

Bases: Coord

Set axis limits for multiple aesthetics.

This zooms the plot to the specified ranges without clipping data.

Parameters:

Name Type Description Default
x

Tuple of (min, max) for x-axis limits.

None
y

Tuple of (min, max) for y-axis limits.

None

Examples:

ggplot(df, aes(x='x', y='y')) + geom_point() + lims(x=(0, 10), y=(0, 100)) ggplot(df, aes(x='x', y='y')) + geom_point() + lims(y=(0, 100))

apply(fig)

Apply axis limits to the figure.

Parameters:

Name Type Description Default
fig Figure

The Plotly figure to modify.

required

Returns:

Name Type Description
None

Modifies the figure in place.