1

I have an horizontal barchart in which I am setting the labels with the command:

ax.set_yticklabels(df_chart.country_group)

I need some of the labels to be bold (and if possible aligned center-ly) depending on some name of the label.

I have tried with:

ax.set_yticklabels(df_chart.country_group, weight=["bold", "bold", "normal"...])

but the function does not accept a list. I have tries also with a loop:

for label in ax.get_yticklabels():
     if label in ["World", "Developing countries", "Developed countries"]:
         label.set_fontproperties(weight="bold")

but I was not able to extract the label value from the Text object.

1 Answer 1

2

Correct, you cannot use a list for such font properties. The second approach goes in the right direction. You need to get_text() from the labels to compare with some other string.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot([1,3,12])
ax.set_yticks([1,3,7,11])
ax.set_yticklabels(list("ABCD"))

for label in ax.get_yticklabels():
    if label.get_text() in ["B","C"]:
        label.set_weight("bold")

plt.show()

enter image description here

Note that this only works if, as in this case, the label text has been set previously via set_*ticklabels.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.