Search code examples
python-3.xmatplotlibplotsubplotfigure

Plotting Subplots from Individual Row in Python


I have the following DataFrame:

   UserId   Gold Silver Bronze TagBased TotalBadges
0   854      1     7    22       2         30
1   5740     6    42    114     12        162
2   26       7    68    168      1        243
3   8884    14    94    229      3        337

I want to make subplots for individual Users showing their Gold, Silver and Bronze medals. Thus, in total I want 4 subplots for these 4 users.

I have tried the following code but it does not give any output.

fig = plt.figure()

ax0=fig.add_subplot(221)
ax1=fig.add_subplot(222)
ax2=fig.add_subplot(223)
ax3=fig.add_subplot(224)

for index,rows in df_tagBased.iterrows():
  df_tagBased['UserId'=='854'].plot(kind = 'hist',ax=ax0,figsize = (15,8))

Solution

  • You can do a set_index and plot the transpose:

    (df.set_index('UserId')[['Gold','Silver','Bronze']]
       .T.plot.bar(subplots=True,layout=(2,2))
    )
    

    Output:

    enter image description here