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

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

问题描述

每次尝试一次拟合过程时,我都会尝试刷新gui中的一些图.而且,这些图位于可调整大小的框架中,因此在调整大小后需要重新绘制轴和标签等.所以想知道是否有人知道如何使用plot.figure.canvas.copy_from_bboxblit之类的东西来更新图形的边.这似乎仅复制并变色绘图区域的背景(绘制线条的背景),而不复制到图形或图形的侧面(标记和刻度线所在的位置).我一直在尝试通过反复试验和阅读mpl文档来更新图形,但是到目前为止,我的代码由于self.this_plot.canvas_of_plot..etc.etc.. .plot.figure.canvas.copy_from_bbox之类的东西而变得极为复杂,这可能太令人费解了. 我知道我的语言可能会稍差一些,但是我一直在尝试阅读matplotlb文档,并且Figure,canvas,graph,plot,figure.Figure等之间的差异开始让我回避.所以我的首要问题是:

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

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

2-就图形用户界面,图形,画布等在GUI中所覆盖的区域而言,有什么区别.

非常感谢您的帮助.

解决方案

一开始这一切肯定会造成混乱!

首先,如果要链接刻度线等,则使用blitting并没有多大意义.如果只有某些内容发生更改,则发条只是避免重绘所有内容的一种方法.如果一切都在变化,那么使用blitting毫无意义.只需重新绘制情节即可.

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

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

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

简而言之,有两个单独的层.一种是将事物分组为绘图时您会担心的部分(例如图形,轴,轴,线等),另一种则涉及一般的渲染和绘制(画布和渲染器).

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

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

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

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

希望这使事情变得更加清晰.

如果您确实确实想使用blit来更新刻度线标签等,则需要获取并还原包括它们的整个区域.该区域的获取有些棘手,因为直到绘制时间 后才确切知道(由于乳胶的支持,在matplotlib中渲染文本比渲染其他对象要复杂得多).您可以做到,如果确实需要它,我会很高兴地举一个例子,但是与仅绘制所有内容相比,它通常不会产生速度优势. (唯一的例外是,如果您仅更新具有很多子图的图形中的一个子图.)

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 - How do you update the ticks and labels around a matplotlib plot.

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

2 - What is the difference between a plot, figure, canvas, etc. in regards to the area which they cover in the GUI.

Thank you very much for the help.

解决方案

All this can certainly be rather confusing at first!

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.

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

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.

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

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.

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.

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天全站免登陆