Plotly
import plotly.express as px
import plotly.graph_objects as go
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.show()
Gantt plot
px.timeline(df, x_start="start", x_end="end", y="index", color="value")
Geo scatter (point) plot
px.scatter_geo(gdf, lat=gdf.geometry.y, lon=gdf.geometry.x, fitbounds="locations")
px.scatter_mapbox(df,
lat=gdf.geometry.y,
lon=gdf.geometry.x,
mapbox_style="carto-positron", # "open-street-map"
)
fig = go.Figure(go.Scattergeo(lon=gdf.geometry.x, lat=gdf.geometry.y))
fig = go.Figure(Scattermapbox(lon=gdf.geometry.x, lat=gdf.geometry.y))
fig.update_geos(
visible=True, resolution=50, showcountries=True, fitbounds="locations",
)
fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
Dash
test.py:
import dash
from dash import html
from dash import dcc
import plotly.express as px
app = dash.Dash(__name__)
# Hello world
app.layout = html.Div(
[
html.Div("Hello World"),
]
)
# figure
app.layout = html.Div(children=[
html.H1(children='Hello Dash'),
html.Div(children='''
Dash: A web application framework for your data.
'''),
dcc.Graph(
id='example-graph',
figure=fig
)
])
# run
if __name__ == '__main__':
app.run_server(debug=True, port=8056)
python test.py
Go to http://127.0.0.1:8050
from jupyter_dash.comms import _send_jupyter_config_comm_request
_send_jupyter_config_comm_request()
from jupyter_dash import JupyterDash
from dash import html
# JupyterDash.infer_jupyter_proxy_config()
app = JupyterDash(__name__)
app.layout = html.Div(
[
html.Div("Hello World"),
]
)
app.run_server(mode="jupyterlab", port=8055, debug=True)
Example of callbacks
app.py
import dash
import dash_bootstrap_components as dbc
from dash import dcc, html
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
app.layout = html.Div(
[
dcc.Dropdown(
id="letter_drop",
options=[
{"label": "a", "value": "a"},
{"label": "b", "value": "b"},
],
value="Letter",
),
dbc.Row(id="selected_letter_drop"),
],
)
@app.callback(
Output("selected_letter_drop", "children"),
Input("letter_drop", "value"),
)
def selected_letter_drop(letter):
return html.H1(f"Letter choice: {letter}")
if __name__ == "__main__":
app.run_server(debug=True, port=8058)
app.py
import dash
from dash import html
from dash import dcc
from dash.dependencies import Input, Output
import plotly.express as px
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv")
app = dash.Dash(__name__)
app.layout = html.Div(
[
dcc.Graph(id="graph-with-slider"),
dcc.Slider(
id="year-slider",
min=df["year"].min(),
max=df["year"].max(),
value=df["year"].min(),
marks={str(year): str(year) for year in df["year"].unique()},
step=None,
),
]
)
@app.callback(Output("graph-with-slider", "figure"), Input("year-slider", "value"))
def update_figure(selected_year):
filtered_df = df[df.year == selected_year]
fig = px.scatter(
filtered_df,
x="gdpPercap",
y="lifeExp",
size="pop",
color="continent",
hover_name="country",
log_x=True,
size_max=55,
)
fig.update_layout(transition_duration=500)
return fig