Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 42 ยท Layout, Styling and Themes

Axes, Ticks and Scales

Master Plotly axes: titles, ranges, log and date axes, tick values and formats, grid lines, rangebreaks for weekends, equal aspect ratio, reversed axes and a secondary y axis.

Axes turn numbers into positions, and they have more settings than almost any other part of a figure. Good axis settings make a chart honest and readable: the right range, sensible ticks, clear units, a log scale where it is needed.

Every figure has at least one x axis (layout.xaxis) and one y axis (layout.yaxis). Additional axes are numbered (xaxis2, yaxis2). You change them with fig.update_xaxes(...) and fig.update_yaxes(...), which apply to all axes of that direction, or with update_layout(xaxis=dict(...)) for one specific axis.

Titles, range and type

Property Meaning
title Axis title; title_text, title_font, title_standoff (gap to the ticks)
range [min, max] (dates as strings; log axes use the exponent, so [0, 3] means 1 to 1000)
autorange True (default), False, "reversed", "min", "max" and "min reversed" etc.
type "linear", "log", "date", "category", "multicategory"
rangemode "normal", "tozero" (include zero), "nonnegative"
fixedrange True disables zoom and pan on that axis
Python Range, log scale and reversed axes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For a log axis, range takes log10 of the values: [2.5, 5] shows roughly 316 to 100,000. This catches many people out. Use the log scale when values span several orders of magnitude, and when ratios matter more than differences. Zero and negative values cannot be shown on a log axis.

Ticks: positions, labels and formats

Ticks have three questions: where, what text, how formatted.

  • Where: tickmode="auto" (default), "linear" (with tick0 and dtick), or "array" (with tickvals and ticktext). nticks suggests how many.
  • What text: tickvals and ticktext give each tick a custom label. Prefixes and suffixes: tickprefix="$", ticksuffix=" kg".
  • How formatted: tickformat takes a d3 format string: ",.0f" (thousands separator), ".1%", "$,.2f", ".2s" (SI prefixes, 1.5k), and for dates "%b %Y".
  • Look: ticks="outside", ticklen, tickangle, showgrid, gridcolor, zeroline, showline, mirror=True.
Custom ticks, formats and a secondary y axis

The second y axis works through two settings: the trace gets yaxis="y2", and the layout defines yaxis2 with overlaying="y" and side="right". Dual axes let you show two measures with different units, but they can mislead, because the visual relationship depends on the chosen ranges. Label both clearly and match the trace colors to the axis titles.

The tick format codes are those of the d3 library:

Code Example result
",.0f" 1,234,567
".2f" 3.14
".1%" 12.5%
"$,.2f" $1,234.50
".3s" 1.23M
"%Y-%m-%d" 2025-03-01 (date axis)
"%b %d" Mar 01 (date axis)

Date axes

An axis becomes a date axis when its values are datetimes or ISO date strings. Tick labels adapt to the zoom level, and tickformat, dtick="M1" (one month), tick0 and tickformatstops (different formats per zoom level) control them. rangebreaks removes time that has no data, such as weekends and holidays, so that financial charts do not show gaps.

Python Date axis, ticks and rangebreaks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The data has no Saturdays or Sundays, but without rangebreaks the axis would still reserve space for them and draw the line with visible gaps. bounds=["sat", "mon"] removes Saturday through Monday 00:00. Other breaks: values=["2025-12-25"] for specific days, and pattern="hour" with bounds=[17, 9] for nights.

Equal aspect ratio and anchoring

For maps, images and geometry, one unit on x must be as long as one unit on y. Link the axes with scaleanchor:

Python
fig.update_yaxes(scaleanchor="x", scaleratio=1)

Express' px.imshow and some other functions do this automatically. With constrain="domain" the plot shrinks instead of stretching the other axis.

Axis lines, grids and spikes

  • showgrid, gridcolor, gridwidth, zeroline, zerolinecolor: grid and zero line.
  • showline=True, linecolor, mirror=True: frame around the plot.
  • showspikes=True draws spike lines to the axes when hovering, with spikemode="across" and spikethickness.
  • showticklabels=False hides labels, visible=False hides the whole axis.
  • categoryorder: "trace", "category ascending", "total descending" or "array" (with categoryarray).
  • automargin=True: lets the margin grow to fit long tick labels.
Python Tidy axis styling in one pass

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Entering real values in the range of a log axis (it needs exponents).
  • Truncating the y axis of a bar chart, which exaggerates differences.
  • Hiding the zero line or axis units, so that readers cannot tell the scale.
  • Using dual axes with ranges chosen to make two lines look correlated.
  • Forgetting automargin=True when category labels are long.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does range=[1, 3] mean on a log-type axis?

    On a log axis the range is given as exponents of 10: 10^1 = 10 and 10^3 = 1000.
  2. 2. Which tick property gives you custom labels for given positions?

    tickvals sets positions and ticktext sets the labels (with tickmode="array", which is implied).
  3. 3. What is rangebreaks used for?

    Rangebreaks hide time intervals so the chart is continuous across them.
  4. 4. How do you put a trace on a second y axis on the right?

    The trace references the axis by name; the layout declares it as overlaying the first one.
  5. 5. Which tickformat shows 0.125 as 12.5%?

    d3 format .1% multiplies by 100, uses one decimal and adds the percent sign.

Summary

  • Change axes with update_xaxes and update_yaxes; set title, range, type (linear, log, date, category) and rangemode.
  • Control ticks with tickmode, tickvals/ticktext, tickformat (d3 formats), prefixes and suffixes.
  • Date axes support dtick, tickformatstops, rangebreaks, range slider and selector.
  • scaleanchor gives equal scales, yaxis2 with overlaying gives a secondary axis, automargin prevents clipping.

Next: Legends, Fonts and Hover Labels