matplotlib - How to save an array as image with overlayed text? - python

I have a 2D array that I need to save as a png. I also need to add a text label to the image. So far, I have tried two approaches, none of which is optimal:
I use the matplotlib.image module to save the array directly as an image:
matplotlib.image.imsave(FILENAME, ARRAY, cmap=plt.cm.binary)
However I am unable to add text using that command. I could use PIL to read and edit after saving the raw images, but the I/O cost on a large data set would be unacceptable.
I use the pyplot interface to convert the array to a figure and then add a legend. However when I save it as a file, there is unnecessary whitespace. I have tried turning axes off, setting padding to 0 etc., but there is always some whitespace margin I cannot get rid of:
import matplotlib.pyplot as plt
plt.imshow(ARRAY, cmap=plt.cm.binary)
plt.axis('off')
plt.savefig(FILENAME, dpi=100, pad_inches=0.0, bbox_inches='tight')
Is there a way to generate an image from a 2D array, overlay text, and save as .png speedily with no whitespace? Preferably a solution using matplotlib/PIL, but if there's anything better out there, I can look into it.

I was able to solve my problem by using an object oriented approach from the start:
import matplotlib.pyplot as plt
fig = plt.figure(dpi=100, tight_layout=True, frameon=False, figsize=(resolution/100.,resolution/100.)) # dpi & figsize of my choosing
fig.figimage(ARRAY, cmap=plt.cm.binary)
fig.text(X,Y,TEXT, size='medium', backgroundcolor='white', alpha=0.5)
plt.savefig(FILENAME)
plt.close(fig)
Additional documentation for the figure class can be found here.
Note: For sizing figures, I found this relationship useful:
size in inches = resolution in pixels / DPI

Related

Convert matplotlib plot to numpy list to show using opencv2

so I am making 4 types of plots in matplotlib through functions. Those include Pie Charts, Line Charts, Scatter Plot and area graphs. I want to get a numpy array of it, so I can display it using opencv2 or something else on django. I have tried this so far:
import matplotlib.pyplot as plt
import numpy as np
# Make a random plot...
fig = plt.figure()
fig.add_subplot(111)
# If we haven't already shown or saved the plot, then we need to
# draw the figure first...
fig.canvas.draw()
# Now we can save it to a numpy array.
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
But the problem is I cannot use a plot here, I have to use a figure which I don't want to use. I tried doing plt.plot([1,2,3,4,5]) for line graph as a test, but it turns out it returns a list, while I need a figure to use the tostring_rgb() What should be the alternative to this?
EDIT:
Suggested in comments for another question, I am not wanting to make a figure, I want to make a normal Plot with plt.plot() of line graph, and also plt.pie()

Showing a picture in python

I'm trying to show a figure, in PyCharm, that is in my working directory, but it either doesn't work or only works with this code:
img = mpimg.imread('Figure_1.png')
plt.imshow(img)
plt.show()
with this result:
I just want the picture as it is, not inside another figure. This is the original picture for reference:
Some quick workarounds: to remove the axes, do plt.axes('off'). To make the picture fit the frame, set the aspect ratio to 'auto' and create a figure with the same aspect ratio as your original image (i'd say it's roughly 4:1?). Use tight_layoutto make sure all of your image is visible. I don't know if that's the official way, but that's how i do it, and it kinda works ;-)
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('Figure_1.png')
f, a = plt.subplots(figsize=(16, 4))
plt.tight_layout()
plt.axis('off')
a.imshow(img)
plt.show()

How to read saved image and locate it in coordinates without any distortins?

