Matplotlib – Changing the color of a single x-axis tick label

Question:

I am trying to create a vertical bar chart of % speakers of 5 spoken languages in the world, using matplotlib. To better accentuate the highest spoken language, I have changed the color of the bar. I also want to change the corresponding x-axis tick label to a darker color. Everything works fine, except I cannot seem to change the x-axis tick label color.

My Software:

Windows
Anaconda 4.3.0 x64, containing:
– IPython 5.1.0
– Python 3.6.0
– matplotlib 2.0.0
– Spyder 3.1.3


What I’ve Tried/Troubleshooting:

I have tried the solution at Formatting only selected tick labels using plt.gca().get_xticklabels().set_color(), which looks like it does exactly what I want,. Unfortunately, this does not change the color of the x-axis tick label even though the color value seems to change:

#Does not change label color, but does show red when called
print("Color before change: " + plt.gca().get_xticklabels()[-3].get_color()) 
plt.gca().get_xticklabels()[-3].set_color('red') #Does not change the label
print("Color after change: " + plt.gca().get_xticklabels()[-3].get_color())  #Does show the value as 'red'

As the comments attest, the x-axis tick label does not turn red, but the .get_color() method does return red:

Color before change: grey
Color after change: red

I have tried varying the index of get_xticklabels()[index], but all of them seem to do the same as listed.

The answer above mentioned the indexing is not always straight forward, so I’ve done some troubleshooting by printing out the text values of the x-axis tick labels:

for item in plt.gca().get_xticklabels():
    print("Text: " + item.get_text())

Each item comes back blank:

In [9]: runfile('x')
Text: 
Text: 
Text: 
Text: 
Text: 
Text: 
Text: 

It seems to me the labels are being held somewhere else, or maybe are not populated yet. I have tried messing around with get_xmajorticklabels() and get_xminorticklabels() with similar results.

I have also tried passing a list of colors straight to the labelcolor parameter:

 label_col_vals = ['grey','grey','black','grey','grey']
 plt.gca().tick_params(axis='x', labelcolor=label_col_vals)

But this only returns what I assume to be the memory location of the figure:

<matplotlib.figure.Figure at 0x14e297d69b0>

This also causes an error if trying to change single x-axis tick label colors using the get_xticklabels().set_color() method:

ValueError: could not convert string to float: 'grey'

Passing a single color value (as shown below) works, but this sets all of the x-axis tick labels to be the same color:

 plt.gca().tick_params(axis='x', labelcolor='grey')

Question:

How can I change the color of a single x-axis tick label?
Passing a list to labelcolor or getting get_xticklabels().set_color() to work would be preferable, but another method would also be nice.

Code:

'''
@brief autoprint height labels for each bar
@detailed The function determines if labels need to be on the inside or outside 
of the bar.  The label will always be centered with respect to he width of the
bar

@param bars the object holding the matplotlib.pyplot.bar objects
'''
def AutoLabelBarVals(bars):
    import matplotlib.pyplot as plt

    ax=plt.gca()

    # Get y-axis height to calculate label position from.
    (y_bottom, y_top) = ax.get_ylim()
    y_height = y_top - y_bottom

    # Add the text to each bar
    for bar in bars:
        height = bar.get_height()
        label_position = height + (y_height * 0.01)

        ax.text(bar.get_x() + bar.get_width()/2., label_position,
                '%d' % int(height),
                ha='center', va='bottom')

import matplotlib.pyplot as plt
import numpy as np

plt.figure()

'''
@note data from https://www.ethnologue.com/statistics/size
'''
languages =['English','Hindi','Mandarin','Spanish','German']
pos = np.arange(len(languages))
percent_spoken = [372/6643, 260/6643, 898/6643, 437/6643, 76.8/6643]
percent_spoken = [x*100 for x in percent_spoken]

'''
@brief change ba colors, accentuate Mandarin
'''
bar_colors = ['#BAD3C8']*(len(languages)-1)
bar_colors.insert(2,'#0C82D3')

