0

Please consider the following example in which I want to change the weight of label "C" to bold.

df = pd.DataFrame(data={'value': [3, 5, 7, 4, 5]},
                  index=list('ABCDE'))

fig, ax = plt.subplots()
df.plot.barh(ax=ax)

enter image description here

Many examples I've seen (e.g., tick_params and set_yticklabels) change all ticklabels or simply replace a label without formatting.

Is there a way to customize it individually?

steven
  • 2,130
  • 19
  • 38

1 Answers1

1

This is one way to do so:

  • Traverse through the default tick-labels,
  • Modify the required label to be bold faced,
  • Reassign the tick-labels.

from matplotlib import rc
import pandas as pd
import matplotlib.pyplot as plt

rc('text', usetex=True)

df = pd.DataFrame(data={'value': [3, 5, 7, 4, 5]},
                  index=list('ABCDE'))

fig, ax = plt.subplots()
df.plot.barh(ax=ax)

fig.canvas.draw()
new_labels = []
to_modify = 'C'

for lab in ax.get_yticklabels():
    txt = lab.get_text()
    if txt == to_modify:
        new_labels.append(r'$\textbf{%s}$' %txt) # append bold face text
    else:    
        new_labels.append(txt) # append normal text

ax.set_yticklabels(new_labels)        

enter image description here

Alternative as suggested by ImportanceOfBeingEarnest : set_fontweight only works if not using latex (TeX rendering).

fig, ax = plt.subplots()
df.plot.barh(ax=ax)

to_modify = 'C'

for lab in ax.get_yticklabels():
    if lab.get_text() == to_modify:
      lab.set_fontweight('bold')
Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • https://matplotlib.org/3.1.0/api/text_api.html#matplotlib.text.Text.set_fontweight – ImportanceOfBeingErnest Jul 15 '19 at 14:49
  • Thx man. This is indeed a way to do that! But using latex may have some limit. For example, in case I need to change fontsize etc. – steven Jul 15 '19 at 14:52
  • @ImportanceOfBeingErnest : Thanks for the link. I must upgrade to 3+ soon. Can't do it at the moment due to some reasons. – Sheldore Jul 15 '19 at 14:55
  • 1
    @steven : You can still change the fontsize even with TeX turned on – Sheldore Jul 15 '19 at 14:56
  • @ImportanceOfBeingErnest sorry I'm not following. Is there a way to do it without using `fig.canvas.draw()`? – steven Jul 15 '19 at 15:06
  • 1
    @steven : Check my edited answer based on IOBE suggestion. I tried it on Google CoLab and it worked without using `fig.canvas.draw()` – Sheldore Jul 15 '19 at 15:23
  • 1
    Maybe one should note that `set_fontweight` only works if ***not*** using latex. Also important to note: `fig.canvas.draw()` is not needed in this case where `df.plot.bar` is used, but in general it is correct, e.g. if using `plt.barh` instead, it would be necessary. – ImportanceOfBeingErnest Jul 15 '19 at 16:00
  • @ImportanceOfBeingErnest : Thanks again. Added the important words to the answer. – Sheldore Jul 15 '19 at 16:05
  • I tried to set the weight with an int (0, 1000) based on the doc but i didn't see anything when setting it to 550, but it suddenly becomes obviously bold when setting to 560, and then has no visual change after than even set to 1000. Am I missing something? – steven Jul 15 '19 at 17:47
  • it seems like [not all font supports all weight](https://github.com/matplotlib/matplotlib/issues/14801#event-2486487150) – steven Jul 16 '19 at 15:22
  • I'm not sure why but the .get_text() comes back empty when running the python script. When I debug the script and step thru it it DOES return a string. – tvdsluijs Sep 04 '22 at 09:52