首页 > 解决方案 > 熊猫,将两列中的字符串组合为绘图中的刻度值

问题描述

我需要barh从熊猫数据框中绘制。但是,因为y_ticks我想要另一列的索引 + 字符串。索引有德语单词,另一列有英文翻译。

这是我的数据框:

     count     eng

    -----------------
seit   12     since
mehr   11     more

目前我有这个情节:

df.sort_values('count').plot.barh(figsize=(15, 8)).grid(axis='x')
plt.ylabel('Most common words')
plt.xlabel('number of Occurances')

这将索引作为刻度,我想要索引加上eng列中的值,可能在索引值之下,如下所示:

(seit)
since

我如何实现这一目标?

提前致谢!

标签: pythonpandasmatplotlib

解决方案


set_yticklabels 方法应该这样做

fig,ax=plt.subplots(1,1,figsize=(15,8))
df.sort_values('count').plot.barh(ax=ax)
ax.grid()
ax.set_ylabel('Most common words')
ax.set_xlabel('number of Occurances')
ax.set_yticklabels([x[0]+'('+x[1]+')' for x in zip(df.index,df.eng)],ha='right')

在此处输入图像描述


推荐阅读