Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

python - Pandas, combine strings from two columns for ticks value in plot

I need to plot barh from pandas dataframe. However, for y_ticks I want the index + string from another column. Index has German words and the other column has the english translation.

This is my Dataframe:

     count     eng

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

Currently I have this plot:

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

This puts index as ticks, I want index plus the value from eng column, possibly under the index value like this:

(seit)
since

How do I achieve this?

Thanks in advance!

question from:https://stackoverflow.com/questions/65918126/pandas-combine-strings-from-two-columns-for-ticks-value-in-plot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

set_yticklabels method should do that

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')

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...