Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make the text size of the x and y axis labels and the title on matplotlib and prettyplotlib graphs bigger

I set the figure size of a matplotlib or prettyplotlib graph to be large. As an example lets say the size is 80 height by 80 width.

The text size for the plot title, x and y axis labels (i.e. point label 2014-12-03 and axis label [month of year] become very small to the point they are unreadable.

How do I increase the size of these text labels? Right now I have to zoom in with the web browser to see them.

enter image description here

like image 364
yoshiserry Avatar asked Dec 08 '14 01:12

yoshiserry


People also ask

How do you change the size of X and Y labels in Matplotlib?

To increase the space for X-axis labels in Matplotlib, we can use the spacing variable in subplots_adjust() method's argument.

How do I change the font size on the X-axis in Matplotlib?

To change the font size of the scale in Matplotlib, we can use labelsize in the tick_params() method.


1 Answers

The size property:

import matplotlib.pyplot as plt
plt.xlabel('my x label', size = 20)
plt.ylabel('my y label', size = 30)
plt.title('my title', size = 40)
plt.xticks(size = 50)
plt.yticks(size = 60)

Example:

import numpy as np
import matplotlib.pyplot as plt
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)
plt.xlabel('Smarts', size = 20)
plt.ylabel('Probability')
plt.title('Histogram of IQ', size = 50)
plt.text(60, .025, r'$\mu=100,\ \sigma=15$', size = 30)
plt.axis([40, 160, 0, 0.03])
plt.xticks(size = 50)
plt.yticks(size = 50)
plt.grid(True)
plt.show()

enter image description here

---------- EDIT --------------

using pretty plot

fig, ax = plt.plot()
fig.suptitle('fig title', size = 80)
ax.set_title('my title', size = 10)
ax.set_xlabel('my x label', size = 20)
ax.set_ylabel('my y label', size = 30)
for tick in ax.xaxis.get_major_ticks():
    tick.label.set_fontsize(40) 
for tick in ax.yaxis.get_major_ticks():
    tick.label.set_fontsize(50) 

--------- LEGEND --------------

use the prop property

ppl.legend(prop={'size':20})
plt.legend(prop={'size':20})

same command..


example:

import matplotlib.pyplot as plt
import matplotlib as mpl
from prettyplotlib import brewer2mpl
import numpy as np
import prettyplotlib as ppl
np.random.seed(12)
fig, ax = plt.subplots(1)
fig.suptitle('fig title', size = 80)
ax.set_title('axes title', size = 50)
for tick in ax.xaxis.get_major_ticks():
    tick.label.set_fontsize(60)
ax.set_ylabel("test y", size = 35)
for i in range(8):
    y = np.random.normal(size=1000).cumsum()
    x = np.arange(1000)
    ppl.plot(ax, x, y, label=str(i), linewidth=0.75)
ppl.legend(prop={'size':30})
fig.savefig('plot_prettyplotlib_default.png')

enter image description here

like image 104
João Paulo Avatar answered Oct 09 '22 22:10

João Paulo