简体   繁体   中英

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 这个图 What I do wrong?

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()

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