在循环中绘图(使用底图和 pyplot)....pyplot.clf() 的问题 [英] Plotting in a loop (with basemap and pyplot)....problems with pyplot.clf()

查看:58
本文介绍了在循环中绘图(使用底图和 pyplot)....pyplot.clf() 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个研究项目绘制一些天气数据.该图包括18个时间步.我决定实现这一点的最佳方法是为每个时间步制作一个新图,将其保存为一个文件,然后为下一个时间步创建一个新图(使用 for 循环).

I am plotting some weather data for a research project. The plot consists of 18 timesteps. I decided the best way to accomplish this was to make a new plot for each timestep, save it a file, and create a new plot for the next timestep (using a for loop).

例如:


map_init  #[Basemap Instance]
extra_shapes  #[Basemap.readshapefile object]

for i in range(timesteps):
    #plot the weather data for current timestep to current plot
    map_init.imshow(data[i])

    # extra_shapes are county boundaries.  Plot those as polygons
    pyplot.Polygon(map_init.extra_shapes[i])

    # Plot the state boundaries (in basemap)
    map_init.drawstates()

    # add a colorbar
    pyplot.colorbar()

    # Save the figure
    pyplot.savefig(filepath)

    #close figure and loop again (if necessary)
    pyplot.clf()

问题出在 pyplot.clf()

除了一件事之外,代码有效.只有第一个情节出人意料.随后的每个图都缺少 extra_shapes (即没有县边界).我不了解 pyplot.clf()的存在与 pyplot.Polygon()吗?

The code works except for one thing. Only the first plot comes out as expected. Every subsequent plot is missing the extra_shapes (ie no county boundaries). I do not understand the relation between presence of pyplot.clf() and the failure of pyplot.Polygon() ?

如果删除,则会绘制 extra_shapes ,但是每个图都有多个颜色条(取决于 i 的值).pyplot.clf() 的唯一原因是避免在最终图中有 18 个颜色条.有没有一种方法可以强制每个图只有一个色标?

If removed, extra_shapes is plotted, but then every plot has multiple colorbars (depending on the value of i). The only reason pyplot.clf() is there is to avoid having 18 colorbars in the final plot. Is there a way to force one and only one colorbar per plot?

推荐答案

尝试制作一个新图形,而不要使用clf().

Try making a new figure instead of using clf().

例如

for i in range(timesteps):
    fig = pyplot.figure()
    ...
    fig.savefig(filepath)

或者(更快)您可以只更新图像对象中的数据(由imshow()返回).

Alternatively (and faster) you could just update the data in your image object (returned by imshow()).

例如类似(完全未经测试):

e.g. something like (completely untested):

map_init  #[Basemap Instance]
extra_shapes  #[Basemap.readshapefile object]


#plot the weather data for current timestep to current plot
img = map_init.imshow(data[0])

# extra_shapes are county boundaries.  Plot those as polygons
plygn = pyplot.Polygon(map_init.extra_shapes[0])

# Plot the state boundaries (in basemap)
map_init.drawstates()

# add a colorbar
pyplot.colorbar()

for i in range(timestamps):
    img.set_data(data[i])
    plygn.set_xy(map_init.extra_shapes[i])
    pyplot.draw()
    pyplot.savefig(filepath)

但是,该方法可能无法与底图配合使用.我可能还不太记得重新绘制图形的正确方法,但是我很确定它只是plt.draw()...

However, there's a chance that that method might not play well with basemap. I may also be misremembering the correct way to redraw the figure, but I'm fairly sure it's just plt.draw()...

希望还是有帮助的

刚刚注意到,您也在循环内绘制多边形.更新了第二个示例以正确反映这一点.

Just noticed that you're drawing your polygons inside of a loop, as well. Updated the second example to properly reflect that.

这篇关于在循环中绘图(使用底图和 pyplot)....pyplot.clf() 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