Plotly Type Stubs
plotly-stubs is a stub-only package providing static type annotations for the Plotly Python graphing library. It enables type checkers like Mypy to validate Plotly-related code, improving code quality and maintainability. Currently, it is in 'Beta' development status.
Warnings
- gotcha The `plotly-stubs` package provides *only* type hints. For actual runtime execution of plotting code, the `plotly` library itself must be installed (e.g., `pip install plotly`). `plotly-stubs` does not install `plotly` as a dependency.
- breaking The `plotly-stubs` package strictly requires Python >=3.11 and <3.15. While the core `plotly` library might support older Python versions, using `plotly-stubs` will enforce this Python version constraint during development and type checking.
- gotcha As of `plotly-stubs` version 0.1.3, the project is still in 'Beta' development status. This implies that the API for type stubs might evolve, and complete coverage for all `plotly` features may not yet be available or fully stable.
- gotcha When using `plotly` with `Dash`, users have reported issues with `Dash` version 3.0 introducing breaking type annotations that do not comply with `mypy`, leading to `Untyped decorator` errors for `dash.callback`. This can indirectly affect the perceived type-completeness when `plotly-stubs` is used in a `Dash` application.
Install
-
pip install plotly-stubs
Imports
- Figure
from plotly.graph_objects import Figure
- DataFrame
import pandas as pd from pandas import DataFrame
- px
import plotly.express as px
Quickstart
import plotly.express as px
import pandas as pd
from plotly.graph_objects import Figure # Type hint for the return
def create_population_chart(df: pd.DataFrame) -> Figure:
"""Creates a population scatter plot using Plotly Express."""
fig = px.scatter(
df,
x="gdpPercap",
y="lifeExp",
size="pop",
color="continent",
hover_name="country",
log_x=True,
size_max=60
)
return fig
# Example usage (assuming 'gapminder' dataset is available, e.g., from px.data.gapminder())
# df_gapminder = px.data.gapminder()
# fig = create_population_chart(df_gapminder)
# fig.show()