I can't overcome maybe very simple obstacle. First, I am doing some spatial operations with shape files, plot the results and save the image:
# read different shape-files, overlaying them, sjoining them`
...
# plotting results:
fig, ax = plt.subplots(figsize=[10, 10])
ax.set_xlim(left=9686238.14, right=9727068.02)
ax.set_ylim(bottom=7070076.66, top=7152463.12)
# various potting like objects.plot(ax=ax, column = 'NAME', cmap='Pastel2', k=6, legend=False) and many others
plt.axis('equal')
plt.savefig('back11.png', dpi=300)
plt.close()
Thus I got such a nice picture back11.png:
Second, I am reading that picture and (in the same cordinates) want to see absolutlely identical one map11.png:
fig, ax = plt.subplots(figsize=[10, 10])
ax.set_xlim(left=9686238.14, right=9727068.02)
ax.set_ylim(bottom=7070076.66, top=7152463.12)
back = plt.imread('back11.png')
ax.imshow(back, extent=[9686238.14, 9727068.02, 7070076.66, 7152463.12])
plt.axis('equal')
plt.savefig('map11.png', dpi=300)
plt.close()
But really I got something else (map11.png):
What is the origin of such a strange mismatch?
When matplotlib is showing an image using plt.imshow, it automatically adds axis and white space around it (regardless of the image content). While your image is accidentally another plot, which contains axis and white space itself. To solve that problem, use
plt.subplots_adjust(0, 0, 1, 1)
plt.axis('off')
which should output nothing but the image.
But on the other hand, you have to specify plt.figure(figsize=xxx, dpi=xxx) correctly in order to get THE stored image (correct size, no interpolation or re-sampling). If you simply want to see the image using python (and you are in jupyter notebook), you can use Pillow. If you convert the image to a PIL.Image object, it is by itself displayable by jupyter REPL.
If you are not inside jupyter, you might also directly open the image using os image viewer. It is at least more convenient than matplotlib to display the "exact" image.
BTW, when displaying the image, the same parameters do not apply any more (since its an image and all parameters are hidden inside the content of it). Therefore, there's no need (and it's wrong) to write all those magic numbers. Also if you want to save the image without white border and axis, use the code above before calling plt.savefig

Importing an svg file into a matplotlib figure

I like to produce high quality plots and therefore avoid rasterized graphics as much as possible.
I am trying to import an svg file on to a matplotlib figure:
import matplotlib.pyplot as plt
earth = plt.imread('./gfx/earth.svg')
fig, ax = plt.subplots()
im = ax.imshow(earth)
plt.show()
This works with png perfectly. Can somebody tell me how to do it with svg or at least point my to proper documentation.
I know that a similar question has been asked (but not answered): here. Has anything changed since?
P.S. I know that I could just export a high resolution png and achieve a similar effect. This is not the solution I am looking for.
Here is the image I would like to import:
.
Maybe what you are looking for is svgutils
import svgutils.compose as sc
from IPython.display import SVG # /!\ note the 'SVG' function also in svgutils.compose
import numpy as np
# drawing a random figure on top of your SVG
fig, ax = plt.subplots(1, figsize=(4,4))
ax.plot(np.sin(np.linspace(0,2.*np.pi)), np.cos(np.linspace(0,2.*np.pi)), 'k--', lw=2.)
ax.plot(np.random.randn(20)*.3, np.random.randn(20)*.3, 'ro', label='random sampling')
ax.legend()
ax2 = plt.axes([.2, .2, .2, .2])
ax2.bar([0,1], [70,30])
plt.xticks([0.5,1.5], ['water ', ' ground'])
plt.yticks([0,50])
plt.title('ratio (%)')
fig.savefig('cover.svg', transparent=True)
# here starts the assembling using svgutils
sc.Figure("8cm", "8cm",
sc.Panel(sc.SVG("./Worldmap_northern.svg").scale(0.405).move(36,29)),
sc.Panel(sc.SVG("cover.svg"))
).save("compose.svg")
SVG('compose.svg')
Output:
to anyone ending up here in 2021...
I'd suggest having a look at the cairosvg package
(conda install -c conda-forge cairosvg or pip3 install cairosvg)
https://cairosvg.org/
import cairosvg
import matplotlib.pyplot as plt
from PIL import Image
from io import BytesIO
img_png = cairosvg.svg2png("... the content of the svg file ...")
img = Image.open(BytesIO(img_png))
plt.imshow(img)
SVG (Scalable Vector Graphics) is a vectorial format, which means the image is not composed of pixels, but instead of relative paths that can be scaled arbitrarily.
NumPy/Matplotlib, as numerics software, only really works with pixel graphics and cannot handle svg. I would suggest first converting the svg file to e.g. a png file by opening and saving it in software such as Inkscape (which is free). Then, open the exported png in Python.
Alternatively, use the wikimedia provided versions of the file in png format on the picture's information page (click on the download button to the right of the picture).
If you really believe you need the vectorial form, well, there is no way to do that. You can always superpose the matplotlib figure on to the figure manually (using the matplotlib Artist to draw on the plot canvas), or through some pycairo magic, and save that. But Matplotlib cannot work directly with svg content.

