python matplotlib blit 到图形的轴或边? [英] python matplotlib blit to axes or sides of the figure?

查看:41
本文介绍了python matplotlib blit 到图形的轴或边?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我通过一个拟合程序时,我都试图刷新我在 gui 中的一些图.此外,这些图位于可以调整大小的框架内,因此在调整大小后需要重新绘制轴和标签等.所以想知道是否有人知道如何使用 plot.figure.canvas.copy_from_bboxblit 之类的东西来更新图形的侧面.这似乎只复制和 blit 图形区域的背景(正在绘制线条的地方),而不是图形或图形的侧面(标签和刻度线所在的地方).我一直在尝试通过反复试验和阅读 mpl 文档来更新我的图表,但到目前为止,我的代码已经变得非常复杂,例如 self.this_plot.canvas_of_plot..etc.etc...plot.figure.canvas.copy_from_bbox... 这可能太复杂了.我知道我的语言可能有点偏差,但我一直在尝试通读 matplotlb 文档,并且图、画布、图形、绘图、图等之间的差异开始回避我.所以我的首要问题是:

I'm trying to refresh some plots that I have within a gui everytime I go once through a fitting procedure. Also, these plots are within a framw which can be resized, so the axes and labels etc need to be redrawn after the resizing. So was wondering if anyone knew how to update the sides of a figure using something like plot.figure.canvas.copy_from_bbox and blit. This appears to only copy and blit the background of the graphing area (where the lines are being drawn) and not to the sides of the graph or figure (where the labels and ticks are). I have been trying to get my graphs to update by trial and error and reading mpl documentation, but so far my code has jst become horrendously complex with things like self.this_plot.canvas_of_plot..etc.etc.. .plot.figure.canvas.copy_from_bbox... which is probably far too convoluted. I know that my language might be a little off but I've been trying to read through the matplotlb documentation and the differences between Figure, canvas, graph, plot, figure.Figure, etc. are starting to evade me. So my first and foremost question would be:

1 - 如何更新 matplotlib 图周围的刻度和标签.

其次,因为我想更好地了解这个问题的答案,

and secondly, since I would like to have a better grasp on what the answer to this question,

2 - 绘图、图形、画布等在 GUI 中覆盖的区域有什么区别.

非常感谢您的帮助.

推荐答案

所有这些一开始肯定会令人困惑!

All this can certainly be rather confusing at first!

首先,如果您要链接刻度线等,则使用块传输没有多大意义.如果只有某些事情发生了变化,Blitting 只是一种避免重新绘制所有内容的方法.如果一切都在改变,那么使用 blitting 就没有意义了.只需重新绘制情节.

To begin with, if you're chaining the ticks, etc, there isn't much point in using blitting. Blitting is just a way to avoid re-drawing everything if only some things are changing. If everything is changing, there's no point in using blitting. Just re-draw the plot.

基本上,你只需要 fig.canvas.draw()plt.draw()

Basically, you just want fig.canvas.draw() or plt.draw()

无论如何,要回答您的第一个问题,在大多数情况下,您不需要手动更新它们.如果您更改轴限制,它们会自行更新.您遇到了问题,因为您只是在轴的内部进行 blitting 而不是重新绘制绘图.

At any rate, to answer your first question, in most cases you won't need to update them manually. If you change the axis limits, they'll update themselves. You're running into problems because you're blitting just the inside of the axes instead of redrawing the plot.

至于你的第二个问题,一个很好的详细概述是艺术家教程://matplotlib.org/users/artists.html">Matplotlib 用户指南.

As for your second question, a good, detailed overview is the Artist Tutorial of the Matplotlib User's Guide.

简而言之,有两个独立的层.一个处理将事物分组到绘图时您会担心的部分(例如图形、轴、轴、线等),另一个处理一般的渲染和绘图(画布和渲染器).

In a nutshell, there are two separate layers. One deals with grouping things into the parts that you'll worry about when plotting (e.g. the figure, axes, axis, lines, etc) and another that deals with rendering and drawing in general (the canvas and renderer).

您在 matplotlib 图中可以看到的任何内容都是 Artist.(例如文本、线条、轴,甚至图形本身.)艺术家 a) 知道如何绘制自己,并且 b) 可以包含其他艺术家.

Anything you can see in a matplotlib plot is an Artist. (E.g. text, a line, the axes, and even the figure itself.) An artist a) knows how to draw itself, and b) can contain other artists.

为了让艺术家自己画画,它使用渲染器(您几乎永远不会直接接触的后端特定模块)在 FigureCanvas 又名画布"(围绕任一基于矢量的页面或像素缓冲区).要绘制图形中的所有内容,请调用 canvas.draw().

For an artist to draw itself, it uses the renderer (a backend-specific module that you'll almost never touch directly) to draw on a FigureCanvas a.k.a. "canvas" (an abstraction around either a vector-based page or a pixel buffer). To draw everything in a figure, you call canvas.draw().

因为艺术家可以是其他艺术家的群体,所以事物之间存在等级关系.基本上是这样的(显然,这会有所不同):

Because artists can be groups of other artists, there's a hierarchy to things. Basically, something like this (obviously, this varies):

Figure
    Axes (0-many) (An axes is basically a plot)
        Axis (usually two) (x-axis and y-axis)
            ticks
            ticklabels
            axis label
         background patch
         title, if present
         anything you've plotted, e.g. Line2D's

无论如何,希望这能让事情变得更清晰.

Hopefully that makes things a touch clearer, anyway.

如果您确实想使用块传输来更新刻度标签等,则需要抓取并恢复包括它们在内的完整区域.这个区域有点难获得,因为它直到 绘制时间之后才完全知道(由于乳胶支持等,在 matplotlib 中渲染文本比渲染其他东西更复杂).你可以做到,如果这真的是你想要的,我很乐意举个例子,但它通常不会比仅仅绘制所有东西产生速度优势.(例外情况是,如果您只更新包含大量子图的图中的一个子图.)

If you really do want to use blitting to update the tick labels, etc, you'll need to grab and restore the full region including them. This region is a bit tricky to get, because it isn't exactly known until after draw-time (rendering text in matplotlib is more complicated than rendering other things due to latex support, etc). You can do it, and I'll be glad to give an example if it's really what you want, but it's typically not going to yield a speed advantage over just drawing everything. (The exception is if you're only updating one subplot in a figure with lots of subplots.)

这篇关于python matplotlib blit 到图形的轴或边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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