Limit characters in xticks pandas bar charts… here is a solution to the problem.
Limit characters in xticks pandas bar charts
I have the following code:
df = input_df_.loc[input_df_.index.isin(inds)]
df = df.drop(expected_metrics+metrics+['AgeNBR'],axis=1).mean()\
.sort_values(ascending=False)[:25]
df.plot(kind='bar',width=.4,position=1,color='red')
full_df = input_df_[df.index].mean()
full_df.plot(kind='bar',width=.4,position=0,color='blue')
plt.show()
difference = full_df - df
difference.plot(kind='bar',color = ['r' if x > 0 else 'b' for x in difference.values])
plt.show()
The code works, but because some x-ticks are very long, the two images end up far apart. Is there a way to limit the size of individual x-ticks Chinese? Similar to the first 7 characters?
Solution
Here’s how the questioner solved the problem:
plt.subplot(1,2,1)
df.plot(kind='bar',width=.4,position=1,color='red')
full_df = input_df_[df.index].mean()
full_df.plot(kind='bar',width=.4,position=0,color='blue')\
.set_xticklabels([str(tick)[:45] for tick in full_df.index])
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.gca().set_title('Selected and full feature set averages',fontsize=30)
plt.subplot(1,2,2)
difference = df - full_df
difference.plot(kind='bar',color = ['r' if x > 0 else 'b' for x in difference.values]).\
set_xticklabels([str(tick)[:45] for tick in difference.index])
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.gca().set_title('Selected minus full feature set averages', fontsize=30)
plt.show()