<p>在脚本过程中,标签可能会发生变化。因此,当不再做任何更改时,最好在脚本的最后设置它们的颜色。</p>
<pre>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)
<b>plt.gca().get_xticklabels()[1].set_color('red')</b>
plt.show()</pre>