19

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()
denis
  • 21,378
  • 10
  • 65
  • 88
Doug B
  • 347
  • 1
  • 2
  • 8

3 Answers3

22

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()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • this solution doesn't help if you have multiple axes. – jimh Jun 10 '18 at 20:40
  • 6
    @jimh The question has a single axes, so this solution is specifically using this single axes. But the general idea will of course work for any number of axes. You would need to call `.get_xticklabels()[1].set_color('red')` on all axes. – ImportanceOfBeingErnest Jun 10 '18 at 20:44
  • well it could be interpreted as on a single individual axis not a plot with only one – jimh Jun 11 '18 at 17:42
  • @jimh But I honestly don't see your point. If you want to do this with several axes, use this solution on all of them. If it doesn't work, ask a new question about it. – ImportanceOfBeingErnest Jun 11 '18 at 17:51
  • It did not work for me, but I was using it to set_text(). I tried for example ax2.get_yticklabels()[-1].set_text('>=30') to no avail and a couple different iterations thereof. basically I was trying to edit the very last y tick on each axis. Couldn't get it to work for me. I tried a couple different answers on similar questions. Tbh, it was so easy to do in Illustrator, I just stopped. – jimh Jun 11 '18 at 19:45
  • Such things cannot be solved in the comments, but as said you may ask a new question about it. – ImportanceOfBeingErnest Jun 12 '18 at 09:52
  • What about if I also want the tick itself to have that color? – gota Apr 14 '20 at 16:59
1

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')
Craig
  • 4,605
  • 1
  • 18
  • 28
  • Unfortunately, this did not work in my situation. I tried varying the indices to see if I was calling the wrong element, but still no luck. Thanks for the suggestion, though, it may come in handy in future issues. – Doug B Mar 18 '17 at 19:40
  • Thanks for looking. If it helps, I was able to reproduce your code snippet with the desired results. I am assuming this means it is not my setup that is causing the issue. – Doug B Mar 18 '17 at 19:44
  • 1
    @DougB It wasn't your setup, just the order of calls in your code. Please let me know if the code works correctly after you move those two lines. – Craig Mar 18 '17 at 20:52
  • Ah, rookie mistake. I should have kept plt.tick_params() in one line. Thanks for figuring this out. I am not sure which of you two figured it out first (between you and @ImportanceOfBeingErnest) so I am going to wait and see if Stackoverflow gives me a better time than 'answered 1 hour ago' before I assign the solution – Doug B Mar 18 '17 at 21:34
1

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

Thijs
  • 433
  • 8