Matplotlib-更改单个x轴刻度标签的颜色
我正在尝试使用matplotlib创建一个垂直条形图,其中包含世界上5种语言的%说话者.为了更好地强调最高口头语言,我更改了栏的颜色.我还想将相应的x轴刻度标签更改为较深的颜色.一切正常,除非我似乎无法更改x轴刻度标签的颜色.
我的软件:
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,包含:
-IPython 5.1.0
-Python 3.6.0
-matplotlib 2.0.0
-Spyder 3.1.3
Windows
Anaconda 4.3.0 x64, containing:
- IPython 5.1.0
- Python 3.6.0
- matplotlib 2.0.0
- Spyder 3.1.3
我尝试过的问题/故障排除:
我已经在使用plt.gca().get_xticklabels().set_color()
仅格式化选定的刻度标签中尝试了该解决方案,看起来确实可以满足我的要求.不幸的是,即使颜色值似乎发生了变化,这也不会改变x轴刻度标签的颜色:
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'
正如评论所证明的那样,x轴刻度标签不会变成红色,但是.get_color()方法确实返回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
我尝试过更改get_xticklabels()[index]
的索引,但是它们似乎都与列出的相同.
I have tried varying the index of get_xticklabels()[index]
, but all of them seem to do the same as listed.
上面提到的索引并非总是简单明了,因此我通过打印出x轴刻度标签的文本值进行了一些故障排除:
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())
每个项目变回空白:
In [9]: runfile('x')
Text:
Text:
Text:
Text:
Text:
Text:
Text:
在我看来,这些标签被保留在其他地方,或者可能尚未填充.我试过弄乱get_xmajorticklabels()
和get_xminorticklabels()
,结果相似.
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.
我也尝试过将颜色列表直接传递给labelcolor
参数:
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>
如果尝试使用get_xticklabels().set_color()
方法更改单个x轴刻度标签颜色,这也会导致错误:
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'
传递单个颜色值(如下所示)是可行的,但这会将所有x轴刻度标签设置为相同的颜色:
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')
问题:
如何更改单个x轴刻度标签的颜色?
将列表传递给labelcolor
或使get_xticklabels().set_color()
工作将是可取的,但是另一种方法也将是不错的选择.
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.
代码:
'''
@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()
刻度标签可能会在脚本过程中发生变化.因此,当不再进行任何更改时,建议在脚本的最后设置它们的颜色.
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()