0

I need to create a bunch of graphs based on a lot of previous code. I cant "hardcode" the axes variables because the spacing would be all wrong.

I have an arbitrary amount of axes that each need formatting as part of the whole, so I cannot create them individually.

What I have so far: I read How do you create different variable names while in a loop? , but as with half of all questions, it simply doesn't get answered, instead receiving some vaguely related code.

What I have:

ax1 = plt.subplot2grid((m, n), (j, k), colspan=1, rowspan=1)
ax3 = plt.subplot2grid((m, n), (j+1, k+1), colspan=1, rowspan=1)
ax4 = plt.subplot2grid((m, n), (j+2, k+2), colspan=1, rowspan=1)
ax5 = plt.subplot2grid((m, n), (j+3, k+3), colspan=1, rowspan=1)

ax1.plot(df['colA'], df['colB'])
etc

What I think I need

axcount = anyinteger
for i in np.arange(axcount):
  ax & str(i+1) = plt.subplot2grid((m, n), (j + (i+1 - 1), k + (i+1 - 1)), colspan=1, rowspan=1)

  ax & str(i+1) .plot(df['colA'], df['col' & chr(i+66)])

which is necessary because I will need to spec each axi's plot.

I am extremely new to this whole python thing, and from what Ive learned so far, it can do basically anything if you can find the syntax/function for it. Im hoping someone has the syntax/function that would allow me to solve my problem.

22134484
  • 113
  • 8
  • `m`,`n`,`j` and `k` are undefined in your code. Maybe it would help to show the desired geometry of the axes that you want to create? – Diziet Asahi Aug 13 '19 at 11:15
  • Are you trying to create a pair-wise scatter plot from a pandas dataframe? – James Aug 13 '19 at 11:20
  • @DizietAsahi m,n,j,k are integers. Doesnt matter what their value is (j and k will probably be 0). It is determined from prev code. For arguments sake, m=10, n=10, j = 0, k=0. but m and n could also be 12 and 123583 – 22134484 Aug 13 '19 at 11:30
  • 1
    Your code only creates axes on the diagonal, is that what you are trying to do? Or are you trying to fill the grid with as many axes as required? – Diziet Asahi Aug 13 '19 at 11:35
  • Im still trying to figure how exactly my bigger loops will look, but that is not my main issue. That i havent define mnjk or that the loop itself only plots diags at the moment is secondary issues I have. The primary issue still stands, regardless of the other "little things", is that " ax & str(i+1) = " is not valid syntax or logic for that matter – 22134484 Aug 13 '19 at 12:02
  • 1
    Suggest you just use plt.subplots outside your loop which will return an mxn array of axes handles that you can then access via axs[i, j].plot. There is no need to “name” each axes separately. – Jody Klymak Aug 13 '19 at 14:54

1 Answers1

0

It sounds like you might want to put each of the axes instances into a list, so that you can loop through the list and modify each axes properties that way?

fig = plt.figure()
m = 4
n = 4
ax_all = []
for j in range(m):
    for k in range(n):
        ax = plt.subplot2grid((m, n), (j, k), colspan=1, rowspan=1)
        ax_all.append(ax)

for ax in ax_all:
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
LABarnard
  • 290
  • 2
  • 9
  • for arguments sake, I may have 3 axes, or 3000000. I cant do it in a list if the list itself and the objects it contains isnt programatically created. – 22134484 Aug 13 '19 at 11:28
  • OK. I've edited the above so that it is fully programmatic, with the list of axes generated within the loop. You can change the `j` and `k` loops so that only the axes you want are created. You might also want to look at the `get_axes()` method of `Figure` instances, which returns a list of all axes in a figure. – LABarnard Aug 13 '19 at 11:49
  • Ill give it a go, it makes sense. Will report back soon – 22134484 Aug 13 '19 at 12:06
  • Good stuff. It works 100%. There was no need for me to put them all into a list to call later, I simply specd the axes in the first loop. Small adjustments were made to the loop to ensure the graphs ended up where they should on the grid. – 22134484 Aug 14 '19 at 06:07