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:
- 3D axes The axis object is
go.layout.Scene - Polar axes. The axis object is
go.layout.Polar - Ternary axes. The axis object is
go.layout.Ternary - Geo axes. The axis object is
go.layout.Geo - Map axes. The axis object is
go.layout.Map - Color axes. The axis object is
go.layout.Coloraxis.
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:
'linear'as described in this page'log'(see the log plot tutorial)'date'(see the tutorial on timeseries)'category'(see the categorical axes tutorial)'multicategory'(see the categorical axes tutorial)
The axis type is auto-detected by looking at data from the first trace linked to this axis:
- First check for
multicategory, thendate, thencategory, else default tolinear(logis never automatically selected) multicategoryis just a shape test: is the array nested?dateandcategory: 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.
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.
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:
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:
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.
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.
import plotly.express as px
df = px.data.stocks(indexed=