Chart styling#
This section discusses chart styling that is common across chart types.
Important note about styling
The purpose of styling a chart is to make it easier to read and understand. It is important to remember that the chart is a tool to communicate information. If the styling makes it harder to understand the information, then it is not good styling. The examples on this page are not good styling. They have been intentionally over-styled to demonstrate the options available. As a rule of thumb, if you are not sure whether to use a particular styling option, then err on the side of simplicity.
Let’s start with our stock data from plotly express.
import plotly.express as px
df_stocks = px.data.stocks()
df_stocks
date | GOOG | AAPL | AMZN | FB | NFLX | MSFT | |
---|---|---|---|---|---|---|---|
0 | 2018-01-01 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 |
1 | 2018-01-08 | 1.018172 | 1.011943 | 1.061881 | 0.959968 | 1.053526 | 1.015988 |
2 | 2018-01-15 | 1.032008 | 1.019771 | 1.053240 | 0.970243 | 1.049860 | 1.020524 |
3 | 2018-01-22 | 1.066783 | 0.980057 | 1.140676 | 1.016858 | 1.307681 | 1.066561 |
4 | 2018-01-29 | 1.008773 | 0.917143 | 1.163374 | 1.018357 | 1.273537 | 1.040708 |
... | ... | ... | ... | ... | ... | ... | ... |
100 | 2019-12-02 | 1.216280 | 1.546914 | 1.425061 | 1.075997 | 1.463641 | 1.720717 |
101 | 2019-12-09 | 1.222821 | 1.572286 | 1.432660 | 1.038855 | 1.421496 | 1.752239 |
102 | 2019-12-16 | 1.224418 | 1.596800 | 1.453455 | 1.104094 | 1.604362 | 1.784896 |
103 | 2019-12-23 | 1.226504 | 1.656000 | 1.521226 | 1.113728 | 1.567170 | 1.802472 |
104 | 2019-12-30 | 1.213014 | 1.678000 | 1.503360 | 1.098475 | 1.540883 | 1.788185 |
105 rows × 7 columns
Templates#
Plotly provides several different chart templates by default. We can choose from the following:
plotly
, plotly_white
, plotly_dark
, ggplot2
, seaborn
, simple_white
, none
Documentation:
https://plotly.com/python/templates/
Here we have the standard line chart template.
stock_fig = px.line(
data_frame=df_stocks,
x='date',
y=['GOOG', 'AAPL'],
)
stock_fig
We can change the style by using the template=
option in the chart.
For example, the simple_white
and plotly_dark
templates are shown below.
# simple_white template
stock_fig = px.line(
data_frame=df_stocks,
x='date',
y=['GOOG', 'AAPL'],
template='simple_white',
)
stock_fig
# plotly_dark template
stock_fig = px.line(
data_frame=df_stocks,
x='date',
y=['GOOG', 'AAPL'],
template='plotly_dark',
)
stock_fig
Plot size#
We can manually change the size of the chart using the width
and height
options.
stock_fig = px.line(
data_frame=df_stocks,
x='date',
y='NFLX',
width=500,
height=500,
template='plotly_dark'
)
stock_fig
Plot margins#
Margins control how much space is between the edge of the chart and the edge of the container. In order to change the margins, we need to use the .update_layout()
method and pass a dictionary to the margin
option. The dictionary should specify the margin (in pixels) for the top, bottom, left, and right.
Let’s continue using the dark theme since it makes it easier to visualize the edges.
stock_fig = px.line(
data_frame=df_stocks,
x='date',
y=['GOOG', 'AAPL'],
template='plotly_dark'
)
# setting all 4 margins
stock_fig.update_layout(
margin=dict(t=50, b=0, l=200, r=300)
)
stock_fig
Titles#
Documentation:
https://plotly.com/python/reference/layout/#layout-title
Now let’s add a title to our stock chart.
The easiest way to do this is by using the title=
option in the chart — in this example, px.line()
.
stock_fig = px.line(
data_frame=df_stocks,
x='date',
y=['GOOG', 'AAPL'],
template='plotly_dark',
title='The value of a $1 investment',
)
stock_fig
Again using the .update_layout()
method, we can edit the title properties.
stock_fig.update_layout(
# update the title text
title_text='The Value of $1',
# change the font color
title_font_color='dimgray',
# change the font family
title_font_family='Arial',
# change the font size
title_font_size=24,
# center the title; can take a value between 0 and 1
title_x=0.5,
# sets whether the alignment is relative to the 'paper' or the 'container'
# in this case, 'paper' centers the title above the chart,
# while 'container' centers the title in the container.
# this is most easily seen with a heavy margin on one side
title_xref='paper',
)
stock_fig
As of April 2022, there is not a convenient way to add a subtitle. Instead you must use an “annotation” work around.
Axes#
Documentation:
https://plotly.com/python/axes/
https://plotly.com/python/reference/layout/xaxis/
https://plotly.com/python/reference/layout/yaxis/
Back to our original starting point:
# the default
stock_fig = px.line(
data_frame=df_stocks,
x='date',
y=['GOOG', 'AAPL'],
)
stock_fig
We can use .update_yaxes()
and .update_xaxes()
to change a variety of parameters.
# update the Y axis
stock_fig.update_yaxes(
# put the tick label inside the chart and above the line
ticklabelposition='inside top',
# no title
title=None,
# put a dollar sign in front of the values
tickprefix='$',
# formatted with two decimal points
tickformat='.2f',
# manually set the range
range=[0.5, 2],
# mark the y-axis with a line
showline=True,
linewidth=1,
linecolor='black',
)
# update the X axis
stock_fig.update_xaxes(
# new title
title='Date',
# title font properties
title_font={
'size': 24,
'color': 'firebrick',
},
# tick font properties
tickfont={
'size': 16,
'color': 'darkgrey',
},
# the angle of the ticks
tickangle=15,
# the date format of the ticks
tickformat="%b '%y",
# mark the x-axis with a line
showline=True,
linewidth=1,
linecolor='black',
)
stock_fig
Background color and grid#
Documentation:
https://plotly.com/python/axes/#axis-lines-grid-and-zerolines
# the default
stock_fig = px.line(
data_frame=df_stocks,
x='date',
y=['GOOG', 'AAPL'],
)
stock_fig
# the default
stock_fig = px.line(
data_frame=df_stocks,
x='date',
y=['GOOG', 'AAPL'],
)
# turn off the x axis grid
stock_fig.update_xaxes(
showgrid=False,
)
stock_fig.update_yaxes(
# set the width in pixels for the gridlines
gridwidth=3,
# set the grid color
gridcolor='green',
)
# set the background color for the 'paper' and the 'plot'
stock_fig.update_layout(
paper_bgcolor='lightblue',
plot_bgcolor='grey',
)
stock_fig
It is also possible to make the backgrounds transparent.
This might be a bad idea, depending on how you plan to use the chart.
# the default
stock_fig = px.line(
data_frame=df_stocks,
x='date',
y=['GOOG', 'AAPL'],
)
# make both bg transparent
# it is the "a" (alpha) in "rgba" that does this
stock_fig.update_layout(
paper_bgcolor='rgba(0, 0, 0, 0)',
plot_bgcolor='rgba(0, 0, 0, 0)',
)
stock_fig
Legend#
Documentation:
https://plotly.com/python/legend/
https://plotly.com/python/reference/layout/#layout-legend
You can click on legend items to show/hide items.
stock_fig = px.line(
data_frame=df_stocks,
x='date',
y=['GOOG', 'AAPL', 'FB'],
)
stock_fig.update_layout(
# no legend title
legend_title=None,
# legend items in rows (h) or columns (v)
legend_orientation='h',
# reference the center of the legend in x dimension for positioning
legend_xanchor='center',
# put the legend in the center in the x dimension
legend_x=0.5,
# reference the top of the legend in y dimension for positioning
legend_yanchor='top',
# put the legend near the top in the y dimension
legend_y=0.98,
# set the background color and border properties
legend_bgcolor='lightgrey',
legend_bordercolor='black',
legend_borderwidth=2,
)
stock_fig