NineWasps
NineWasps

Reputation: 2273

How add plots to subplots using matplotlib

I want to add plots to subplots using

fig, axarr = plt.subplots(2, 2)
plt.sca(axarr[0, 0])
result = desire_salary.pivot_table('city', 'cult', aggfunc='count')
axarr[0, 0] = result.plot(kind='bar', alpha=0.75, rot=0, label="Presence /         Absence of cultural centre")
axarr[0, 0].set_xlabel("Cultural centre")
axarr[0, 0].set_ylabel("Frequency")
axarr[0, 0].set_title('Salary and culture')
axarr[0, 0].plot(result[[0]], color='red')
plt.sca(axarr[0, 1])
axarr[0, 1] = df.plot()
plt.sca(axarr[1, 0])
plt.show()

But one added to subplot, but others no. I get this graphs What I do wrong?

Upvotes: 0

Views: 996

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40747

When you use pandas (I assume), the surest way to ensure which axes is used is to pass the reference to the axes to the plotting function using the ax= parameter

fig, axarr = plt.subplots(2, 2)
result = desire_salary.pivot_table('city', 'cult', aggfunc='count')
result.plot(kind='bar', alpha=0.75, rot=0, label="Presence /         Absence of cultural centre", ax=axarr[0, 0])
axarr[0, 0].set_xlabel("Cultural centre")
axarr[0, 0].set_ylabel("Frequency")
axarr[0, 0].set_title('Salary and culture')
axarr[0, 0].plot(result[[0]], color='red')


df.plot(ax=axarr[0, 1])

plt.show()

Upvotes: 1

Related Questions