可视化 2 个参数及其结果
Visualising 2 parameters and their results
有 2 个参数我想尝试不同的值:
a = [0.0, 0.5, 0.6] # len == 3
b = [0.0, 0.02 , 0.05, 0.1] # len == 4
对于a的每个值,尝试b的每个值。
这带有 3 * 4 = 12 个不同的结果。
我的数据采用
格式
res = [(0.0, 0.0, res1), (0.0, 0.02, res2), ...]
有什么方法可以巧妙地将其形象化吗?我在考虑 contour/heat 地图或 3d 平面,但遗憾的是我无法让它工作。
有很多不同的选择。在任何情况下,第一步都需要将您的 res
列表转换为一个 numpy 数组。
对于 imshow
、pcolor(mesh)
或 contourf
等许多绘图,您需要三个二维数组,您可以通过对输入数组进行整形来获得它们(假设它是正确订购)。
以下显示了您的一些选择:
res = [(0.0, 0.0, 0.5), (0.0, 0.02, 0.7), (0.0, 0.05, 0.6), (0.0, 0.10, 0.8),
(0.5, 0.0, 0.4), (0.5, 0.02, 0.6), (0.5, 0.05, 0.5), (0.5, 0.10, 0.7),
(0.6, 0.0, 0.3), (0.6, 0.02, 0.5), (0.6, 0.05, 0.4), (0.6, 0.10, 0.6)]
import matplotlib.pyplot as plt
import numpy as np
res = np.array(res)
A = res[:,0].reshape(3,4) #A in y direction
B = res[:,1].reshape(3,4)
Z = res[:,2].reshape(3,4)
fig, ((ax, ax2), (ax3, ax4)) = plt.subplots(2,2)
#imshow
im = ax.imshow(Z, origin="lower")
ax.set_xticks(range(len(Z[0,:])))
ax.set_yticks(range(len(Z[:,0])))
ax.set_xticklabels(B[0,:])
ax.set_yticklabels(A[:,0])
#pcolormesh, first need to extend the grid
bp = np.append(B[0,:], [0.15])
ap = np.append(A[:,0], [0.7])
Bp, Ap = np.meshgrid(bp, ap)
ax2.pcolormesh(Bp, Ap, Z)
#contour
ax3.contourf(B, A, Z, levels=np.linspace(Z.min(), Z.max(),5))
#scatter
ax4.scatter(res[:,1], res[:,0], c=res[:,2], s=121)
ax.set_title("imshow")
ax2.set_title("pcolormesh")
ax3.set_title("contourf")
ax4.set_title("scatter")
plt.tight_layout()
fig.colorbar(im, ax=fig.axes, pad=0.05)
plt.show()
有 2 个参数我想尝试不同的值:
a = [0.0, 0.5, 0.6] # len == 3
b = [0.0, 0.02 , 0.05, 0.1] # len == 4
对于a的每个值,尝试b的每个值。 这带有 3 * 4 = 12 个不同的结果。
我的数据采用
格式res = [(0.0, 0.0, res1), (0.0, 0.02, res2), ...]
有什么方法可以巧妙地将其形象化吗?我在考虑 contour/heat 地图或 3d 平面,但遗憾的是我无法让它工作。
有很多不同的选择。在任何情况下,第一步都需要将您的 res
列表转换为一个 numpy 数组。
对于 imshow
、pcolor(mesh)
或 contourf
等许多绘图,您需要三个二维数组,您可以通过对输入数组进行整形来获得它们(假设它是正确订购)。
以下显示了您的一些选择:
res = [(0.0, 0.0, 0.5), (0.0, 0.02, 0.7), (0.0, 0.05, 0.6), (0.0, 0.10, 0.8),
(0.5, 0.0, 0.4), (0.5, 0.02, 0.6), (0.5, 0.05, 0.5), (0.5, 0.10, 0.7),
(0.6, 0.0, 0.3), (0.6, 0.02, 0.5), (0.6, 0.05, 0.4), (0.6, 0.10, 0.6)]
import matplotlib.pyplot as plt
import numpy as np
res = np.array(res)
A = res[:,0].reshape(3,4) #A in y direction
B = res[:,1].reshape(3,4)
Z = res[:,2].reshape(3,4)
fig, ((ax, ax2), (ax3, ax4)) = plt.subplots(2,2)
#imshow
im = ax.imshow(Z, origin="lower")
ax.set_xticks(range(len(Z[0,:])))
ax.set_yticks(range(len(Z[:,0])))
ax.set_xticklabels(B[0,:])
ax.set_yticklabels(A[:,0])
#pcolormesh, first need to extend the grid
bp = np.append(B[0,:], [0.15])
ap = np.append(A[:,0], [0.7])
Bp, Ap = np.meshgrid(bp, ap)
ax2.pcolormesh(Bp, Ap, Z)
#contour
ax3.contourf(B, A, Z, levels=np.linspace(Z.min(), Z.max(),5))
#scatter
ax4.scatter(res[:,1], res[:,0], c=res[:,2], s=121)
ax.set_title("imshow")
ax2.set_title("pcolormesh")
ax3.set_title("contourf")
ax4.set_title("scatter")
plt.tight_layout()
fig.colorbar(im, ax=fig.axes, pad=0.05)
plt.show()