October 22, 2024
Chicago 12, Melborne City, USA
python

How to display axis tick labels with a detailed scientific notation offset?


I am trying to produce a figure with multiple subplots where the axis tick labels of each subplot should be formatting in the same way. As the numbers are large, displaying tick labels in a legible format is tricky. I like the matplotlib format of a detailed scientific offset b x 10^c (see plot below on right) where the displayed tick labels a can be then interpreted as (b+a) x 10^c.

However, I cannot find out how to control the use of this tick label formatter. It seems to me that matplotlib decides the use it very inconsistently.

This is my initial code which yields the subsequent plot:

import matplotlib.pyplot as plt

targets = [[-167297.54344731, -167301.10372634, -167308.44207031, -167312.11313287],[-145434.59446349, -145430.40561915, -145432.73985156, -145432.6785802]]
preds = [[-167298.46656051, -167300.59898357, -167308.72554459, -167311.71904965],[-145433.43693344, -145429.41508183, -145432.73929695, -145431.94648615]]

fig, axs = plt.subplots(nrows=1, ncols=2)

# For each subplot
for i in range(2):

    # make subplot square    
    min_max = [min(targets[i]+preds[i]), max(targets[i]+preds[i])]
    offset = 2
    axs[i].set_ylim(min_max[0]-offset, min_max[1]+offset)
    axs[i].set_xlim(min_max[0]-offset, min_max[1]+offset)
    axs[i].set_aspect("equal")
    
    axs[i].scatter(targets[i], preds[i])

Incorrect tick formatting on left and correct tick formatting on the right.

I have tried using matplotlib.ticker.ScalarFormatter which allows me to get the tick labels of the form 10^c instead of the desired b x 10^c. Setting axs[i].xaxis.set_major_locator(plt.MaxNLocator(4)) also seems to intermittently have an effect on whether or not the desired format is used.

So, my question is: how can I force the use of a tick label offset with a scientific notation format of b x 10^c?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video