Home > Mobile >  X-axis labels not showing on bar plot
X-axis labels not showing on bar plot

Time:10-15

I'm having an issue with my plot only displaying every other label on the x-axis.

Code:

m_count=  [12, 12, 13, 16, 12, 12, 13, 16, 9, 10]
f_count =[13, 13, 12, 9, 13, 13, 11, 9, 15, 15]
    
labels = ["Capomulin", "Ceftamin", "Infubinol", "Ketapril", "Naftisol", "Placebo", "Propriva", "Ramicane", "Stelasyn", "Zoniferol"]
N=10
ind = np.arange(N)
width = .35
fig= plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(ind, m_count, width, color ="b")
ax.bar(ind, f_count, width, color ="r", bottom=m_count)
ax.set_ylabel('Count')
ax.set_title("Count of Mice by Drug Regimen")
ax.set_xticks(ind, labels)
ax.set_xticklabels(labels, rotation=90 )
ax.set_yticks(np.arange(0, 30, 2))
ax.legend(labels=['Male', 'Female'])
plt.show()

This results in :

enter image description here

CodePudding user response:

My quickest fix is to use

plt.xticks(ind, labels)

But thanks to Trenton's comments, I have come up with an updated solution:

ax.set_xticks(ind)
ax.set_xticklabels(labels)

CodePudding user response:

This is causing the problem for you

ax = fig.add_axes([0,0,1,1])

This code tells the matplotlib to use all the figure space for actual plot and leave no space for the axes and labels.

Instead, Use this

ax = fig.add_subplot(111)

This should help

  • Related