简体   繁体   English

如何保存箭袋图

[英]How to save a quiver plot

Let me show you my code让我向你展示我的代码

array['fieldv'].shape
(112, 750, 750, 2)

and if I a plot an element, this image appear如果我绘制一个元素,则会出现此图像

%matplotlib inline
U, V  = array['fieldv'][10,:,:,0], array['fieldv'][10,:,:,1]
plt.quiver(U[::4,::4], V[::4, ::4])

在此处输入图片说明

Now my question is: how can I save in a directory all the quiver lots of the 112 elements?现在我的问题是:如何将 112 个元素的所有箭袋保存在一个目录中? I show you my code that doesn't work我给你看我的代码不起作用

%matplotlib inline

i = 0
while i != len(array['fieldv'][0]):
    U, V  = array['fieldv'][i:,:,0], array['fieldv'][i,:,:,1]
    fig = plt.quiver(U[::4,::4], V[::4, ::4])

    filename = '/home/rr/workspace/TEST/OUTPUT/vectorf/'
    fig.savefig(filename + 'TEST_' + str(i) + '.png')
    i += 1

plt.quiver does not return a Figure instance (check type(fig) and you'll see that plt.quiver actually creates an instance of Quiver). plt.quiver不返回 Figure 实例(检查type(fig)并且您会看到plt.quiver实际上创建了 Quiver 的实例)。 The Quiver object does not have a savefig method and so fig.savefig will not work. Quiver 对象没有savefig方法,因此fig.savefig将不起作用。 However, you should just be able to swap the the fig.savefig command with plt.savefig .但是,您应该能够将fig.savefig命令与plt.savefig

As a more general comment I think it's more appropriate to use a for loop here, rather than a while statment.作为更一般的评论,我认为在这里使用 for 循环而不是 while 语句更合适。 I'd do so as for i in range (array['fieldv'].shape[0]): . for i in range (array['fieldv'].shape[0]): I also think .format is a bit neater: plt.savefig('{}TEST_{}.png'.format(filename, i)) .我也认为.format更简洁一点: plt.savefig('{}TEST_{}.png'.format(filename, i))

Putting that altogether I'd do:把它完全放在我会做的:

filename = '/home/rr/workspace/TEST/OUTPUT/vectorf/'

for i in range(array['fieldv'].shape[0]):
    U, V  = array['fieldv'][i:,:,0], array['fieldv'][i,:,:,1]
    Q = plt.quiver(U[::4,::4], V[::4, ::4])

    plt.savefig('{}TEST_{}.png'.format(filename, i))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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