Plotly Express

Plotly Express is a Plotly module with tools which simplify creation of many types of plots. It is especially useful for visualizing data coming from Pandas dataframes.

[2]:
import plotly.express as px

Plotly Express includes sample sets of data which will use here to illustrate its features. The function px.data.tips() returns a dataframe with data on restaurant visits: the total bill amount, tip amount, sex of the person paying the bill, whether the visiting group included smokers, day and time (dinner or lunch) of the visit, and the size of the group:

[3]:
tips = px.data.tips()
tips.head()
[3]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

Plots from dataframes

In order to produce a scatter plot showing the bill amount on the \(x\) axis and the tip amount on the \(y\) axis we just need to specify the dataframe and names of the corresponding columns:

[4]:
fig = px.scatter(
                 tips,            # dataframe
                 x="total_bill",  # x-values column
                 y="tip",         # y-values column
                 title="Restaurant data")
fig.show()

We can use the values in the “day” column to assign marker colors:

[5]:
fig = px.scatter(
                 tips,
                 x="total_bill",
                 y="tip",
                 color="day",  # assign colors based on values of this column
                 title="Restaurant data")
fig.show()

By clicking on a color dot in the legend, we can hide markers of that color in the plot. Double clicking on a dot in the legend shows markers of the specified color only.

Next, we set different marker sizes based on values in the “size” column:

[6]:
fig = px.scatter(
                 tips,
                 x="total_bill",
                 y="tip",
                 color="day",
                 size="size",  # assign marker sizes based on values of this column
                 title="Restaurant data")
fig.show()

We can also split the plot into subplots based on values of some column. Below we create two subplots, each displaying data for a different value of the “time” column:

[7]:
fig = px.scatter(
                 tips,
                 x="total_bill",
                 y="tip",
                 color="day",
                 size="size",
                 facet_row="time",  # rows of subplots depending on "time" value
                 title="Restaurant data")
fig.show()

We can subdivide the plot even further using values of the “sex” column:

[8]:
fig = px.scatter(
                 tips,
                 x="total_bill",
                 y="tip",
                 color="day",
                 size="size",
                 facet_row="time",  # rows of subplots depending on "time" value
                 facet_col="sex",   # columns of subplots depending on "sex" value
                 title="Restaurant data")
fig.show()

Numerical data can be also visualized on a 3-dimensional scatter plot, with column values providing \(x\), \(y\), and \(z\) coordinates:

[9]:
fig = px.scatter_3d(tips,
                    x="total_bill",
                    y="tip",
                    z="size",
                    color = "day")
fig.show()

While all above examples show scatter plots only, Plotly Express can be used to create many other types of plots. See the documentation for details.

Customizing plots

Plotly Express functions return Plotly figure objects, which can be customized using other Plotly tools. For example, we can use the fig.add_trace() method to add additional traces a figure:

[10]:
import plotly.graph_objects as go

# create a figure using Plotly Express
fig = px.scatter(tips, x="total_bill", y="tip", color="day", size="size")

# create a trace
line = go.Scatter(x=[10, 50],
                  y=[2, 10],
                  mode="lines",
                  line_color='black',
                  line_width=10,
                  opacity=0.5)

# add trace to the figure
fig.add_trace(line)
fig.show()

We can use fig.update_layout() method to change the figure layout:

[11]:
# create a figure using Plotly Express
fig = px.scatter(tips, x="total_bill", y="tip", color="day", size="size")

# modify layout of the figure
fig.update_layout(title_text="<b>Restaurant data</b>",
                  title_font_family="Courier New",
                  title_font_size=30,
                  title_font_color="cornflowerblue")
fig.show()

We can also apply the method fig.update_traces() to modify traces included in the figure. Below we use it to change colors of markers. The selector argument indicates which markers should be affected by the change. For example, the code

fig.update_traces(marker_color="DarkOrange", selector={"marker_color" : '#EF553B'})

selects all markers with color ‘#EF553B’, and sets their color to “DarkOrange”. Hexadecimal codes of colors appearing in the original figure can be obtained e.g. by using fig.to_dict() to get a dictionary describing the figure, and then inspecting the dictionary.

[12]:
# create a figure using Plotly Express
fig = px.scatter(tips, x="total_bill", y="tip", color="day", size="size")

# change colors of markers
fig.update_traces(marker_color="DarkOrange", selector={"marker_color": '#EF553B'})
fig.update_traces(marker_color="RoyalBlue", selector={"marker_color": '#00cc96'})
fig.update_traces(marker_color="Chartreuse", selector={"marker_color": '#ab63fa'})
fig.update_traces(marker_color="DeepPink", selector={"marker_color": '#636efa'})