matplotlib make axis ticks label for dates bold

Question:

I want to have bold labels on my axis, so I can use the plot for publication. I also need to have the label of the lines in the legend plotted in bold.
So far I can set the axis labels and the legend to the size and weight I want. I can also set the size of the axis labels to the size I want, however I am failing with the weight.

Here is an example code:

# plotting libs
from pylab import *
from matplotlib import rc


if __name__=='__main__':

  tmpData = np.random.random( 100 )


  # activate latex text rendering
  rc('text', usetex=True)
  rc('axes', linewidth=2)
  rc('font', weight='bold')

  #create figure
  f = figure(figsize=(10,10))

  ax = gca()

  plot(np.arange(100), tmpData, label=r'textbf{Line 1}', linewidth=2)

  ylabel(r'textbf{Y-AXIS}', fontsize=20)
  xlabel(r'textbf{X-AXIS}', fontsize=20)

  fontsize = 20
  fontweight = 'bold'
  fontproperties = {'family':'sans-serif','sans-serif':['Helvetica'],'weight' : fontweight, 'size' : fontsize}
  ax.set_xticklabels(ax.get_xticks(), fontproperties)
  ax.set_yticklabels(ax.get_yticks(), fontproperties)

  for tick in ax.xaxis.get_major_ticks():
      tick.label1.set_fontsize(fontsize)

  for tick in ax.yaxis.get_major_ticks():
      tick.label1.set_fontsize(fontsize)



  legend()
  show()

  sys.exit()

And this is what I get:

plot example

Any idea what I am missing or doing wrong in order to get the axis ticks label in bold?

EDIT

I have updated my code using toms response. However I now have another problem, as I need to use datetime on the x-axis, this has not the same effect as on the normal y-axis (sorry for not putting this in in the original question, but I did not think it would change things):

# plotting libs
from pylab import *
from matplotlib import rc, rcParams
import matplotlib.dates as dates

# datetime
import datetime

if __name__=='__main__':

  tmpData = np.random.random( 100 )
  base = datetime.datetime(2000, 1, 1)
  arr = np.array([base + datetime.timedelta(days=i) for i in xrange(100)])


  # activate latex text rendering
  rc('text', usetex=True)
  rc('axes', linewidth=2)
  rc('font', weight='bold')

  rcParams['text.latex.preamble'] = [r'usepackage{sfmath} boldmath']

  #create figure
  f = figure(figsize=(10,10))

  ax = gca()

  plot(np.arange(100), tmpData, label=r'textbf{Line 1}', linewidth=2)

  ylabel(r'textbf{Y-AXIS}', fontsize=20)
  xlabel(r'textbf{X-AXIS}', fontsize=20)

  ax.xaxis.set_tick_params(labelsize=20)
  ax.yaxis.set_tick_params(labelsize=20)

  ax.xaxis.set_major_formatter(dates.DateFormatter('%m/%Y'))
  ax.xaxis.set_major_locator(dates.MonthLocator(interval=1))


  legend()

Now my result looks like this:

updated figure

It seems to be that the changes doe not affect the display or rather the weight of the x-axis ticks labels.

Asked By: red_tiger

||

Answers:

I think the problem is because the ticks are made in LaTeX math-mode, so the font properties don’t apply.

You can get around this by adding the correct commands to the LaTeX preamble, using rcParams. Specifcally, you need to use boldmath to get the correct weight, and usepackage{sfmath} to get sans-serif font.

Also, you can use set_tick_params to set the font size of the tick labels.enter image description here

Here’s some code that does what you want:

import numpy as np
from matplotlib import rc,rcParams
from pylab import *

tmpData = np.random.random( 100 )


# activate latex text rendering
rc('text', usetex=True)
rc('axes', linewidth=2)
rc('font', weight='bold')
rcParams['text.latex.preamble'] = [r'usepackage{sfmath} boldmath']

#create figure
f = figure(figsize=(10,10))

ax = gca()

plot(np.arange(100), tmpData, label=r'textbf{Line 1}', linewidth=2)

ylabel(r'textbf{Y-AXIS}', fontsize=20)
xlabel(r'textbf{X-AXIS}', fontsize=20)

ax.xaxis.set_tick_params(labelsize=20)
ax.yaxis.set_tick_params(labelsize=20)

legend()
Answered By: tmdavison

Use

plt.xticks(x, weight = 'bold')
Answered By: Emeka Boris Ama
labels = axes.get_xticklabels() + axes.get_yticklabels()
    [label.set_fontweight('bold') for label in labels]
Answered By: wei liu

If you want to make the axis labels bold automatically (i.e. without having to add textbf every time), you could do the following

from matplotlib.axes import Axes
from matplotlib import pyplot as plt
plt.rcParams['text.usetex'] = True

def get_new_func(axis_name):  # returns a modified version of the Axes.set_xlabel (or y) methods
    orig_func = getattr(Axes, f'set_{axis_name}label')

    def add_bold(self, *args, **kwargs):
        new_args = list(args)
        new_args[0] = fr"textbf{{{new_args[0]}}}" # modify the argument
        return orig_func(self, *new_args, **kwargs)

    return add_bold

for x in ['x', 'y']: 
    setattr(Axes, f'set_{x}label', get_new_func(x)) # modify the methods of the Axis class


x = np.linspace(0,  2 * 3.14, 20)
y = np.sin(x)
ax = plt.gca()

ax.plot(x, y)
ax.set_xlabel("Theta")
ax.set_ylabel("Amp")

plt.show()

This makes use of the fact that the Axis.set_xlabel and Axis.set_ylabel methods are attributes (in this case function objects) that can be modified by the user. The modification is done in add_bold, which simply calls the original function object but with a modified argument. orig_func and add_bold are defined inside of get_new_func in order to correctly preserve the reference to the original method (i.e. i’m forming a closure).

Answered By: garage physicist

this is another example for you

import matplotlib.pyplot as plt
  
places = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
literacy_rate = [100, 98, 90, 85, 75, 50, 30, 45, 65, 70]
female_literacy = [95, 100, 50, 60, 85, 80, 75, 99, 70, 30]
  
plt.xlabel("Places")
plt.ylabel("Percentage")
  
plt.plot(places, literacy_rate, color='blue',
         linewidth=6, label="Literacy rate")
  
plt.plot(places, female_literacy, color='fuchsia',
         linewidth=4, label="Female Literacy rate")
  
plt.legend(loc='lower left', ncol=1)

and the youtput will be like this:

example plot

Answered By: Nindi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.