Home > other >  Make separate sub plot for each id
Make separate sub plot for each id

Time:10-20

I am trying to extract information of largest 10 values from my csv based on column 'id' and plot them together in a subplot(each subplot for each id).

#data : contains original csv
#data_big : contains refined data

data_big = data_big.nlargest(10, 'a') #searched first 10 largest entries from 'a'


fig, ax = plt.subplots(nrows=5, ncols=2, figsize=(12, 16))
fig.subplots_adjust(hspace=0.5)

# match data_big['id] and data['id]
for i in data_big['id']:
    if i in data['id']:
        data_big1 = data[data['id'] == i]
        #count unique values in column id
        count = data_big1['id'].value_counts()
        #print(count)
        for k in range(5):
            for n in range(2):    
                data_big1.plot(x='TIMESTEP', y='a', ax=ax[k,n], label='big_{}'.format(i))
                ax[k,n].set_xlabel('TIMESTEP')
                ax[k,n].set_ylabel('a')
                ax[k,n].legend()

plt.show()

This code genrates a subplot of 5 rows and 2 columns with 10 ids in each plot.

What I want is each id in each plot (not 10 ids in each plot).

Can I get some help to figure out what did I miss?

Thanks

CodePudding user response:

You have to enumerate data_big and use this value to calculate k,n

And plot it without using for-loops.

for number, i in enumerate(data_big['id']):

    k = number // 2
    n = number % 2

    if i in data['id']:

        data_big1 = data[data['id'] == i]
        #count unique values in column id
        count = data_big1['id'].value_counts()
        #print(count)

        # plot it only once - without using `for`-loops
        data_big1.plot(x='TIMESTEP', y='a', ax=ax[k,n], label='big_{}'.format(i))

        ax[k,n].set_xlabel('TIMESTEP')
        ax[k,n].set_ylabel('a')
        ax[k,n].legend()
  • Related