Ciara Spencer
Ciara Spencer

Reputation: 129

X-axis labels not showing on bar plot

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

Upvotes: 1

Views: 6943

Answers (1)

Jeremy
Jeremy

Reputation: 876

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)

Upvotes: 2

Related Questions