我有许多绘图,显示从语音到文本引擎转录的文本,我想在其中显示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
可以在打印条之前和之后根据标签设置每个条的颜色
我将使用下面的示例数据进行演示
1。打印前: 这是绘制彩色条形图最常用的方法。您可以将颜色列表传递给
plt.plot()
传递给函数的
data
包含两列,其中第一列列出xtick上的所有单词,第二列列出相应的数字。target
是您期望的单词。 代码根据每个单词是否与目标一致来确定其颜色。比如说,2。绘图后: 如果要在调用
plt.plot
后访问颜色,请使用set_color
更改特定条的颜色plt.bar
返回一个BarContainer
,其第i个元素是一个面片(矩形)。迭代所有标签,如果单词击中目标,则更改颜色。 比如说,最后,对于在
axs
上迭代,只需将其拉威尔即可解决问题相关问题 更多 >
编程相关推荐