在Matplotlib中使用组合列表的多个子图
我想做一个向量场的流图,其中包含一些我想改变的自由常数。所以我把这些常数组合起来,我可以用这个成功地一个接一个地绘制流图:在Matplotlib中使用组合列表的多个子图,matplotlib,combinations,subplot,Matplotlib,Combinations,Subplot,我想做一个向量场的流图,其中包含一些我想改变的自由常数。所以我把这些常数组合起来,我可以用这个成功地一个接一个地绘制流图: Y, X = np.mgrid[-1:10:200j, 0:10:200j] tau_x = [0.01, 0.1, 1., 10.] tau_y = [0.01, 0.1, 1., 10.] alpha = [0.01, 0.1, 1., 10.] r = [0.1, 0.01, 0.001] K = [0.1, 0.5, 1.0, 1.5] combinations
Y, X = np.mgrid[-1:10:200j, 0:10:200j]
tau_x = [0.01, 0.1, 1., 10.]
tau_y = [0.01, 0.1, 1., 10.]
alpha = [0.01, 0.1, 1., 10.]
r = [0.1, 0.01, 0.001]
K = [0.1, 0.5, 1.0, 1.5]
combinations_list = list(itertools.product(tau_x,tau_y,alpha,r,K))
for a in combinations_list:
(tau_x, tau_y, alpha, r, K) = a
Fx = (1/tau_x) * ( (-8/3)*(2*r-alpha)*(X-1) + K*X )
Fy = (2/(tau_y*X**(3/2))) * ( -2*(Y-1) + 3*Y*(X-1)/X + K*X*Y )
fig, ax = plt.subplots()
strm = ax.streamplot(X, Y, Fx, Fy, linewidth=0.5)
plt.show()
现在,因为我们正在讨论大量的组合,我想制作一个带有子图的图(每个图9个,但可能更多),这将大大减少图的数量
注意:我对每次看到一个图形感兴趣,这就是为什么plt.show()在循环中,以避免一次打开所有图形
编辑:以下是我将代码更改为的ImportanceOfBeingErnest建议
Y, X = np.mgrid[-1:10:200j, 0:10:200j]
tau_x = [0.01, 0.1, 1., 10.]
tau_y = [0.01, 0.1, 1., 10.]
alpha = [0.01, 0.1, 1., 10.]
r = [0.1, 0.01, 0.001]
K = [0.1, 0.5, 1.0, 1.5]
combinations_list = list(itertools.product(tau_x,tau_y,alpha,r,K))
length = len(combinations_list)
N = 9 #number of subplots per figure
for i in range(0,100):
subset = combinations_list[9*i:9*i+9]
fig = plt.figure()
j = 1
for a in subset:
(tau_x, tau_y, alpha, r, K) = a
Fx = (1/tau_x) * ( (-8/3)*(2*r-alpha)*(X-1) + K*X )
Fy = (2/(tau_y*X**(3/2))) * ( -2*(Y-1) + 3*Y*(X-1)/X + K*X*Y )
ax = fig.add_subplot(3,3,j)
ax.streamplot(X, Y, Fx, Fy, linewidth=0.5)
++j
plt.show()
但是它只是以一种奇怪的方式绘制每个子集的第一个,并且向量中有颜色。您没有正确地更新
j
++j
不更新j
的值。如果将++j
替换为j+=1
或j=j+1
,代码将正常工作。两者是等价的
for i in range(0,100):
subset = combinations_list[9*i:9*i+9]
fig = plt.figure()
j = 1
for a in subset:
(tau_x, tau_y, alpha, r, K) = a
Fx = (1/tau_x) * ( (-8/3)*(2*r-alpha)*(X-1) + K*X )
Fy = (2/(tau_y*X**(3/2))) * ( -2*(Y-1) + 3*Y*(X-1)/X + K*X*Y )
ax = fig.add_subplot(3,3,j)
ax.streamplot(X, Y, Fx, Fy, linewidth=0.5)
j += 1 # <--- change here
范围(0100)内的i的:
子集=组合_列表[9*i:9*i+9]
图=plt.图()
j=1
对于一个子集中的:
(tau_x,tau_y,alpha,r,K)=a
Fx=(1/tau_x)*(-8/3)*(2*r-alpha)*(x-1)+K*x)
Fy=(2/(tau_y*X**(3/2))*(-2*(y-1)+3*y*(X-1)/X+K*X*y)
ax=图添加_子批次(3,3,j)
最大流线图(X,Y,Fx,Fy,线宽=0.5)
j+=1#可以使用两个循环。其中一个选择每个图形要显示的组合子集,另一个在该子集上循环,并在其各自的子地块中绘制每个组合。@ImportanceOfBeingErnest正如您在“编辑”中看到的,我已经按照您的建议进行了操作,但仍有问题。。。