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?
To increase the space for X-axis labels in Matplotlib, we can use the spacing variable in subplots_adjust() method's argument.
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')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With