tandem
tandem

Reputation: 2238

overlapping subplots for matplotlib

I am trying to get subplots of 5 rows and two columns working in matplotlib. The variable X indicates a dictionary. [*X] is giving keys available in the dictionary. Each key should be present in a different row.

idx = 0
axes = []
for key, val in X.items():
    axes.append(plt.subplot(len([*X]),1,idx+1))
    axes[idx].scatter(X[key], Y[key], color='r')
    axes[idx].set_title(key)
    axes[idx].set_xlabel(title)
    axes[idx].set_ylabel('QoS')#, color='g')
    axes[idx].spines['right'].set_visible(False)
    axes[idx].spines['top'].set_visible(False)
    axes[idx].xaxis.set_ticks_position('bottom')
    axes[idx].yaxis.set_ticks_position('left')
    axes[idx].set_ylim([0,1])
    axes.append(plt.subplot(len([*X]),2,idx+1))
    tmp = idx+1
    axes[tmp].scatter(X[key], Y1[key], color='r')
    axes[tmp].set_title(key)
    axes[tmp].set_xlabel(title)
    axes[tmp].set_ylabel('Power', color='g')
    axes[tmp].spines['right'].set_visible(False)
    axes[tmp].spines['top'].set_visible(False)
    axes[tmp].xaxis.set_ticks_position('bottom')
    axes[tmp].yaxis.set_ticks_position('left')
    #axes[idx].set_ylim([0,1])
    idx+=1

Intuitively, I thought it should be this way, assuming len([*X]) is 5.

 511, 521
 512, 522
 513, 523
 513, 523
 514, 525

With the current setup, I get it this way. enter image description here What am I missing?

Upvotes: 0

Views: 447

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339052

This seems a bit convoluted. To see how subplots are defined see In Matplotlib, what does the argument mean in fig.add_subplot(111)?

However, here it may make sense to not define each subplot individually, but instead create them all at once.

fig, axes = plt.subplots(nrows=5, ncols=2)
for (key, val), axrow in zip(X.items(), axes):
    axrow[0].scatter(X[key], Y[key])
    axrow[1].scatter(X[key], Y1[key])

Upvotes: 1

Related Questions