pythonmatplotlibpolar-plot

Change matplotlib polar plot radial axis label fonts


I tried to create two polar scatter plots, one being a zoom-in of the other. I have no problem configuring the font properties of the angular axis, but my attempts to change the fonts of the radial axis have not been successful.

I have tried using the font_manager to define the font:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties

#define font
baseFont = FontProperties(family='Helvetica',fname=[path to the .ttf file])

a = np.random.uniform(-np.pi/2,np.pi/2,100)
d = np.random.uniform(0,20,100)
c = np.random.uniform(0,1,100)

fig, sub = plt.subplots(1,2,subplot_kw={'polar':[True,True]})

#configure the ticks
aTicks = np.linspace(-np.pi/2,np.pi/2,7,endpoint=True)
aLabels = np.linspace(-90,90,7,endpoint=True)
dTicks1 = np.arange(0,20.1,5)
dTicks2 = np.arange(0,5.1,1)

#plot the first graph
sub[0].scatter(a,d,c=c,cmap='hsv')

sub[0].set_thetamin(-90)
sub[0].set_thetamax(90)
sub[0].set_ylim(0,20.1)
sub[0].set_xticks(aTicks,labels=aLabels,font=baseFont,fontsize=12)
sub[0].set_yticks(dTicks1,labels=dTicks1,font=baseFont,fontsize=12)

sub[1].scatter(a,d,c=c,cmap='hsv')

sub[1].set_thetamin(-90)
sub[1].set_thetamax(90)
sub[1].set_ylim(0,5.1)
sub[1].set_xticks(aTicks,labels=aLabels,font=baseFont,fontsize=12)
sub[1].set_yticks(dTicks2,labels=dTicks2,font=baseFont,fontsize=12)

plt.show()

The angular axis tick font is changed to Helvetica, while the radial axis tick font remains the default font.

I have also tried changing the tick densities and confirm that sub[0].set_yticks() can affect the radial axis tick marks, but the only thing that does not change is the tick label font. The resulting image is attached.

Plot result


Solution

  • Here is the working code:

    import matplotlib.pyplot as plt
    import numpy as np
    # import global font_manager
    from matplotlib import font_manager
    
    # add the custom font to the global font manager (don't create an instance)
    font_manager.fontManager.addfont('Jersey15.ttf')  # font ttf file path
    # in this case 'Jersey15.ttf' has a font family 'Jersey 15' (note the name, I think you got it already)
    
    a = np.random.uniform(-np.pi / 2, np.pi / 2, 100)
    d = np.random.uniform(0, 20, 100)
    c = np.random.uniform(0, 1, 100)
    
    fig, sub = plt.subplots(1, 2, subplot_kw={'polar': [True, True]})
    
    # configure the ticks
    aTicks = np.linspace(-np.pi / 2, np.pi / 2, 7, endpoint=True)
    aLabels = np.linspace(-90, 90, 7, endpoint=True)
    dTicks1 = np.arange(0, 20.1, 5)
    dTicks2 = np.arange(0, 5.1, 1)
    
    # plot the first graph
    sub[0].scatter(a, d, c=c, cmap='hsv')
    
    sub[0].set_thetamin(-90)
    sub[0].set_thetamax(90)
    sub[0].set_ylim(0, 20.1)
    sub[0].set_xticks(aTicks, labels=aLabels)
    sub[0].set_yticks(dTicks1, labels=dTicks1)
    # use the `font_manager.FontProperties(fontname).get_name()` for the font family (i.e. 'Jersey 15' or 'Bernard MT 
    # Condensed')
    sub[0].tick_params(labelfontfamily=font_manager.FontProperties("Jersey 15").get_name(), labelsize=20)
    # or, you can use the name for the font family directly also
    # sub[1].tick_params(labelfontfamily="Jersey 15", labelsize=12)
    
    sub[1].scatter(a, d, c=c, cmap='hsv')
    
    sub[1].set_thetamin(-90)
    sub[1].set_thetamax(90)
    sub[1].set_ylim(0, 5.1)
    sub[1].set_xticks(aTicks, labels=aLabels)
    sub[1].set_yticks(dTicks2, labels=dTicks2)
    # you can use the name for the font family directly also
    sub[1].tick_params(labelfontfamily="Jersey 15", labelsize=12)
    
    plt.show()
    
    

    This produces the following output:

    Jersey 15 Helvetica
    Jersey 15 Helvetica

    To check the fonts available you can use font_manager.get_font_names().

    I think this will point you in right direction. I have attached the links I read to get the idea below please refer to those if you want to learn more.

    1. https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.tick_params.html
    2. https://matplotlib.org/stable/gallery/ticks/tick-formatters.html