Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do 'normalized figure coordinates' work?

Tags:

matplotlib

In matplotlib, I recently came across the term 'normalized figure coordinates', which is apparently a specification of a rectangle by four parameters.

It is evident that a rectangle can be described by four numbers, and I'm guessing these four numbers somehow describe the dimensions as well as the location of the rectangle. However, I haven't managed to find an answer as to which of these parameters specifies which value.

Additionally, I'm not sure whether this is a matplotlib-specific term or one of general meaning, as the matplotlib documentation does not cite or link any sources with respect to this term.

Can anyone shed some light on this issue, please?

like image 367
Tobias Feil Avatar asked Sep 25 '26 12:09

Tobias Feil


2 Answers

There are several functions where normalized figure coordinates are used.

In general possibilities are

  • (left, bottom, width, height) (this is called "bounds" in matplotlib); or
  • (left, bottom, right, top) (called "extent").

Hopefully the documentation will make it clear which 4 tuple is expected in the respective case.

Here you seem to be interested in the GridSpec's tight_layout parameter rect. From its documentation

rect : tuple of 4 floats, optional
(left, bottom, right, top) rectangle in normalized figure coordinates that the whole subplots area (including labels) will fit into. Default is (0, 0, 1, 1).

like image 131
ImportanceOfBeingErnest Avatar answered Sep 27 '26 03:09

ImportanceOfBeingErnest


To answer your last question, the term normalization is not matplotlib-specific you can get a very short intro from wikipedia.

As for Matplotlib: you can have different coordinate systems relative to different objects (e.g. the axis, the figure). Each of these systems is normalized, in the sense that the 4 corners of the chosen reference object will always have the following coordinates:

(0,1) Top left corner 
(1,1) Top right corner 
(1,0) Bottom right corner 
(0,0) Bottom left corner 

Where the first element of each pair refers to x-axis and the second element refers to the y-axis.

This makes, among other things, annotation or placements of artist objects easier as you can specify the position of the element you wish to add using any of the available coordinate systems. All you need to do is select an appropriate coordinate system by passing a transformation object to the transform parameter.

Some example:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot([5.], [2.], 'o')

circle=plt.Circle((0, 0), 0.1, color="g",transform=ax.transAxes) #bottom (y=0) left (x=0) green circle of radius 0.1 (expressed in coord system)
ax.add_artist(circle)

ax.annotate('I am the top (y=1.0) right (x=1.0) Figure corner',
            xy=(1, 1), xycoords=fig.transFigure,
            xytext=(0.2, 0.2), textcoords='offset points',
            )

plt.text(  # position text relative to data
    5., 2., 'I am the (5,2) data point',  # x, y, text,
    ha='center', va='bottom',   # text alignment
    transform=ax.transData      # coordinate system transformation
)
plt.text(  # position text relative to Axes
    1.0, 0.0, 'I am the bottom (y=0.0) right (x=1.0) axis corner',
    ha='right', va='bottom',
    transform=ax.transAxes
)
plt.text(  # position text relative to Figure
    0.0, 1.0, 'I am the top (y=1.0) left (x=0.0) figure corner',
    ha='left', va='top',
    transform=fig.transFigure
)


plt.show()

example

like image 41
CAPSLOCK Avatar answered Sep 27 '26 03:09

CAPSLOCK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!