bars = plt.bar(pos, percent_spoken, align='center', color=bar_colors)

'''
@brief Soften the other bars to highlight Mandarin
'''
plt.gca().yaxis.label.set_color('grey')
label_colors = ['grey','grey','black','grey','grey']
#plt.gca().tick_params(axis='x', labelcolor=label_colors)   #Does not work
plt.gca().tick_params(axis='x', labelcolor='grey')   #Works


'''
@brief Try to change colors as in https://stackoverflow.com/questions/41924963/formatting-only-selected-tick-labels
'''
# Try to output values of text to pinpoint which one needs changed
for item in plt.gca().get_xticklabels():
    print("Text: " + item.get_text())

print(plt.gca().get_xticklabels()[0].get_text())  

'''
@warning If trying to set the x-axis tick labels via list, this code block will fail
'''
print("Color before change: " + plt.gca().get_xticklabels()[1].get_color())
plt.gca().get_xticklabels()[1].set_color('red') #Does not change the label
print("Color after change: " + plt.gca().get_xticklabels()[1].get_color())  #Does show the value as 'red'
'''
@warning If trying to set the x-axis tick labels via list, this code block will fail
'''


plt.xticks(pos, languages)
plt.title('Speakers of Select Languages as % of World Population')

# remove all the ticks (both axes), and tick labels on the Y axis
plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off', labelbottom='on', color='grey')

'''
@brief remove the frame of the chart
'''
for spine in plt.gca().spines.values():
    spine.set_visible(False)

# Show % values on bars
AutoLabelBarVals(bars)

plt.show()
Asked By: Doug B

||

Answers:

The call to plt.tick_params with color='grey' is overwriting the color setting.

Your print statements aren’t working because they occur before you set the x tick labels with plt.xticks(). Move both these lines to just after the call to plt.bars and your code should work.

bars = plt.bar(pos, percent_spoken, align='center', color=bar_colors)

plt.xticks(pos, languages)
plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off', labelbottom='on', color='grey')
Answered By: Craig

The ticklabels may change over the course of the script. It is therefore advisable to set their color at the very end of the script, when no changes are made any more.

from __future__ import division
import matplotlib.pyplot as plt
import numpy as np

def AutoLabelBarVals(bars):
    ax=plt.gca()
    (y_bottom, y_top) = ax.get_ylim()
    y_height = y_top - y_bottom
    for bar in bars:
        height = bar.get_height()
        label_position = height + (y_height * 0.01)
        ax.text(bar.get_x() + bar.get_width()/2., label_position,
                '%d' % int(height),
                ha='center', va='bottom')
plt.figure()
languages =['English','Hindi','Mandarin','Spanish','German']
pos = np.arange(len(languages))
percent_spoken = [372/6643, 260/6643, 898/6643, 437/6643, 76.8/6643]
percent_spoken = [x*100 for x in percent_spoken]
bar_colors = ['#BAD3C8']*(len(languages)-1)
bar_colors.insert(2,'#0C82D3')
bars = plt.bar(pos, percent_spoken, align='center', color=bar_colors)
plt.gca().yaxis.label.set_color('grey')
plt.gca().tick_params(axis='x', labelcolor='grey')   #Works
plt.xticks(pos, languages)
plt.title('Speakers of Select Languages as % of World Population')
plt.tick_params(top='off', bottom='off', left='off', right='off', 
                labelleft='off', labelbottom='on', color='grey')
for spine in plt.gca().spines.values():
    spine.set_visible(False)
AutoLabelBarVals(bars)

plt.gca().get_xticklabels()[1].set_color('red') 

plt.show()

I think you can also do this by just indexing the xtick_label you want in plt.setp():

import matplotlib.pyplot as plt

ax = plt.subplot(111)
ax.bar(x=[1, 2, 3], height=[1, 4, 9])
ax.set_xticks([1, 2, 3])
id_tick_change_colour = 1 
plt.setp(ax.get_xticklabels()[id_tick_change_colour], color='red')

enter image description here

Answered By: Thijs
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.