Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a bigger font size for x-axis tick labels in scipy-generated dedrogram?

With the following imports:

import matplotlib as mpl
from scipy.cluster.hierarchy import dendrogram

I set the font size globally thus (based on this other Stack Overflow answer):

mpl.rcParams.update({'font.size': 20})

Then I create the dendrogram with the following (where m is a matrix created elsewhere):

dendrogram(m)

Then finally I show the plot with:

mpl.pyplot.show()

The y-axis tick labels are 20 points as expected. However, the x-axis tick labels are tiny, much smaller than 20 points. It seems like matplotlib is automatically scaling down the font size to fit the density of the data despite the font size settings above. This is the case even when I zoom in and there is plenty of room to show the larger font.

How can I make the x-axis ticks use the larger font size?

like image 583
Ghopper21 Avatar asked Aug 17 '15 03:08

Ghopper21


People also ask

How do you change the size of the X-axis labels in python?

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


2 Answers

You can do it with Axes.tick_params() method:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
import numpy

N = 20
n = numpy.random.normal(size=(N, 2))
Z = linkage(n)

# implicit interface
dendrogram(Z)
ax = plt.gca()
ax.tick_params(axis='x', which='major', labelsize=15)
ax.tick_params(axis='y', which='major', labelsize=8)
plt.savefig('t.png')

# explicit interface
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
dendrogram(Z, ax=ax)
ax.tick_params(axis='x', which='major', labelsize=15)
ax.tick_params(axis='y', which='major', labelsize=8)
fig.savefig('t.png')

enter image description here

like image 194
fjarri Avatar answered Oct 23 '22 17:10

fjarri


Another way to adjust the x-axis font size is changing leaf_font_size:

dendrogram(linkage_matrix, leaf_font_size=8)

Here is a dendrogram tutorial that I find useful.

like image 39
Doralisa Avatar answered Oct 23 '22 19:10

Doralisa