简体   繁体   中英

“SuperAxis” in matplotlib subplot

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比较矩阵

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:

像这样的东西

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: 在此输入图像描述

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()

在此输入图像描述

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()

在此输入图像描述

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