Reputation: 3318
I am trying to make subplots. I call many columns from a dataframe, turn them into array and plot them. I want to plot them in 4 rows, 2 columns. But I only get 1 column (you can check the image). What am I doing wrong?
Here is my code:
for column in df3: #I call the dataframe
data=df3[column].values #Turn it into an array
fig = plt.figure()
plt.subplot (4,2,1) #I want 4 rows and 2 columns
ax,_=plot_topomap(data, sensors_pos, cmap='viridis', vmin=0, vmax=100, show=False)
plt.title("KNN" + " " + column) #This is the title for each subplot
fig.colorbar(ax)
plt.show
Upvotes: 0
Views: 777
Reputation: 339052
There are several things which might cause problems in your code and it's hard to find a solution without knowing the complete code.
In your code you create several figures. However, you really want one single figure. So the figure needs to be creates outside the loop.
Then you want to create subplots, so in every loop step you need to tell matplotlib to which subplot it should plot. This can be done by ax = fig.add_subplot(4,2,n)
where n is a number which you increase in every run of the loop.
Next you call plot_topomap
. But how would plot_topomap
know what to plot where? You need to tell it, by supplying the keyword argument axes = ax
.
Finally try to set a colorbar, with the return image as argument to the axes ax
.
Of course I cannot test the following code, but it might do what you want, in case I interpreted everything well.
n = 1
fig = plt.figure()
for column in df3: #I call the dataframe
data=df3[column].values #Turn it into an array
ax = fig.add_subplot(4,2,n) #I want 4 rows and 2 columns
im,_ = plot_topomap(data, sensors_pos, cmap='viridis', vmin=0, vmax=100, show=False, axes=ax)
ax.set_title("KNN" + " " + column) #This is the title for each subplot
fig.colorbar(im, ax=ax)
n+=1
Upvotes: 1