Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Definition of matplotlib.pyplot.axes.bbox

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

like image 481
BerndGit Avatar asked Apr 22 '15 21:04

BerndGit


People also ask

What is BBOX in matplotlib?

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.

What does axes mean in matplotlib?

Axes object is the region of the image with the data space. A given figure can contain many Axes, but a given Axes object can only be in one Figure. The Axes contains two (or three in the case of 3D) Axis objects. The Axes class and its member functions are the primary entry point to working with the OO interface.

What is axes and axis in matplotlib?

Axis is the axis of the plot, the thing that gets ticks and tick labels. The axes is the area your plot appears in.

How do you define an axis in Python?

To define x-axis and y-axis data coordinates, use arange() and sin() functions. To plot a line graph, use the plot() function. To set range of x-axis and y-axis, use xlim() and ylim() function respectively.


1 Answers

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

like image 173
J Richard Snape Avatar answered Oct 02 '22 17:10

J Richard Snape