Matplotlib Legend Height in pixels?

This is because you haven't yet drawn the canvas.

This is because you haven't yet drawn the canvas. Pixel values simply don't exist in matplotlib (or rather, they exist, have no relation to the screen or other output) until the canvas is drawn. There are a number of reasons for this, but I'll skip them at the moment.

Suffice it to say that matplotlib tries to stay as general as possible, and generally avoids working with pixel values until things are drawn. As a simple example: import matplotlib. Pyplot as plt fig = plt.figure() ax = fig.

Add_subplot(111) ax. Plot(range(10), label='Test') legend = ax. Legend(loc='upper left') print 'Height of legend before canvas is drawn:' print legend.

Get_window_extent(). Height fig.canvas.draw() print 'Height of legend after canvas is drawn:' print legend. Get_window_extent().

Height However, this is only going to represent the height of the legend in pixels as it is drawn on the screen! If you save the figure, it will be saved with a different dpi (100, by default) than it is drawn on the screen, so the size of things in pixels will be different. There are two ways around this: Quick and dirty: draw the figure's canvas before outputting pixel values and be sure to explicitly specify the dpi of the figure when saving (e.g.Fig.

Savefig('temp. Png', dpi=fig. Dpi).

Recommended, but slightly more complicated: Connect a callback to the draw event and only work with pixel values when the figure is drawn. This allows you to work with pixel values while only drawing the figure once. As a quick example of the latter method: import matplotlib.

Pyplot as plt def on_draw(event): fig = event.canvas. Figure ax = fig. Axes0 # I'm assuming only one subplot here!

Legend = ax. Legend_ print legend. Get_window_extent().

Height fig = plt.figure() ax = fig. Add_subplot(111) ax. Plot(range(10), label='Test') legend = ax.

Legend(loc='upper left') fig.canvas. Mpl_connect('draw_event', on_draw) fig. Savefig('temp.

Png') Notice the different in what is printed as the height of the legend for the first and second examples. (31.0 for the second vs. 24.8 for the first, on my system, but this will depend on the defaults in your . Matplotlibrc file) The difference is due to the different dpi between the default fig.

Dpi (80 dpi, by default) and the default resolution when saving a figure (100 dpi, by default). Hopefully that makes some sense, anyway.

Do you know why 1*dpi will not result in the pixel height post render. I.e. Why is it always 1 before drawing?

– Duncan Feb 17 at 8:52 @Duncan - Basically, matplotlib deliberately avoids any idea of pixel values until you get down to the renderer for a given canvas. (Pixels don't make sense for many of the backends matplotlib supports. If you're using a vector backend, real "pixel" values don't exist.) The renderer doesn't exist until the figure's canvas is drawn.

Basically, you're seeing the default values (0-1) when there is no renderer. (There's a bit more to it, but that's the gist of it.) – Joe Kington Feb 17 at 15:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions