Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a small image on the corner of a plot with matplotlib?

What I want is really simple: I have a small image file called "logo.png" that I want to display on the upper left corner of my plots. But you can't find any example of that in the matplotlib examples gallery.

I'm using django, and my code is something like this:

def get_bars(request)     ...     fig = Figure(facecolor='#F0F0F0',figsize=(4.6,4))     ...     ax1 = fig.add_subplot(111,ylabel="Valeur",xlabel="Code",autoscale_on=True)     ax1.bar(ind,values,width=width, color='#FFCC00',edgecolor='#B33600',linewidth=1)     ...     canvas = FigureCanvas(fig)     response = HttpResponse(content_type='image/png')     canvas.print_png(response)     return response 
like image 611
pleasedontbelong Avatar asked Aug 31 '10 13:08

pleasedontbelong


People also ask

How do I shade an area in matplotlib?

You can use ax. axvspan , which apparently does exactely what you want. For better results, usa an alpha value below 0.5, and optionally set color and edge-color/width. If you want the shading to be in a different orientation (horizontal instead of vertical), there is also the ax.

How do I resize an image in Python using matplotlib?

Use the opencv module to resize images in Python To resize an image, we will first read the image using the imread() function and resize it using the resize() function as shown below. The imread() returns an array that stores the image. We resize it with the resize() function.


1 Answers

If you want the image at the corner of your actual figure (rather than the corner of your axis), look into figimage.

Perhaps something like this? (using PIL to read the image):

import matplotlib.pyplot as plt import Image import numpy as np  im = Image.open('/home/jofer/logo.png') height = im.size[1]  # We need a float array between 0-1, rather than # a uint8 array between 0-255 im = np.array(im).astype(np.float) / 255  fig = plt.figure()  plt.plot(np.arange(10), 4 * np.arange(10))  # With newer (1.0) versions of matplotlib, you can  # use the "zorder" kwarg to make the image overlay # the plot, rather than hide behind it... (e.g. zorder=10) fig.figimage(im, 0, fig.bbox.ymax - height)  # (Saving with the same dpi as the screen default to #  avoid displacing the logo image) fig.savefig('/home/jofer/temp.png', dpi=80)  plt.show() 

alt text

Another option, if you'd like to have the image be a fixed fraction of the figure's width/height is to create a "dummy" axes and place the image in it with imshow. This way the image's size and position is independent of DPI and the figure's absolute size:

import matplotlib.pyplot as plt from matplotlib.cbook import get_sample_data  im = plt.imread(get_sample_data('grace_hopper.jpg'))  fig, ax = plt.subplots() ax.plot(range(10))  # Place the image in the upper-right corner of the figure #-------------------------------------------------------- # We're specifying the position and size in _figure_ coordinates, so the image # will shrink/grow as the figure is resized. Remove "zorder=-1" to place the # image in front of the axes. newax = fig.add_axes([0.8, 0.8, 0.2, 0.2], anchor='NE', zorder=-1) newax.imshow(im) newax.axis('off')  plt.show() 

enter image description here

like image 60
Joe Kington Avatar answered Sep 19 '22 15:09

Joe Kington