matplotlib.pyplot.axes.bbox 的定义 [英] Definition of matplotlib.pyplot.axes.bbox

查看:63
本文介绍了matplotlib.pyplot.axes.bbox 的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白axes.bbox 的定义.例如:

 >>>导入matplotlib.pyplot作为plt>>>f, ax = plt.subplots()>>>ax.bboxTransformedBbox(Bbox('array([[ 0.125, 0.1 ],\n [ 0.9 , 0.9 ]])'), BboxTransformTo(TransformedBbox(Bbox('array([[ 0., 0.],\n [ 8.,6.]])'),Affine2D(array([[80.,0.,0.],[0., 80., 0.],[0.,0.,1.]]))))))))

  1. 这些值是什么意思?我会假设 4 个数字足以定义一个矩形.显然,更多信息存储在这里.

  2. 对于像 ax.figure.canvas.blit(bbox)这样的命令,我需要为bbox定义一个值.如何手动定义特定尺寸的 bbox (假设是轴的右下四分之一)?

解决方案

  1. 您看到的显示值有点复杂Bbox,带有自动应用的嵌套转换.首先,它是一个 TransformedBbox 实例 -引用文档:

    <块引用>

    由给定变换自动变换的 Bbox.

    它在上面显示的控制台中的表示显示了两件事(逗号分隔) - 它所基于的主要 Bbox,以及它所基于的 transform适用.在这种情况下,transform 是一个 BboxTransformTo 对象,其中:

    <块引用>

    BboxTransformTo是一种将点从单位边界框线性转换为给定Bbox的转换.

    在你的情况下,transform 本身是基于一个 TransformedBBox 的,它同样有一个 Bbox 作为它的基础和一个转换 -对于此嵌套实例,请执行 Affine2D 转换.

    我认为,转换的目的是将相对坐标转换为屏幕单位.

    在您的示例中,您可能会发现您希望看到的点由

    给出

     >>>ax.bbox.get_points()数组([[ 80., 48.],[576.,432.]])

    所有代码均在github上可用的 如果您想让自己确切地说出所显示的内容.

  2. 从文档中,您可以实例化 Bbox对象加上您想象的四个数字,例如

    from matplotlib.transforms import Bboxmy_blit_box = Bbox(np.array([[x0,y0],[x1,y1]])

    您也可以使用 静态方法之一,例如

    my_blit_box = Bbox.from_bounds(x0, y0, width, height)

警告

我没有你的用例,所以不能说滚动你自己的 Bbox 并将它传递给 blit() 是否会直接适用于你的案例.

然而,做你想做的事情可能是一种非常复杂的方式.

假设要为绘图设置动画-通常可以将 blit = True 作为动画函数的参数传入,它们会自行进行排序.文档在这里.这里有一些示例,包括带有子图的示例.作为一个骨架 - 你可能会做类似的事情

  fig = plt.figure()ax1 = fig.add_subplot(1、2、1)ax2 = fig.add_subplot(2, 2, 2)ax3 = fig.add_subplot(2, 2, 4)# 将数据实际放在 3 个轴上的代码animation.TimedAnimation.__init__(self, fig, interval=50, blit=True)

如果你想从多个子图中刷新一个子图 - 将 ax.bbox 直接传入 blit 函数应该可以工作.

请注意,给出的大多数示例并没有定义自己的Bbox,而是传入了从 axes figure canvas blit.另请注意,不向 ax.figure.canvas.blit() 传递任何内容将重绘整个画布(默认选项 - 尽管我不明白您为什么要这样做).

I don't understand the definition of axes.bbox. For example:

>>> import matplotlib.pyplot as plt
>>> f, ax = plt.subplots()
>>> ax.bbox
TransformedBbox(Bbox('array([[ 0.125,  0.1  ],\n       [ 0.9  ,  0.9  ]])'), BboxTransformTo(TransformedBbox(Bbox('array([[ 0.,  0.],\n       [ 8.,  6.]])'), Affine2D(array([[ 80.,   0.,   0.],
       [  0.,  80.,   0.],
       [  0.,   0.,   1.]])))))

  1. What do these values mean? I would have assumed that 4 numbers would be sufficient to define a rectangle. Obviously more information is stored here.

  2. For the commands like ax.figure.canvas.blit(bbox) I need to define a value for the bbox. How can I manually define a bbox of particular dimensions (let's say for the lower right quarter of the axes)?

解决方案

  1. The value you see displayed is a bit of a complicated Bbox, with nested transforms that it automatically applies. Firstly, it is a TransformedBbox instance - quoting from the docs:

    A Bbox that is automatically transformed by a given transform.

    the representation of it in the console that you show above displays two things (comma separated) - the main Bbox upon which it is based, and the transform that it applies. The transform in this case is a BboxTransformTo object, which:

    BboxTransformTo is a transformation that linearly transforms points from the unit bounding box to a given Bbox.

    In your case, the transform itself is based upon a TransformedBBox which again has a Bbox upon which it is based and a transform - for this nested instance an Affine2D transform.

    The purpose of the transforms (I believe) is to translate from relative co-ordinates to screen units.

    In your example, you might find that the points you expected to see are given by

    >>> ax.bbox.get_points()
    array([[  80.,   48.],
           [ 576.,  432.]])
    

    All the code for this is in available on github if you want to convince yourself exactly what is being displayed.

  2. From the documentation, you can instantiate a Bbox object with the four numbers you imagine, e.g.

    from matplotlib.transforms import Bbox
    
    my_blit_box = Bbox(np.array([[x0,y0],[x1,y1]])
    

    You could also use one of the static methods, e.g.

    my_blit_box = Bbox.from_bounds(x0, y0, width, height)
    

Caveat

I haven't got your use case, so can't say whether rolling your own Bbox and passing it to blit() will work directly for your case.

However, it's likely to be a really complicated way round to do what you want.

Assming that you want to animate a plot - you can usually pass blit=True in as an argument to the animation functions and they will sort this out themselves. The docs are here. There are some examples here, including ones with subplots. As a skeleton - you might do something like

fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 4)

    # Code to actually put data on your 3 axes

animation.TimedAnimation.__init__(self, fig, interval=50, blit=True)

If you want to refresh one subplot out of many - passing in the ax.bbox directly into the blit function should work.

Note that most of the examples given don't define their own Bbox, but rather pass in a Bbox derived from an axes, figure or canvas into blit. Note also that passing nothing into ax.figure.canvas.blit() will redraw the whole canvas (the default option - although I can't see why you'd want to do that).

这篇关于matplotlib.pyplot.axes.bbox 的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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