An open-source interactive data visualization library for Python
—
Tools for creating subplot layouts, converting matplotlib figures, and other utility operations essential for complex figure composition and integration with other plotting libraries.
Functions for creating multi-panel figure layouts with customizable subplot arrangements and spacing.
def make_subplots(rows=1, cols=1, shared_xaxes=False, shared_yaxes=False,
start_cell='top-left', print_grid=False, horizontal_spacing=None,
vertical_spacing=None, subplot_titles=None, column_widths=None,
row_heights=None, specs=None, insets=None, column_titles=None,
row_titles=None, x_title=None, y_title=None, **kwargs):
"""
Create subplots with customizable layout and spacing.
Parameters:
- rows: int, number of subplot rows
- cols: int, number of subplot columns
- shared_xaxes: bool or str, whether to share x-axes across subplots
- shared_yaxes: bool or str, whether to share y-axes across subplots
- start_cell: str, starting position ('top-left', 'bottom-left', etc.)
- horizontal_spacing: float, spacing between subplot columns (0-1)
- vertical_spacing: float, spacing between subplot rows (0-1)
- subplot_titles: list of str, titles for each subplot
- column_widths: list of float, relative widths of subplot columns
- row_heights: list of float, relative heights of subplot rows
- specs: list of list of dict, subplot specifications
- insets: list of dict, inset subplot specifications
- column_titles: list of str, titles for each column
- row_titles: list of str, titles for each row
- x_title: str, shared x-axis title
- y_title: str, shared y-axis title
Returns:
Figure: Figure object with subplot layout configured
"""
def get_subplots(rows=1, columns=1, print_grid=False, **kwargs):
"""
Legacy function for creating subplot layouts (deprecated, use make_subplots).
Parameters:
- rows: int, number of subplot rows
- columns: int, number of subplot columns
- print_grid: bool, whether to print grid layout
- **kwargs: additional subplot parameters
Returns:
Figure: Figure object with subplot layout
"""Convert matplotlib figures to plotly format for interactive visualization.
def mpl_to_plotly(fig, resize=False, strip_style=False, verbose=False):
"""
Convert matplotlib figure to plotly figure.
Parameters:
- fig: matplotlib Figure object to convert
- resize: bool, whether to resize the converted figure
- strip_style: bool, whether to remove matplotlib styling
- verbose: bool, whether to print conversion details
Returns:
Figure: Plotly figure object equivalent to matplotlib input
"""Access plotly server configuration and validation utilities.
def get_config_plotly_server_url():
"""
Get configured plotly server URL.
Returns:
str: Server URL configuration string
"""
def get_graph_obj(obj, obj_type=None):
"""
Validate and convert object to plotly graph object.
Parameters:
- obj: object to convert/validate
- obj_type: str, expected object type
Returns:
graph object: Validated plotly graph object
"""
def return_figure_from_figure_or_data(figure_or_data, validate_figure):
"""
Extract figure from figure or data input.
Parameters:
- figure_or_data: Figure object or data traces
- validate_figure: bool, whether to validate the figure
Returns:
Figure: Plotly figure object
"""import plotly.tools as tools
import plotly.graph_objects as go
import matplotlib.pyplot as plt
# Create subplot layout
fig = tools.make_subplots(
rows=2, cols=2,
subplot_titles=['Plot 1', 'Plot 2', 'Plot 3', 'Plot 4'],
horizontal_spacing=0.1,
vertical_spacing=0.1
)
# Add traces to specific subplots
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_trace(go.Bar(x=['A', 'B', 'C'], y=[1, 2, 3]), row=1, col=2)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[6, 5, 4]), row=2, col=1)
fig.add_trace(go.Heatmap(z=[[1, 2], [3, 4]]), row=2, col=2)
fig.show()
# Convert matplotlib figure
plt.figure(figsize=(8, 6))
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.title('Matplotlib Figure')
mpl_fig = plt.gcf()
# Convert to plotly
plotly_fig = tools.mpl_to_plotly(mpl_fig)
plotly_fig.show()
# Shared axes example
fig_shared = tools.make_subplots(
rows=3, cols=1,
shared_xaxes=True,
vertical_spacing=0.02,
subplot_titles=['Time Series 1', 'Time Series 2', 'Time Series 3']
)
# Add time series data
import pandas as pd
dates = pd.date_range('2023-01-01', periods=100)
fig_shared.add_trace(go.Scatter(x=dates, y=range(100)), row=1, col=1)
fig_shared.add_trace(go.Scatter(x=dates, y=range(100, 0, -1)), row=2, col=1)
fig_shared.add_trace(go.Scatter(x=dates, y=[i**2 for i in range(100)]), row=3, col=1)
fig_shared.show()Install with Tessl CLI
npx tessl i tessl/pypi-plotly