Plotting two histograms from a pandas DataFrame in one subplot using matplotlib

微笑、不失礼 提交于 2019-12-01 08:44:22

I don't know if I understood your question correctly, but something like this can combine the plots. You might want to play around a little with the alpha and change the headers.

#NOTE that you might want to specify your bins or they wont line up exactly
fig, ax = plt.subplots(1, 3, sharex='col', sharey='row', figsize=(20, 18))
n = 3
for j in range(n):
    df.hist(column=df.columns[j], bins=12, ax=ax[j], alpha=0.5, color='red')
    df.hist(column=df.columns[j+n], bins=12, ax=ax[j], alpha=0.5, color='blue')
    ax[j].set_title(df.columns[j][2:])

To plot them both next to eachother, try this:

#This example doesnt have the issue with different binsizes within one subplot
fig, ax = plt.subplots(1, 3, sharex='col', sharey='row', figsize=(20, 18))

n = 3
colors = ['red', 'blue']

axes = ax.flatten()
for i,j in zip(range(n), axes):
    j.hist([df.iloc[:,i], df.iloc[:,i+n]], bins=12, color=colors)
    j.set_title(df.columns[i][2:])

you want something that loops through each column and plot its data in histogram, right? I can suggest you to make few modifications that you can re-use in future code, before giving the code there are few useful tips that are helpful,

  1. One must be aware that dataframes have attribute that can be used to loop through, for instance, the attribute .columns let have the list of columns
  2. Also when plotting, I noticed that using directly the coordinates on the grid won't let your code be adaptable, so you need to 'flatten' your grid coordinates, hence the use of ax.ravel() which enable this.
  3. enumerate() is always useful to loop through an object while making available the ith element and its index at the same time.
  4. Understanding subplots in python is tricky at the beginning, so reading other people code is really helpful, I strongly advise you look at the plot done in the exemples for scikit functions (it helped a lot)

here is my code proposal :

fig, ax = plt.subplots(1, 3, sharex='col', sharey='row', figsize=(12,7))
ax = ax.ravel() 
# this method helps you to go from a 2x3 array coordinates to 
# 1x6 array, it will be helpful to use as below

for idx in range(3):
    ax[idx].hist(df.iloc[:,idx], bins=12, alpha=0.5)
    ax[idx].hist(df.iloc[:,idx+3], bins=12, alpha=0.5)
    ax[idx].set_title(df.columns[idx]+' with '+df.columns[idx+3])
    ax[idx].legend(loc='upper left')

I hope this is helpful, feel free to ask me question if you need more details :)

NOTE : re-used Alex's answer to edit my answer. Also check this matplotlib documentation for more details. In this specific case point 3 is no more relevant.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!