px :: Plotly express
https://plotly.com/python-api-reference/
- Plotly Express: high-level interface for data visualization
- Graph Objects: low-level interface to figures, traces and layout
- Subplots: helper function for layout out multi-plot figures
- Figure Factories: helper methods for building specific complex charts
- I/O: low-level interface for displaying, reading and writing figures
plotly.colors: colorscales and utility functionsplotly.data: built-in datasets for demonstration, educational and test purposes
import plotly.express as px
px vs. go
import pandas as pd
df = pd.DataFrame({
"Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
"Contestant": ["Alex", "Alex", "Alex", "Jordan", "Jordan", "Jordan"],
"Number Eaten": [2, 1, 3, 1, 3, 2],
})
# Plotly Express ----------------------------------------------------
import plotly.express as px
fig = px.bar(df,
x="Fruit", y="Number Eaten",
color="Contestant", barmode="group")
fig.show()
# Graph Objects -----------------------------------------------------
import plotly.graph_objects as go
fig = go.Figure()
for contestant, group in df.groupby("Contestant"):
fig.add_trace(
go.Bar(
x=group["Fruit"], y=group["Number Eaten"],
name=contestant,
\t\t\thovertemplate="Contestant=%s
Fruit=%%{x}
Number Eaten=%%{y} "% contestant)
\t)
fig.update_layout(legend_title_text = "Contestant")
fig.update_xaxes(title_text="Fruit")
fig.update_yaxes(title_text="Number Eaten")
fig.show()