import plotly.express as px
= px.data.gapminder().query("year==2007") df
Introduction to Plotly Express
Introduction to Plotly Express
The plotly.express module (usually imported as px) contains functions that can create entire figures at once, and is referred to as Plotly Express or PX. Plotly Express is a built-in part of the plotly library, and is the recommended starting point for creating most common figures. Every Plotly Express function uses graph objects internally and returns a plotly.graph_objects.Figure
instance. Throughout the plotly documentation, you will find the Plotly Express way of building figures at the top of any applicable page, followed by a section on how to use graph objects to build similar figures. Any figure created in a single function call with Plotly Express could be created using graph objects alone, but with between 5 and 100 times more code.
df.columns
Index(['country', 'continent', 'year', 'lifeExp', 'pop', 'gdpPercap',
'iso_alpha', 'iso_num'],
dtype='object')
df.describe()
year | lifeExp | pop | gdpPercap | iso_num | |
---|---|---|---|---|---|
count | 142.0 | 142.000000 | 1.420000e+02 | 142.000000 | 142.000000 |
mean | 2007.0 | 67.007423 | 4.402122e+07 | 11680.071820 | 425.880282 |
std | 0.0 | 12.073021 | 1.476214e+08 | 12859.937337 | 249.111541 |
min | 2007.0 | 39.613000 | 1.995790e+05 | 277.551859 | 4.000000 |
25% | 2007.0 | 57.160250 | 4.508034e+06 | 1624.842248 | 209.500000 |
50% | 2007.0 | 71.935500 | 1.051753e+07 | 6124.371109 | 410.000000 |
75% | 2007.0 | 76.413250 | 3.121004e+07 | 18008.835640 | 636.000000 |
max | 2007.0 | 82.603000 | 1.318683e+09 | 49357.190170 | 894.000000 |
='lifeExp', hover_name="country") px.strip(df, x
='lifeExp', color="continent", hover_name="country") px.strip(df, x
='lifeExp', color="continent", hover_name="country") px.histogram(df, x
='lifeExp', color="continent", hover_name="country", marginal="rug") px.histogram(df, x
='lifeExp', y="pop", color="continent", hover_name="country", marginal="rug") px.histogram(df, x
='lifeExp', y="pop", color="continent", hover_name="country", marginal="rug", facet_col="continent") px.histogram(df, x
='lifeExp', x="pop", y="continent", hover_name="country") px.bar(df, color
='lifeExp', values="pop", path=["continent", "country"], hover_name="country", height=500) px.sunburst(df, color
='lifeExp', values="pop", path=["continent", "country"], hover_name="country", height=500) px.treemap(df, color
='lifeExp', locations="iso_alpha", hover_name="country", height=500) px.choropleth(df, color
="gdpPercap", y='lifeExp', hover_name="country", height=500) px.scatter(df, x
="gdpPercap", y='lifeExp', hover_name="country", color="continent",size="pop", height=500) px.scatter(df, x
We can see that the curve follows a logarithmic path, so make log_x=True
to straighten out the line to view the relationships in an easier manner. In the graph below we can view the monotic and nonmonotonic relationships in the dataset.
="gdpPercap", y='lifeExp', hover_name="country", color="continent",size="pop", size_max=60, log_x=True, height=500) px.scatter(df, x
= px.scatter(df, x="gdpPercap", y='lifeExp', hover_name="country", color="continent",size="pop", size_max=60, log_x=True, height=500) fig
This will allow you to inspect the values for each of these cells, unfortunately this is a great deal easier to see in JupyterLab.
"json") fig.show(
Unable to display output for mime type(s): application/json
import plotly.express as px
= px.data.gapminder().query("year == 2007")
df
= px.scatter(df, x="gdpPercap",y="lifeExp", color="continent", log_x=True, size="pop", size_max=60,
fig ="country", height=600, width=1000, template="simple_white",
hover_name=px.colors.qualitative.G10,
color_discrete_sequence="Health vs Wealth 2007",
title=dict(
labels="Continent", pop="Population",
continent="GDP per Capita (US$, price-adjusted)",
gdpPercap="Life Expectancy (years)"))
lifeExp
="Rockwell",
fig.update_layout(font_family=dict(orientation="h", title="", y=1.1, x=1, xanchor="right", yanchor="bottom"))
legend="$", range=[2,5], dtick=1)
fig.update_xaxes(tickprefixrange=[30,90])
fig.update_yaxes("lifeExp"]*df["pop"]).sum()/df["pop"].sum(), line_width=1, line_dash="dot")
fig.add_hline((df["gdpPercap"]*df["pop"]).sum()/df["pop"].sum(), line_width=1, line_dash="dot")
fig.add_vline((df[
fig.show()
# fig.write_image("gapminder_2007.svg") # static export
# fig.write_html("gapminder_2007.html") # interactive export
# fig.write_json("gapminder_2007.json") # serialized export
Animations in Plotly Express
= px.data.gapminder()
df_animation
= px.scatter(df_animation, x="gdpPercap", y="lifeExp",
anim_fig ="Health vs Wealth from 1952 to 2007",
title=dict(continent="Continent", pop="Population", gdpPercap="GDP per Capita (US$, price-adjusted)", lifeExp="Life Expectancy (years)"),
labels="year", animation_group="country",
animation_frame="pop",
size="continent",
color="country",
hover_name=600,width=1000,
height="simple_white",
template=px.colors.qualitative.G10,
color_discrete_sequence=True,
log_x=60,
size_max=[100,100000],
range_x=[25,90])
range_y
="Rockwell",
anim_fig.update_layout(font_family=dict(orientation="h", title="", y=1.1, x=1, xanchor="right", yanchor="bottom"))
legend="$", range=[2,5], dtick=1) anim_fig.update_xaxes(tickprefix
"gapminder_animation.html", auto_play=False) # You're able to export this animation. anim_fig.write_html(
=600 px.defaults.height
import plotly.express as px
= [[.1, .3, .5, .7, .9],
z 1, .8, .6, .4, .2],
[.2, 0, .5, .7, .9],
[.9, .8, .4, .2, 0],
[.3, .4, .5, .7, 1]]
[
= px.imshow(z, text_auto=True)
fig fig.show()
import plotly.express as px
= px.data.wind()
df = px.bar_polar(df, r="frequency", theta="direction", height=600,
fig ="strength", template="plotly_dark",
color= px.colors.sequential.Plasma_r)
color_discrete_sequence fig.show()
= px.data.election()
df = px.scatter_ternary(df, a="Joly", b="Coderre", c="Bergeron", color="winner", size="total", hover_name="district",
fig =15, color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"} )
size_max fig.show()
= px.data.election()
df = px.scatter_3d(df, x="Joly", y="Coderre", z="Bergeron", color="winner", size="total", hover_name="district",
fig ="result", color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"})
symbol fig.show()