简体   繁体   中英

How to get two subplots side-by-side in matplotlib?

I just used following code to plot 2 subplot:

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8,4))
ax1 = fig.add_subplot(121)
ax1.set_xlabel('Credit_History')
ax1.set_ylabel('Count of Applicants')
ax1.set_title("Applicants by Credit_History")
temp1.plot(kind='bar')
ax2 = fig.add_subplot(122)
temp2.plot(kind='bar')
ax2.set_xlabel('Credit_History')
ax2.set_ylabel('Probability of getting loan')
ax2.set_title("Probability of getting loan by credit history")

I am getting following output

截图 1

截图 2

I am getting 3 plots ie 2 subplots as I intended, however the second one empty with titles as described. And third one underneath the two with bars for temp2 without the titles.

What I want are two subplots side by side with titles as described. Wondering what I have done wrong?

I'm not able to reproduce this exactly unless I put plt.show() between creating the second axes instance and temp2.plot(kind='bar') . If you aren't calling plt.show() (or clearing the buffer in some other way) you should try

temp1.plot(kind='bar', ax=ax1)
temp2.plot(kind='bar', ax=ax2)

This should correctly use the two generated axes instances.

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