jupyter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Plotly's figure data structure supports defining subplots of various types (e.g. cartesian, polar, 3-dimensional, maps etc) with attached traces of various compatible types (e.g. scatter, bar, choropleth, surface etc). This means that Plotly figures are not constrained to representing a fixed set of "chart types" such as scatter plots only or bar charts only or line charts only: any subplot can contain multiple traces of different types.
Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures.
Plotly Express exposes a number of functions such as px.scatter()
and px.choropleth()
which generally speaking only contain traces of the same type, with exceptions made for trendlines and marginal distribution plots.
Figures produced with Plotly Express functions support the add_trace()
method documented below, just like figures created with graph objects so it is easy to start with a Plotly Express figure containing only traces of a given type, and add traces of another type.
import plotly.express as px
fruits = ["apples", "oranges", "bananas"]
fig = px.line(x=fruits, y=[1,3,2], color=px.Constant("This year"),
labels=dict(x="Fruit", y="Amount", color="Time Period"))
fig.add_bar(x=fruits, y=[2,1,3], name="Last year")
fig.show()
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=[0, 1, 2, 3, 4, 5],
y=[1.5, 1, 1.3, 0.7, 0.8, 0.9]
))
fig.add_trace(
go.Bar(
x=[0, 1, 2, 3, 4, 5],
y=[1, 0.5, 0.7, -1.2, 0.3, 0.4]
))
fig.show()
import plotly.graph_objects as go
# Load data
import json
import six.moves.urllib
response = six.moves.urllib.request.urlopen(
"https://raw.githubusercontent.com/plotly/datasets/master/steepest.json")
data = json.load(response)
# Create figure
fig = go.Figure()
fig.add_trace(
go.Contour(
z=data["contour_z"][0],
y=data["contour_y"][0],
x=data["contour_x"][0],
ncontours=30,
showscale=False
)
)
fig.add_trace(
go.Scatter(
x=data["trace_x"],
y=data["trace_y"],
mode="markers+lines",
name="steepest",
line=dict(
color="black"
)
)
)
fig.show()
See https://plotly.com/python/reference/ for more information and attribute options!