Home > front end >  How to set font size/weight for small ticks in simple Matplotlib chart?
How to set font size/weight for small ticks in simple Matplotlib chart?

Time:11-08

I try to make a simple plot and set the size and weight of the font for ticks, however, even after reading matplotlib manual I am not clear what is the algorithm for that.

I am using recommended OOP-approach with matplotlib 3.3. Here is the MWE:

import numpy as np
from matplotlib import pyplot as plt
import matplotlib.ticker as ticker
x = np.linspace(0, 10, 1000)
print(x)
y = x**2
z = 1   5*x

# Plotting:
fig, ax = plt.subplots()
ax.plot(x, y, color='blue', linewidth=2.0, label="Quadratic")
ax.plot(x, z, color='orange', linestyle='-.', linewidth=1.5, label="Linear")
# Axis limits
ax.set_xlim(0, 10)
ax.set_ylim(0, 100)

# Axis labels
ax.set_xlabel(r"A linear array of $X$ values", fontsize=16)
ax.set_ylabel(r"Some functions $y(x)$", fontsize=16)

# Ticks:
ax.xaxis.set_major_locator(ticker.AutoLocator())
ax.yaxis.set_major_locator(ticker.AutoLocator())

ax.xaxis.set_minor_locator(ticker.AutoMinorLocator())
ax.yaxis.set_minor_locator(ticker.AutoMinorLocator())
ax.xaxis.set_minor_formatter(ticker.ScalarFormatter())

ax.set_title("Some simple plots", loc='left')
ax.legend(frameon=False)

plt.show()

Questions:

  • How to specify font size (in pt) and font weight (i.e. bold) for labels on both major and minor ticks? I found a way to specify font size via ax.xaxis.set_tick_params(labelsize=12), but I cannot find a way to turn font bold.
  • Can I specify font parameters for tick labels only via ax.xaxis.set_tick_params()? Is there a way to specify font parameters via Formatters?
  • I have installed monospaced font "Source Code Pro". How can I use this specific font for my Matplotlib? Can I specify the path to the font?

I know that my questions might be basic, but I cannot clarify an algorithm for formatting of tick labels. I would appreciate your help!

CodePudding user response:

How to specify font size (in pt) and font weight (i.e. bold) for labels on both major and minor ticks?

If you just want to set the label size, then tick_params is more easier to use. To set the font weight, then you can get the Text instance for each tick labels by Axis.get_majorticklabels:

for tick in ax.xaxis.get_majorticklabels():
    tick.set_fontweight('bold')

# similar for minor tick labels by get_minorticklabels

Can I specify font parameters for tick labels only via ax.xaxis.set_tick_params()? Is there a way to specify font parameters via Formatters?

As I said above, tick_params is a better choice if the fontweight is not involved. As far as I know, Formatter is not related to font properties, but to the numeric representation of tick labels. So I don't think font properties can be specified via Formatter.

I have installed monospaced font "Source Code Pro". How can I use this specific font for my Matplotlib? Can I specify the path to the font?

The question can be transformed to how to specify the font name in matplotlib. You can find the font name in the property when you right click the font file. Alternatively, you can use the set_fontproperties method of Text instance to set the font by the file path, such as:

for tick in ax.xaxis.get_majorticklabels():
    tick.set_fontweight('bold')
    tick.set_fontproperties(pathlib.WindowsPath(r'C:\Users\Downloads\TTF-source-code-pro-2.038R-ro-1.058R-it\SourceCodePro-Regular.ttf'))
  • Related