Matplotlib: disable powers of ten in log plot
Asked Answered
G

2

6

Is there a simple way to make matplotlib not show the powers of ten in a log plot, and instead just show the numbers? I.e., instead of [10^1, 10^2, 10^3] display [10, 100, 1000]? I don't want to change the tickmark locations, just want to get rid of the powers of ten.

This is what I currently have: enter image description here

I can change the labels themselves via xticks, however I then get mismatching fonts or sizes for the y tick labels. I am using TeX for this text. I've tried the following:

xx, locs = xticks()
ll = [r'\rm{%s}' % str(a) for a in xx]
xticks(xx, ll)

This gives the following result: enter image description here

In this particular case, I could use the same LaTeX roman font, but the sizes and looks are different to those in the y axis. Plus, if I used a different LaTeX font in matplotlib this is going to be problematic.

Is there a more flexible way of switching off the power of ten notation?

Grommet answered 30/11, 2012 at 1:25 Comment(0)
H
11

Use a ScalarFormatter:

from matplotlib import rc
rc('text', usetex=True)
rc('font', size=20)

import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111)
ax.semilogx(range(100))
ax.xaxis.set_major_formatter(ScalarFormatter())
plt.show()

enter image description here

Hestia answered 30/11, 2012 at 5:58 Comment(0)
S
0

With newer matplotlib version, use the plt.FuncFormatter.

For example:

def format_func(value, tick_number):
    return '%g' % (value)
ax.xaxis.set_major_formatter(plt.FuncFormatter(format_func))
Sailfish answered 16/4 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.