jupyter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
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.
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", facet_col="sex",
width=800, height=400)
fig.update_layout(
margin=dict(l=20, r=20, t=20, b=20),
paper_bgcolor="LightSteelBlue",
)
fig.show()
Dash is the best way to build analytical apps in Python using Plotly figures. To run the app below, run pip install dash
, click "Download" to get the code and run python app.py
.
Get started with the official Dash docs and learn how to effortlessly style & deploy apps like this with Dash Enterprise.
from IPython.display import IFrame
snippet_url = 'https://dash-gallery.plotly.host/python-docs-dash-snippets/'
IFrame(snippet_url + 'setting-graph-size', width='100%', height=630)
Graph objects are the low-level building blocks of figures which you can use instead of Plotly Express for greater control.
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
y=[0, 1, 2, 3, 4, 5, 6, 7, 8]
))
fig.update_layout(
autosize=False,
width=500,
height=500,
margin=dict(
l=50,
r=50,
b=100,
t=100,
pad=4
),
paper_bgcolor="LightSteelBlue",
)
fig.show()
Set automargin to True
and Plotly will automatically increase the margin size to prevent ticklabels from being cut off or overlapping with axis titles.
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Bar(
x=["Apples", "Oranges", "Watermelon", "Pears"],
y=[3, 2, 1, 4]
))
fig.update_layout(
autosize=False,
width=500,
height=500,
yaxis=dict(
title_text="Y-axis Title",
ticktext=["Very long label", "long label", "3", "label"],
tickvals=[1, 2, 3, 4],
tickmode="array",
titlefont=dict(size=30),
)
)
fig.update_yaxes(automargin=True)
fig.show()
See https://plotly.com/python/reference/layout/ for more information and chart attribute options!