Matplotlib Resize Image

20221
3
06-26-2017 11:01 AM
LloydBronn
Occasional Contributor II

I have a Python geoprocessing tool that creates a chart from a click on our website. I'm trying to put our company's logo in the chart, but it's a little too big and it's blocking some of the X axis labels. I've tried physically resizing the .png in Windows and linking to the smaller version. No matter which one I use, it's displayed the same size in the chart. I haven't been able to find a clear solution online. Here is my code:

fig, ax1 = plt.subplots(figsize=(12,8),facecolor='w')

logo = plt.imread(get_sample_data("our logo"))
axim = fig.add_axes([0.01,0.01,0.2,0.14], anchor='SW')
axim.imshow(logo, aspect='auto')
axim.axis('off')
0 Kudos
3 Replies
ClintonDow1
Occasional Contributor II

In the matplotlib docs there are some examples using PIL (Python Image Library) to resize the image before plotting:

In [16]: from PIL import Image
In [17]: img = Image.open('../_static/stinkbug.png')
In [18]: img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
In [19]: imgplot = plt.imshow(img)

Perhaps give that a try? 

LloydBronn
Occasional Contributor II

I tried this and nothing showed up. I've always had problems with PIL.

0 Kudos
ClintonDow1
Occasional Contributor II

Yeah its not something I've used often, just saw the snippet in the docs and figured it was a different approach worth trying.

 

I also just found this tutorial which looks better than the PIL approach, uses OffsetImage and AnnotationBox: 

pylab_examples example code: demo_annotation_box.py — Matplotlib 2.0.2 documentation 

0 Kudos