1

I'm using the mathplot library from Python to illustrate some data, and in order to present it for a paper I'm looking to increase its readability by increasing the size of the axis labels and ticks.

However, I've only been able to modify only the size of the text for the legends, and no matter how much I change the value for the text size for the labels and ticks, they remain the same.

I've tried the following solutions I found on this website, but none seem to work (using the standard import mathplotlib.plytplot as plt):

#Attempt #1
params = {'axes.labelsize': 20,'axes.titlesize':20, 'legend.fontsize': 20, 'xtick.labelsize': 20, 'ytick.labelsize': 20}
plt.rcParams.update(params)

################################################

#Attempt #2
MEDIUM_SIZE = 16
BIGGER_SIZE = 12

font = {'family' : 'Arial','weight' : 'bold', 'size'   : 22}

plt.rc('font', **font)
plt.rc('font', size=MEDIUM_SIZE)          # controls default text sizes
plt.rc('axes', titlesize=MEDIUM_SIZE)     # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE)    # fontsize of the x and y labels
plt.rc('xtick', labelsize=MEDIUM_SIZE)    # fontsize of the tick labels
plt.rc('ytick', labelsize=MEDIUM_SIZE)    # fontsize of the tick labels
plt.rc('legend', fontsize=BIGGER_SIZE)    # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title

################################################

#Attempt #3
plt.rcParams['axes.linewidth'] = 20.0
plt.rcParams['xtick.labelsize']=8
plt.rcParams['ytick.labelsize']=8

################################################

#Attempt #4
ax = plt.subplot(111, xlabel='x', ylabel='y', title='title')
for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
         ax.get_xticklabels() + ax.get_yticklabels()):
item.set_fontsize(20)

I expected the size of the labels and the ticks to change by modifying the corresponding parameters. However, they always stay the same.

For example, by setting all font sizes to 12, I get the following plot:

enter image description here

By changing the sizes to 18, I get the following plot:

enter image description here

As you can see, the only text that actually changes is the one of the legends, so I find it weird it only affects this one.

I've already tried the solutions proposed here without any results:

How to change the font size on a matplotlib plot

Why doesn't the matplotlib label 'fontsize' work?

Python: How to increase/reduce the fontsize of x and y tick labels?

UPDATE: I tried the solution proposed by user Mstaino and it worked to change the size of the labels of the ticks:

enter image description here

Now I just need to find a way to change the size of the labels for the axes, since no solution I have tried so far is able to change them (as you can see in the images).

Charlie
  • 83
  • 7

1 Answers1

1

Not sure if it's the "most efficient" way, but you can access the ticks using plt.xticks() and change them one by one (using a for loop). As for the label, simply pass prop={'size': your_size}.

(Edit) As for the labels, simply pass the corresponding **kwargs: fontsize and color

Example:

x= list(range(10))
y = [i**2 for i in x]

fontsize_x = 30
fontsize_y = 20
fontsize_label = 15

plt.plot(x, y, label='data')
plt.legend(loc='best', prop={'size': fontsize_label})
plt.xlabel('your xlabel', fontsize=15, color='green')
plt.ylabel('your ylabel', fontsize=18, color='orange')

for t in plt.xticks()[1]:
    t.set_fontsize(fontsize_x)
    t.set_color('r')
for t in plt.yticks()[1]:
    t.set_fontsize(fontsize_y)
    t.set_color('b')

If you want to use the axes object, is mostly the same with some small syntax changes:

# using axis
fig, ax = plt.subplots()
ax.plot(x, y, label='data')
ax.legend(loc='best', prop={'size': fontsize_label})
ax.set_xlabel('your xlabel', fontsize=15, color='green')   #set_xlabel instead of xlabel
ax.set_ylabel('your ylabel', fontsize=18, color='orange')   #ibid


for t in ax.get_xticklabels():    #get_xticklabels will get you the label objects, same for y
    t.set_fontsize(fontsize_x)
    t.set_color('r')
for t in ax.get_yticklabels():
    t.set_fontsize(fontsize_y)
    t.set_color('b')

enter image description here

Tarifazo
  • 4,118
  • 1
  • 9
  • 22
  • I tried your solution and it works really good for the label for the individual ticks, thanks a lot. I just need to find a way to change the labels for the axes. – Charlie Jan 24 '19 at 19:41
  • That's even easier. I'll add it to the answer – Tarifazo Jan 25 '19 at 12:27