2

I am trying to increase the fontsize of the scale tick in a matplotlib plot when using scientific notation for the tick labels.

import matplotlib.pyplot as plt 
import numpy as np 

x = np.linspace(0, 100, 100)
y = np.power(x, 3) 

plt.ticklabel_format(
                    axis="y",
                    style="sci",
                    scilimits=(0,0),
                    useMathText=True
)

plt.yticks(fontsize=30)
plt.xticks(fontsize=30)

plt.plot(x, y)
plt.show()

The above is a minimal example. As you can see, the fontsize of the (x10^6) is tiny and I would like it be the same size as the other ticks.

Minimal example

Sal
  • 41
  • 3
  • [This](https://stackoverflow.com/questions/34227595/how-to-change-font-size-of-the-scientific-notation-in-matplotlib) should help – jylls Feb 01 '22 at 05:02

2 Answers2

1

You could try using plt.rc('font', size=30) to set the font size of everything on the plot?

import matplotlib.pyplot as plt 
import numpy as np 

x = np.linspace(0, 100, 100)
y = np.power(x, 3) 

plt.ticklabel_format(
                    axis="y",
                    style="sci",
                    scilimits=(0,0),
                    useMathText=True
)

#set all font in plot to a given size
plt.rc('font', size=30)

plt.plot(x, y)
plt.show()

Jupyter-Notebook output

Dan
  • 103
  • 1
  • 8
  • Thanks for the suggestion but I'm pretty sure that doesn't work. I just tried it and it doesn't seem to change the font size at all. – Sal Jan 27 '22 at 15:46
  • Ah I did this in a jupyter-notebook - since that is an ipython kernal it may behave slightly differently than if you are running you program as a python script. Unfortunately I don't know the solution. – Dan Feb 01 '22 at 16:50
0

You need to change the size of the offset_text that belongs to the y axis:

ax = plt.gca()

txt = ax.yaxis.get_offset_text()
txt.set_fontsize('large')

Note this works also for colourbars.

ezatterin
  • 626
  • 5
  • 17