|
| 1 | +--- |
| 2 | +jupyter: |
| 3 | + jupytext: |
| 4 | + notebook_metadata_filter: all |
| 5 | + text_representation: |
| 6 | + extension: .md |
| 7 | + format_name: markdown |
| 8 | + format_version: '1.2' |
| 9 | + jupytext_version: 1.3.1 |
| 10 | + kernelspec: |
| 11 | + display_name: Python 3 |
| 12 | + language: python |
| 13 | + name: python3 |
| 14 | + language_info: |
| 15 | + codemirror_mode: |
| 16 | + name: ipython |
| 17 | + version: 3 |
| 18 | + file_extension: .py |
| 19 | + mimetype: text/x-python |
| 20 | + name: python |
| 21 | + nbconvert_exporter: python |
| 22 | + pygments_lexer: ipython3 |
| 23 | + version: 3.6.8 |
| 24 | + plotly: |
| 25 | + description: Plotly Express is a terse, consistent, high-level API for rapid data |
| 26 | + exploration and figure generation. |
| 27 | + display_as: file_settings |
| 28 | + language: python |
| 29 | + layout: base |
| 30 | + name: Plotly Express |
| 31 | + order: 29 |
| 32 | + page_type: u-guide |
| 33 | + permalink: python/styling-plotly-express/ |
| 34 | + thumbnail: thumbnail/plotly-express.png |
| 35 | +--- |
| 36 | + |
| 37 | +### Styling Figures made with Plotly Express |
| 38 | + |
| 39 | +[Plotly Express](/python/plotly-express/) is the easy-to-use, high-level interface to Plotly, which [operates on "tidy" data](/python/px-arguments/). Every Plotly Express function returns a `graph_objects.Figure` object whose `data` and `layout` has been pre-populated according to the provided arguments. |
| 40 | + |
| 41 | +> 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. |
| 42 | +
|
| 43 | +More specifically, here are the 4 ways you can style and customize figures made with Plotly Express: |
| 44 | + |
| 45 | +1. Control common parameters like width & height, titles, labeling and colors using built-in Plotly Express function arguments |
| 46 | +2. Updating the figure attributes using update methods or by directly setting attributes |
| 47 | +3. Using Plotly's theming/templating mechanism via the `template` argument to every Plotly Express function |
| 48 | +4. Setting default values for common parameters using `px.defaults` |
| 49 | + |
| 50 | +### Built-in Plotly Express Styling Arguments |
| 51 | + |
| 52 | +Many common styling options can be set directly in the `px` function call. Every Plotly Express function accepts the following arguments: |
| 53 | + |
| 54 | +- `title` to set the figure title |
| 55 | +- `width` and `height` to set the figure dimensions |
| 56 | +- `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. |
| 57 | +- `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. |
| 58 | +- [Various color-related attributes](/python/colorscales/) 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. |
| 59 | +- `template` to [set many styling parameters at once](/python/templates/) (see below for more details) |
| 60 | + |
| 61 | +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. |
| 62 | + |
| 63 | +```python |
| 64 | +import plotly.express as px |
| 65 | +df = px.data.tips() |
| 66 | +fig = px.histogram(df, x="day", y="total_bill", color="sex", histfunc="sum") |
| 67 | +fig.show() |
| 68 | +``` |
| 69 | + |
| 70 | +Here is the same figure, restyled by adding some extra parameters to the initial Plotly Express call: |
| 71 | + |
| 72 | +```python |
| 73 | +import plotly.express as px |
| 74 | +df = px.data.tips() |
| 75 | +fig = px.histogram(df, x="day", y="total_bill", color="sex", histfunc="sum", |
| 76 | + title="Receipts by Payer Gender and Day of Week", |
| 77 | + width=600, height=400, |
| 78 | + labels={ # replaces default labels by column name |
| 79 | + "sex": "Payer Gender", "day": "Day of Week", "total_bill": "Receipts" |
| 80 | + }, |
| 81 | + category_orders={ # replaces default order by column name |
| 82 | + "day": ["Thur", "Fri", "Sat", "Sun"], "sex": ["Male", "Female"] |
| 83 | + }, |
| 84 | + color_discrete_map={ # replaces default color mapping by value |
| 85 | + "Male": "RebeccaPurple", "Female": "MediumPurple" |
| 86 | + }, |
| 87 | + template="simple_white" |
| 88 | + ) |
| 89 | +fig.show() |
| 90 | +``` |
| 91 | + |
| 92 | +### Updating or Modifying Figures made with Plotly Express |
| 93 | + |
| 94 | +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 `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](/python/axes/), control [legends](/python/legend/) and [colorbars](/python/colorscales/), add [shapes](/python/shapes/) and [annotations](/python/text-and-annotations/) etc. |
| 95 | + |
| 96 | +Here is the same figure as above, with some additional customizations to the axes and legend via `.update_yaxes()`, and `.update_layout()`, as well as some annotations added via `.add_shape()` and `.add_annotation()`. |
| 97 | + |
| 98 | +```python |
| 99 | +import plotly.express as px |
| 100 | +df = px.data.tips() |
| 101 | +fig = px.histogram(df, x="day", y="total_bill", color="sex", histfunc="sum", |
| 102 | + title="Receipts by Payer Gender and Day of Week vs Target", |
| 103 | + width=600, height=400, |
| 104 | + labels={"sex": "Payer Gender", "day": "Day of Week", "total_bill": "Receipts"}, |
| 105 | + category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "sex": ["Male", "Female"]}, |
| 106 | + color_discrete_map={"Male": "RebeccaPurple", "Female": "MediumPurple"}, |
| 107 | + template="simple_white" |
| 108 | + ) |
| 109 | + |
| 110 | +fig.update_yaxes( # the y-axis is in dollars |
| 111 | + tickprefix="$", showgrid=True |
| 112 | +) |
| 113 | + |
| 114 | +fig.update_layout( # customize font and legend orientation & position |
| 115 | + font_family="Rockwell", |
| 116 | + legend=dict( |
| 117 | + title=None, orientation="h", y=1, yanchor="bottom", x=0.5, xanchor="center" |
| 118 | + ) |
| 119 | +) |
| 120 | + |
| 121 | +fig.add_shape( # add a horizontal "target" line |
| 122 | + type="line", line_color="salmon", line_width=3, opacity=1, line_dash="dot", |
| 123 | + x0=0, x1=1, xref="paper", y0=950, y1=950, yref="y" |
| 124 | +) |
| 125 | + |
| 126 | +fig.add_annotation( # add a text callout with arrow |
| 127 | + text="below target!", x="Fri", y=400, arrowhead=1, showarrow=True |
| 128 | +) |
| 129 | + |
| 130 | +fig.show() |
| 131 | +``` |
| 132 | + |
| 133 | +### How Plotly Express Works with Templates |
| 134 | + |
| 135 | +Plotly has a [theming system based on templates](/python/templates/) and figures created with Plotly Express interact smoothly with this system: |
| 136 | + |
| 137 | +* Plotly Express methods will use the default template if one is set in `plotly.io` (by default, this is set to `plotly`) or in `plotly.express.defaults` (see below) |
| 138 | +* The template in use can always be overridden via the `template` argument to every PX function |
| 139 | +* The default `color_continuous_scale` will be the value of `layout.colorscales.sequential` in the template in use, unless it is overridden via the corresponding function argument or via `plotly.express.defaults` (see below) |
| 140 | +* The default `color_discrete_sequence` will be the value of `layout.colorway` in the template in use, unless it is overridden via the corresponding function argument or via `plotly.express.defaults` (see below) |
| 141 | + |
| 142 | +By way of example, in the following figure, simply setting the `template` argument will automatically change the default continuous color scale, even though we have not specified `color_continuous_scale` directly. |
| 143 | + |
| 144 | +```python |
| 145 | +import plotly.express as px |
| 146 | +df = px.data.iris() |
| 147 | +fig = px.density_heatmap(df, x="sepal_width", y="sepal_length", template="seaborn") |
| 148 | +fig.show() |
| 149 | +``` |
| 150 | + |
| 151 | +### Setting Plotly Express Styling Defaults |
| 152 | + |
| 153 | +Plotly Express supports a simple default-configuration system via the `plotly.express.defaults` singleton object. The values of the properties set on this object are used for the rest of the active session in place of `None` as the default values for any argument to a PX function with a matching name: |
| 154 | + |
| 155 | +* `width` and `height` can be set once globally for all Plotly Express functions |
| 156 | +* `template` can override the setting of `plotly.io.templates.default` for all Plotly Express functions |
| 157 | +* `color_continuous_scale` and `color_discrete_scale` can override the contents of the template in use for all Plotly Express functions that accept these arguments |
| 158 | +* `line_dash_sequence`, `symbol_sequence` and `size_max` can be set once globally for all Plotly Express functions that accept these arguments |
| 159 | + |
| 160 | +To illustrate this "defaults hierarchy", in the following example: |
| 161 | +* we set the Plotly-wide default template to `simple_white`, but |
| 162 | +* we override the default template for Plotly Express to be `ggplot2`, but |
| 163 | +* we also set the default `color_continuous_scale`, and |
| 164 | +* we set the default `height` and `width` to 400 by 600, but |
| 165 | +* we override the default `width` to 400 via the function argument. |
| 166 | + |
| 167 | +As a result, any figure produced with Plotly Express thereafter uses the `ggplot2` settings for all attributes except for the continuous color scale (visible because `simple_white` doesn't set a plot background, and neither the `simple_white` nor `ggplot2` template uses `Blackbody` as a color scale), and uses the Plotly Express defaults for height but not width (visible because the figure height is the same as the figure width, despite the default). |
| 168 | + |
| 169 | +```python |
| 170 | +import plotly.express as px |
| 171 | +import plotly.io as pio |
| 172 | + |
| 173 | +pio.templates.default = "simple_white" |
| 174 | + |
| 175 | +px.defaults.template = "ggplot2" |
| 176 | +px.defaults.color_continuous_scale = px.colors.sequential.Blackbody |
| 177 | +px.defaults.width = 600 |
| 178 | +px.defaults.height = 400 |
| 179 | + |
| 180 | +df = px.data.iris() |
| 181 | +fig = px.scatter(df, x="sepal_width", y="sepal_length", color="sepal_length", width=400) |
| 182 | +fig.show() |
| 183 | +``` |
0 commit comments