2

I'm using subplot to evaluate some conditions, with that I create two arrays with my test conditions for example:

A=[ -2, -.1, .1, 2 ]
B=[ -4, -.2, .2, 4 ]

Then I evaluate it in a loop:

for i,a in enumerate(A):
    for j,b in enumerate(B):
        U_E[i][j]=u(t,b,a)

And to finish I plot it:

f, axarr = plt.subplots(len(A),len(B), sharex=True, sharey='row')
for i in range(len(A)):
    for j in range(len(B)):
        axarr[i, j].plot(t, U_E[i][j])

It's nice and I'm almost happy with that. :-) It's something like:

Matplotlib comparative matrix

But I would love to add the values of A and B as a "superaxis" to quickly see that B and A are for each plot.

Something like:

Something like this

Lin
  • 1,145
  • 11
  • 28
  • You forgot to tell us what a "superaxis" would be. Can I annotate an axis with the word "superaxis" to make it a "superaxis"? And what if I wanted a "hyperaxis" instead? ;-) – ImportanceOfBeingErnest May 21 '17 at 23:07
  • I thought I made it clear, the superaxis would be two orthogonal axis over the whole image above where the `x` coordinate line will the the content of `B` and the `y` coordinate line would be the content of `A`. – Lin May 21 '17 at 23:11
  • @ImportanceOfBeingErnest , I have added an image with my objective, sorry for the bad edition I'm not good with image editors at all. (btw, I don't want the "superaxis" numbers covering the values of each matrix cell image). – Lin May 21 '17 at 23:42
  • @Lin you can workaround using [parasite axes](http://matplotlib.org/examples/axes_grid/demo_parasite_axes2.html), but since you want is not exactly an axes, since it would jump from -4 to -.2 without any values in between I'd not recommend using that. What I think is the better solution is to use `annotate` to make things more clear. – Vinícius Figueiredo May 22 '17 at 03:34

2 Answers2

3

I'd suggest using axarr[i,j].text to write the A and B parameters inside each subplot:

for i in range(len(A)):
    for j in range(len(B)):
        axarr[i,j].plot(x, y*j) # just to make my subplots look different
        axarr[i,j].text(.5,.9,"A="+str(A[j])+";B="+str(B[i]), horizontalalignment='center', transform=axarr[i,j].transAxes)

plt.subplots_adjust(hspace=0,wspace=0)
plt.show()

transform=axarr[i,j].transAxes assures we are taking the coordinates as relative to each one of the axes:enter image description here

And of course, if you don't think decoupling the subplots is a big issue for visualization in your case:

for i in range(len(A)):
for j in range(len(B)):
    axarr[i,j].plot(x, y*j)
    axarr[i,j].set_title("A="+str(A[i])+";B="+str(B[j]))

#plt.subplots_adjust(hspace=0,wspace=0)
plt.show()

enter image description here

Vinícius Figueiredo
  • 6,300
  • 3
  • 25
  • 44
2

It seems you want to just label the axes. This can be done using .set_xlabel() and .set_ylabel().

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = 10,7

t = np.linspace(0,1)
u = lambda t, b,a : b*np.exp(-a*t)

A=[ -2, -.1, .1, 2 ]
B=[ -4, -.2, .2, 4 ]

f, axarr = plt.subplots(len(A),len(B), sharex=True, sharey='row')
plt.subplots_adjust(hspace=0,wspace=0)
for i in range(len(A)):
    axarr[i, 0].set_ylabel(A[i], fontsize=20, fontweight="bold")
    for j in range(len(B)):
        axarr[i, j].plot(t, u(t,B[j],A[i]))
        axarr[-1, j].set_xlabel(B[j],  fontsize=20,fontweight="bold")

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712