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.

import plotly.express as px

df = px.data.gapminder().query("year==2007")
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
px.strip(df, x='lifeExp', hover_name="country")
px.strip(df, x='lifeExp', color="continent", hover_name="country")
px.histogram(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.bar(df, color='lifeExp', x="pop", y="continent", hover_name="country")
px.sunburst(df, color='lifeExp', values="pop", path=["continent", "country"], hover_name="country", height=500)
px.treemap(df, color='lifeExp', values="pop", path=["continent", "country"], hover_name="country", height=500)
px.choropleth(df, color='lifeExp', locations="iso_alpha", hover_name="country", height=500)
px.scatter(df, x="gdpPercap", y='lifeExp', hover_name="country", height=500)
px.scatter(df, x="gdpPercap", y='lifeExp', hover_name="country", color="continent",size="pop", height=500)

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.

px.scatter(df, x="gdpPercap", y='lifeExp', hover_name="country", color="continent",size="pop", size_max=60, log_x=True, height=500)
fig = px.scatter(df, x="gdpPercap", y='lifeExp', hover_name="country", color="continent",size="pop", size_max=60, log_x=True, height=500)

This will allow you to inspect the values for each of these cells, unfortunately this is a great deal easier to see in JupyterLab.

fig.show("json")
Unable to display output for mime type(s): application/json
import plotly.express as px

df = px.data.gapminder().query("year == 2007")

fig = px.scatter(df, x="gdpPercap",y="lifeExp", color="continent", log_x=True, size="pop", size_max=60,
                 hover_name="country", height=600, width=1000, template="simple_white", 
                 color_discrete_sequence=px.colors.qualitative.G10,
                 title="Health vs Wealth 2007",
                 labels=dict(
                     continent="Continent", pop="Population",
                     gdpPercap="GDP per Capita (US$, price-adjusted)", 
                     lifeExp="Life Expectancy (years)"))

fig.update_layout(font_family="Rockwell",
                  legend=dict(orientation="h", title="", y=1.1, x=1, xanchor="right", yanchor="bottom"))
fig.update_xaxes(tickprefix="$", range=[2,5], dtick=1)
fig.update_yaxes(range=[30,90])
fig.add_hline((df["lifeExp"]*df["pop"]).sum()/df["pop"].sum(), line_width=1, line_dash="dot")
fig.add_vline((df["gdpPercap"]*df["pop"]).sum()/df["pop"].sum(), line_width=1, line_dash="dot")
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

df_animation = px.data.gapminder()

anim_fig = px.scatter(df_animation, x="gdpPercap", y="lifeExp",
                      title="Health vs Wealth from 1952 to 2007",
                      labels=dict(continent="Continent", pop="Population", gdpPercap="GDP per Capita (US$, price-adjusted)", lifeExp="Life Expectancy (years)"),
                      animation_frame="year", animation_group="country",
                      size="pop",
                      color="continent",
                      hover_name="country",
                      height=600,width=1000,
                      template="simple_white",
                      color_discrete_sequence=px.colors.qualitative.G10,
                      log_x=True,
                      size_max=60,
                      range_x=[100,100000],
                      range_y=[25,90])

anim_fig.update_layout(font_family="Rockwell",
                  legend=dict(orientation="h", title="", y=1.1, x=1, xanchor="right", yanchor="bottom"))
anim_fig.update_xaxes(tickprefix="$", range=[2,5], dtick=1)
anim_fig.write_html("gapminder_animation.html", auto_play=False) # You're able to export this animation.
px.defaults.height=600
import plotly.express as px

z = [[.1, .3, .5, .7, .9],
     [1, .8, .6, .4, .2],
     [.2, 0, .5, .7, .9],
     [.9, .8, .4, .2, 0],
     [.3, .4, .5, .7, 1]]

fig = px.imshow(z, text_auto=True)
fig.show()
import plotly.express as px
df = px.data.wind()
fig = px.bar_polar(df, r="frequency", theta="direction", height=600,
                   color="strength", template="plotly_dark",
                   color_discrete_sequence= px.colors.sequential.Plasma_r)
fig.show()
df = px.data.election()
fig = px.scatter_ternary(df, a="Joly", b="Coderre", c="Bergeron", color="winner", size="total", hover_name="district",
                   size_max=15, color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"} )
fig.show()
df = px.data.election()
fig = px.scatter_3d(df, x="Joly", y="Coderre", z="Bergeron", color="winner", size="total", hover_name="district",
                  symbol="result", color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"})
fig.show()