Styling Plotly Express Figures in Python

Figures made with Plotly Express can be customized in all the same ways as figures made with graph objects, as well as with PX-specific function arguments.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now.

Styling Figures made with Plotly Express

Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data. Every Plotly Express function returns a plotly.graph_objects.Figure object whose data and layout has been pre-populated according to the provided arguments.

You can style and customize figures made with Plotly Express in all the same ways as you can style figures made more manually by explicitly assembling graph_objects into a figure.

More specifically, here are the 4 ways you can style and customize figures made with Plotly Express:

  1. Control common parameters like width & height, titles, labeling and colors using built-in Plotly Express function arguments
  2. Updating the figure attributes using update methods or by directly setting attributes
  3. Using Plotly's theming/templating mechanism via the template argument to every Plotly Express function
  4. Setting default values for common parameters using px.defaults

Built-in Plotly Express Styling Arguments

Many common styling options can be set directly in the px function call. Every Plotly Express function accepts the following arguments:

  • title to set the figure title
  • width and height to set the figure dimensions
  • template to set many styling parameters at once (see below for more details)
  • labels to override the default axis and legend labels behaviour, which is to use the data frame column name if available, and otherwise to use the label name itself like "x", "y", "color" etc. labels accepts a dict whose keys are the label to rename and whose values are the desired labels. These labels appear in axis labels, legend and color bar titles, and in hover labels.
  • category_orders to override the default category ordering behaviour, which is to use the order in which the data appears in the input. category_orders accepts a dict whose keys are the column name to reorder and whose values are a list of values in the desired order. These orderings apply everywhere categories appear: in legends, on axes, in bar stacks, in the order of facets, in the order of animation frames etc.
  • hover_data and hover_name to control which attributes appear in the hover label and how they are formatted.
  • Various color-related attributes such as color_continuous_scale, color_range, color_discrete_sequence and/or color_discrete_map set the colors used in the figure. color_discrete_map accepts a dict whose keys are values mapped to color and whose values are the desired CSS colors.

To illustrate each of these, here is a simple, default figure made with Plotly Express. Note the default orderings for the x-axis categories and the usage of lowercase & snake_case data frame columns for axis labelling.

In [1]:
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="day", y="total_bill", color="sex")
fig.show()

Here is the same figure, restyled by adding some extra parameters to the initial Plotly Express call:

In [2]:
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="day", y="total_bill", color="sex",
            title="Receipts by Payer Gender and Day of Week",
            width=600, height=400,
            labels={ # replaces default labels by column name
                "sex": "Payer Gender",  "day": "Day of Week", "total_bill": "Receipts"
            },
            category_orders={ # replaces default order by column name
                "day": ["Thur", "Fri", "Sat", "Sun"], "sex": ["Male", "Female"]
            },
            color_discrete_map={ # replaces default color mapping by value
                "Male": "RebeccaPurple", "Female": "MediumPurple"
            },
            template="simple_white"
            )
fig.show()

Updating or Modifying Figures made with Plotly Express

If none of the built-in Plotly Express arguments allow you to customize the figure the way you need to, you can use the update_* and add_* methods on the plotly.graph_objects.Figure object returned by the PX function to make any further modifications to the figure. This approach is the one used throughout the Plotly.py documentation to customize axes, control legends and colorbars, add shapes and