Reduce the size of .eps figure made using matplotlib

Today I was doing a report for a course and I needed to include a figure of a contour plot of some field. I did this with matplotlib (ignore the chaotic header):
import numpy as np
import matplotlib
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
## for Palatino and other serif fonts use:
#rc('font',**{'family':'serif','serif':['Palatino']})
rc('text', usetex=True)
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
import numpy.ma as ma
from numpy.random import uniform
from matplotlib.colors import LogNorm
fig = plt.figure()
data = np.genfromtxt('Isocurvas.txt')
matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'
rc('text', usetex=True)
rc('font', family='serif')
x = data[:,0]
y = data[:,1]
z = data[:,2]
# define grid.
xi = np.linspace(0.02,1, 100)
yi = np.linspace(0.02,1.3, 100)
# grid the data.
zi = griddata(x,y,z,xi,yi)
# contour the gridded data.
CS = plt.contour(xi,yi,zi,25,linewidths=0,colors='k')
CS = plt.contourf(xi,yi,zi,25,cmap=plt.cm.jet)
plt.colorbar() # draw colorbar
# plot data points.
plt.scatter(x,y,marker='o',c='b',s=0)
plt.xlim(0.01,1)
plt.ylim(0.01,1.3)
plt.ylabel(r'$t$')
plt.xlabel(r'$x$')
plt.title(r' Contour de $\rho(x,t)$')
plt.savefig("Isocurvas.eps", format="eps")
plt.show()
where "Isocurvas.txt" is a 3 column file, which I really don't want to touch (eliminate data, or something like that, wouldn't work for me). My problem was that the figure size was 1.8 Mb, which is too much for me. The figure itself was bigger than the whole rest of the report, and when I opened the pdf it wasn't very smooth .
So , my question is :
Are there any ways of reducing this size without a sacrifice on the quality of the figure?. I'm looking for any solution, not necessarily python related.
This is the .png figure, with a slight variation on parameters. using .png you can see the pixels, which i don't like very much, so it is preferable pdf or eps.
Thank you.
The scatter plot is what's causing your large size. Using the EPS backend, I used your data to create the figures. Here's the filesizes that I got:
Straight from your example: 1.5Mb
Without the scatter plot: 249Kb
With a raster scatter plot: 249Kb
In your particular example it's unclear why you want the scatter (not visible). But for future problems, you can use the rasterized=True keyword on the call to plt.scatter to activate a raster mode. In your example you have 12625 points in the scatter plot, and in vector mode that's going to take a bit of space.
Another trick that I use to trim down vector images from matplotlib is the following:
Save figure as EPS
Run epstopdf (available with a TeX distribution) on the resulting file
This will generally give you a smaller pdf than matplotlib's default, and the quality is unchanged. For your example, using the EPS file without the scatter, it produced a pdf with 73 Kb, which seems quite reasonable. If you really want a vector scatter command, running epstopdf on the original 1.5 Mb EPS file produced a pdf with 198 Kb in my system.
I'm not sure if it helps with size, but if your willing to try the matplotlib 1.2 release candidate there is a new backend for producing PGF images (designed to slot straight into latex seamlessly). You can find the docs for that here: http://matplotlib.org/1.2.0/users/whats_new.html#pgf-tikz-backend
If you do decide to give it a shot and you have any questions, I'm probably not the best person to talk to, so would recommend emailing the matplotlib-users mailing list.
HTH,
Try removing the scatter plot of your data. They do not appear to be visible in your final figure (because you made them size 0) and may be taking up space in your eps.
EDITED: to completely change the answer because I read the question wrong.

Categories