循环绘图(使用底图和 pyplot)....pyplot.clf() 的问题

发布于 2024-09-13 18:14:05 字数 1038 浏览 3 评论 0原文

我正在为一个研究项目绘制一些天气数据。该图由 18 个时间步组成。我认为完成此任务的最佳方法是为每个时间步长创建一个新图,将其保存为文件,然后为下一个时间步长创建一个新图(使用 for 循环)。

例如:


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() 的失败之间的关系?

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

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

For example:


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

The problem lies with pyplot.clf()

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声情话 2024-09-20 18:14:05

尝试创建一个新图形而不是使用 clf()。

例如

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

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

例如(完全未经测试):

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

希望能有所帮助

编辑:刚刚注意到你正在循环内绘制多边形,还有。更新了第二个示例以正确反映这一点。

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

e.g.

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

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)

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

Hope that helps a bit anyway

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文