简体   繁体   中英

Is it possible to generate a plot shown in the enclosed figure using matplotlib?

I want to plot a series of line graphs on subplot grid using matplotlib. However, the upper right section of the grid is a repeat of the lower left corner so I want to leave out the top-right corner. I enclose a png image of what I am looking for. Is it possible to draw something like this using matplotlib?

It would also be useful to have the axes labeled but using subplots that's not difficult.

在此输入图像描述

For those interested, this is a series of phase plots for a chemical reaction system with four variables. I'm plotting each variable against the other. Note the main diagonal represents the variable plotted against itself, which could probably be left out as well.

The code I use at the moment might not be so useful to look out but this is what I used to do a full 4 by 4 grid, I'd like to do a 4 by 4 with the top-right block left out. Note that if I don't plot some of the grid cells I still get the axes showing, maybe I can turn of the plotting of the axes for those subplots?

plt.subplots(4,4,figsize=(9,6))
slist = ['S1', 'S2', 'S3', 'S4']
count = 1
for i in range (4):
    for j in range (4):       
        r.reset()
        m = r.simulate (0, 80, 2000, [slist[i], slist[j]])
        plt.subplot (4,4,count)
        plt.plot (m[:,0], m[:,1])
        count += count
plt.show()

The following line calls a simulator which returns the two columns for the currently selected variables. It's not efficient as I could be because I should really be calling it once with all variables then picking out the ones I want at each plot. But this was just a prototype to experiment with plotting. Optimization can occur later. The variable r is a reference to the simulstor.

m = r.simulate (0, 80, 2000, [slist[i], slist[j]])

I just discovered this:

  ax = plt.subplot (4,4,count)
  plt.plot (m[:,0], m[:,1])
  if count in [2,3,4,7,8,12]:
     ax.set_visible(False)

Which is simple but would need to be generalized.

The final code which works well and is based on the answer by Kota Mori which also includes axes labeling is:

slist = ['S1', 'S2', 'S3', 'S4']
m = r.simulate (0, 80, 2000, slist)
fig = plt.figure(figsize=(9,6))
fig.tight_layout() 
count = 0
for i in range(4):
    for j in range(4):
        count += 1
        if i >= j:
           ax = fig.add_subplot(4, 4, count)
           ax.set_xlabel (slist[i])
           ax.set_ylabel (slist[j])
           ax.plot (m[:,i], m[:,j])

You can use add_subplot to add only cells that you need.

fig = plt.figure(figsize=(9, 6))

count = 0
for i in range(4):
    for j in range(4):
        count += 1
        if i >= j:
            ax = fig.add_subplot(4, 4, count)
            ax.text(0.5, 0.5, str(count))

Can you please try this?

plt.subplots(4,4,figsize=(9,6))
slist = ['S1', 'S2', 'S3', 'S4']
for i in range (4):
    for j in range (4): 
        if i>=j:
          pos = i*4+j+1
          r.reset()
          m = r.simulate (0, 80, 2000, [slist[i], slist[j]])
          plt.subplot (4,4,pos)
          plt.plot (m[:,0], m[:,1])
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM