Axes in Python

How to adjust axes properties in Python - axes titles, styling and coloring axes and grid lines, ticks, tick labels and more.


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

This tutorial explains how to set the properties of 2-dimensional Cartesian axes, namely go.layout.XAxis and go.layout.YAxis.

Other kinds of subplots and axes are described in other tutorials:

See also the tutorials on facet plots, subplots and multiple axes.

2-D Cartesian Axis Types and Auto-Detection

The different types of Cartesian axes are configured via the xaxis.type or yaxis.type attribute, which can take on the following values:

The axis type is auto-detected by looking at data from the first trace linked to this axis:

  • First check for multicategory, then date, then category, else default to linear (log is never automatically selected)
  • multicategory is just a shape test: is the array nested?
  • date and category: require more than twice as many distinct date or category strings as distinct numbers in order to choose that axis type.
    • Both of these test an evenly-spaced sample of at most 1000 values

Forcing an axis to be categorical

It is possible to force the axis type by setting explicitly xaxis_type. In the example below the automatic X axis type would be linear (because there are not more than twice as many unique strings as unique numbers) but we force it to be category.

In [1]:
import plotly.express as px
fig = px.bar(x=["a", "a", "b", 3], y = [1,2,3,4])
fig.update_xaxes(type='category')
fig.show()

General Axis properties

The different groups of Cartesian axes properties are

  • title of the axis
  • tick values (locations of tick marks) and tick labels. Tick labels and grid lines are placed at tick values.
  • lines: grid lines (passing through tick values), axis lines, zero lines
  • range of the axis
  • domain of the axis

The examples on this page apply to axes of any type, but extra attributes are available for axes of type category and axes of type date.

Set and Style Axes Title Labels

Set axis title text with Plotly Express

Axis titles are automatically set to the column names when using Plotly Express with a data frame as input.

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

Axis titles (and legend titles) can also be overridden using the labels argument of Plotly Express functions:

In [3]:
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="sex",
    labels=dict(total_bill="Total Bill ($)", tip="Tip ($)", sex="Payer Gender")
)
fig.show()

The PX labels argument can also be used without a data frame argument:

In [4]:
import plotly.express as px
fig = px.bar(x=["Apples", "Oranges"], y=[10,20], color=["Here", "There"],
    labels=dict(x="Fruit", y="Amount", color="Place")
)
fig.show()
Rotating tick labels in Dash

Dash is the best way to build analytical apps in Python using Plotly figures. To run the app below, run pip install dash, click "Download" to get the code and run python app.py.

Get started with the official Dash docs and learn how to effortlessly style & publish apps like this with Dash Enterprise or Plotly Cloud.

Out[5]:

Sign up for Dash Club → Free cheat sheets plus updates from Chris Parmer and Adam Schroeder delivered to your inbox every two months. Includes tips and tricks, community apps, and deep dives into the Dash architecture. Join now.

Moving Tick Labels Inside the Plot

The ticklabelposition attribute moves tick labels inside the plotting area, and modifies the auto-range behaviour to accommodate the labels.

In [6]:
import plotly.express as px

df = px.data.stocks(indexed=