title: Matplotlib - Changing the color of a single x-axis tick label
body: <p>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.
  <br /><br />
  <strong>My Software:</strong></p>
  
  <p>Windows <br />
  Anaconda 4.3.0 x64, containing:<br />
   - IPython 5.1.0 <br />
   - Python 3.6.0 <br />
   - matplotlib 2.0.0 <br />
   - Spyder 3.1.3</p>
  
  <p><br /> 
  <strong>What I've Tried/Troubleshooting:</strong></p>
  
  <p>I have tried the solution at <a href="https://stackoverflow.com/questions/41924963/formatting-only-selected-tick-labels">Formatting only selected tick labels</a> using <code>plt.gca().get_xticklabels().set_color()</code>, 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:</p>
  
  <pre><code>#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'
  </code></pre>
  
  <p>As the comments attest, the x-axis tick label does not turn red, but the .get_color() method does return <code>red</code>:</p>
  
  <pre><code>Color before change: grey
  Color after change: red
  </code></pre>
  
  <p>I have tried varying the index of <code>get_xticklabels()[index]</code>, but all of them seem to do the same as listed.</p>
  
  <p>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:</p>
  
  <pre><code>for item in plt.gca().get_xticklabels():
      print("Text: " + item.get_text())
  </code></pre>
  
  <p>Each item comes back blank:</p>
  
  <pre><code>In [9]: runfile('x')
  Text: 
  Text: 
  Text: 
  Text: 
  Text: 
  Text: 
  Text: 
  </code></pre>
  
  <p>It seems to me the labels are being held somewhere else, or maybe are not populated yet.  I have tried messing around with <code>get_xmajorticklabels()</code> and <code>get_xminorticklabels()</code> with similar results.  </p>
  
  <p>I have also tried passing a list of colors straight to the <code>labelcolor</code> parameter:</p>
  
  <pre><code> label_col_vals = ['grey','grey','black','grey','grey']
   plt.gca().tick_params(axis='x', labelcolor=label_col_vals)
  </code></pre>
  
  <p>But this only returns what I assume to be the memory location of the figure:</p>
  
  <pre><code>&lt;matplotlib.figure.Figure at 0x14e297d69b0&gt;
  </code></pre>
  
  <p>This also causes an error if trying to change single x-axis tick label colors using the <code>get_xticklabels().set_color()</code> method:</p>
  
  <pre><code>ValueError: could not convert string to float: 'grey'
  </code></pre>
  
  <p>Passing a single color value (as shown below) works, but this sets all of the x-axis tick labels to be the same color:</p>
  
  <pre><code> plt.gca().tick_params(axis='x', labelcolor='grey')
  </code></pre>
  
  <p><br />
  <strong>Question:</strong></p>
  
  <p><strong>How can I change the color of a single x-axis tick label?</strong>
  Passing a list to <code>labelcolor</code> or getting <code>get_xticklabels().set_color()</code> to work would be preferable, but another method would also be nice. </p>
  
  <p><br />
  <strong>Code:</strong></p>
  
  <pre><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()
  </code></pre>