-2

Perhaps the title is misleading or someone will come along telling me that is duplicated. However after many (many) hours of browsing I haven't found anything. I want to plot multiple scatter diagrams and merge them up into a subplot for some nrows and ncols?

Assume that we have the following:

new_list=[]

for j in list(set(lala)):

df1 = df[df['Date'] == j]
df1.drop('Date', axis = 1, inplace = True)

df2 = df1.groupby('Z').mean()
df2.reset_index(inplace = True)

new_list.append(df2)

for j in range(0, len(new_list)):
plt.figure(figsize=(6, 6), dpi=80)
plt.scatter(new_list[j]['X'],new_list[j]['Y'])

and let me explain a little bit of what it does; I create a list called new_list, which contains data frames constructed in the for loop (you can ignore the construction since I'm asking for a global approach). Afterwards, I print scatter diagrams (in total as many as the number of elements of new_list) for each data frame in new_list.

Because the number of the printouts is big, I want to create subplots off these printouts to make the final image easier for the eye.

So how can I take all these scatter diagrams and merge them up into a subplot for some nrows and ncols?

smci
  • 32,567
  • 20
  • 113
  • 146
  • You defined didn’t look for hours or else you would have easily found the subplots from Matplotlib. If you just google for “Rows and columns subplots Matplotlib “, Not only you will get the official examples but also several stack overflow answers. So try once again perhaps – Sheldore Jan 26 '19 at 22:03
  • @Bazingaa I have tried many things (but certainly not everything) and I have been looking for hours. Anyways, I have seen the link you quote. However, seems that you need to write out all the frames that the list contains and I want to avoid this. Can you elaborate a little bit more please? – mayer_vietoris Jan 26 '19 at 22:08
  • So for instance, what if you have a bunch of data frames like above (I mean in my answer), and you want to create a subplot without writing explicitly all of them like the link you posted? – mayer_vietoris Jan 26 '19 at 22:09
  • How many rows and columns do you want? – Sheldore Jan 26 '19 at 22:09
  • Say nrwos=4, ncols=10 – mayer_vietoris Jan 26 '19 at 22:10
  • This was previously marked as a dupe of [How can I plot separate Pandas DataFrames as subplots?](https://stackoverflow.com/questions/22483588/how-can-i-plot-separate-pandas-dataframes-as-subplots), is that not the case? If yes it should be closed-as-dupe. – smci Jan 26 '19 at 22:54
  • Possible duplicate of [How can I plot separate Pandas DataFrames as subplots?](https://stackoverflow.com/questions/22483588/how-can-i-plot-separate-pandas-dataframes-as-subplots) – smci Jan 26 '19 at 22:55
  • Possible duplicate of [How to run a smart loop for subplots in python](https://stackoverflow.com/questions/42845887/how-to-run-a-smart-loop-for-subplots-in-python) – Andras Deak -- Слава Україні Jan 26 '19 at 23:12
  • Possible duplicate of [How to use matplotlib to create a large graph of subplots?](https://stackoverflow.com/questions/44093705/how-to-use-matplotlib-to-create-a-large-graph-of-subplots) – Thomas Kühn Jan 29 '19 at 09:43

1 Answers1

1

Assuming you have 4 rows and 10 columns, you can do something like this (just one way of doing it). Here flatten returns you a list of 40 axis objects (4 x 10) where the order is across the row: first row four columns first, then second row four columns, and so on.

fig, axes = plt.subplots(nrows=4, ncols=10)

for i, ax in enumerate(axes.flatten()):
    ax.scatter(new_list[i]['X'],new_list[i]['Y'])

If you don't want to use enumerate, alternatively you can also use the following

fig, axes = plt.subplots(nrows=4, ncols=10)
ax = axes.flatten()

for j in range(0, len(new_list)):
    ax[j].scatter(new_list[j]['X'],new_list[j]['Y'])
Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Although for pedagogical reasons I would like to ask how you come up with that? – mayer_vietoris Jan 26 '19 at 22:17
  • @mayer_vietoris: Just years of experience and working with matplotlib and exploring it's features.Read more Stack overflow answers about matplotlib. For example, you can start [here](https://stackoverflow.com/questions/tagged/matplotlib?sort=votes&pageSize=50) to get more insights into different options matplotlib offers – Sheldore Jan 26 '19 at 22:19
  • 1
    Seems that there is nothing like experience :). Thank you again. – mayer_vietoris Jan 26 '19 at 22:22