【发布时间】:2021-01-18 15:04:15
【问题描述】:
我有许多图表显示从语音到文本引擎的转录文本,我想在其中显示 S2T 引擎正确转录的条形图。我已根据子图的预期值标记了子图,现在想将引擎正确转录的条着色为与其他条不同的数字。
这意味着我需要根据它们的 x-tick 标签访问条形的颜色。我该怎么做?
基本上:
for xlabel in fig.xlabels:
if(xlabel.text == fig.title):
position = xlabel.position
fig.colorbar(position, 'red')
用于生成绘图的代码:
def count_id(id_val, ax=None):
title = df.loc[df['ID'] == id_val, 'EXPECTED_TEXT'].iloc[0]
fig = df[df['ID']==id_val]['TRANSCRIPTION_STRING'].value_counts().plot(kind='bar', ax=ax, figsize=(20,6), title=title)
fig.set_xticklabels(fig.get_xticklabels(), rotation=40, ha ='right')
fig.yaxis.set_major_locator(MaxNLocator(integer=True))
fig, axs = plt.subplots(2, 4)
fig.suptitle('Classic subplot')
fig.subplots_adjust(hspace=1.4)
count_id('byte', axs[0,0])
count_id('clefting', axs[0,1])
count_id('left_hander', axs[0,2])
count_id('leftmost', axs[0,3])
count_id('right_hander', axs[1,0])
count_id('rightmost', axs[1,1])
count_id('wright', axs[1,2])
count_id('write', axs[1,3])
如果有人知道如何迭代axs,这样我就不必调用count_id() 8 次,那也很有帮助。是的,我试过了:
misses = ['byte', 'cleftig', 'left_hander', 'leftmost', 'right_hander', 'rightmost', 'wright', 'write']
for ax, miss in zip(axs.flat, misses):
count_id(ax, miss) # <- computer says no
【问题讨论】:
-
设置条形的颜色时,您可以只传递一种颜色(所有条形都相同)或包含每个标签颜色的数组。示例:stackoverflow.com/a/59578106/9142735
-
我的意思是,是的,但是有没有办法在初始化 plt.plot() 后访问颜色属性?
-
哦,我不确定你可以。除了使用
DataFrame.plot创建图形,您可以直接使用 matplotlib 的plt.bar并在创建每个绘图时将您的颜色数组传递给它(如上例所示)。
标签: python pandas matplotlib