简体   繁体   中英

Xaxis text overlapping - Matplotlib

I plot a bar graph using matplotlib and everything works fine. When the "text label" on X-axis is too lengthy for each data point, then the text overlaps like shown in the figure. I also tried to rotate the alignment to 45 and 90 degrees, but the text goes beyond the frame.

在此处输入图片说明

X = [1,2,3,4,5,6]
Y = [55,46,46,36,27,9]
point_labels =[50,38,25,6,6,6]
fig, axes = plt.subplots(1)
ax2 = axes
n=7
ind=np.arange(n)
width=0.1
axis_font = {'fontname':'Arial', 'size':'11'}
Color = ['0.1','0.2','0.3','0.4','0.5','0.6']

bar = ax2.bar(X,Y,align="center",width=0.5,color=Color)
bar[0].set_color('r')

plt.xticks(ind+width/2, ('','Insufficient skills','Complex hardware and software','High initial investments','Complexity in collection and storing data','Insufficient hardware power for computing','Data collaboration'),horizontalalignment='center')

plt.ylabel('in precentage (%)')

text(4.6,52, '100% = 11 responses', style='italic',bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})

axes.yaxis.grid()
axes.set_ylim([0,60])

plt.show()

Can anyone suggest an idea to fix this?

The best I can come up with is set the rotation as vertical changing the figure extension and run a plt.tight_layout() . Those would be the last 3 lines of your code:

mngr = plt.get_current_fig_manager()
mngr.window.setGeometry(50,50,960, 640)
plt.tight_layout()

Alternatively, you should use multi lines with \\n . See http://matplotlib.org/examples/pylab_examples/multiline.html在此处输入图片说明

Below solution worked for me

How to prevent overlapping x-axis labels in sns.countplot

plt.xticks(
    rotation=45, 
    horizontalalignment='right',
    fontweight='light',
    fontsize='x-large'  
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM