Export matplotlib basemap to SVG - python

How can I save a basemap created by the code below in Python as a SVG image (a *.pdf file would also do the job as it can easily be converted)?
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
# lon_0 is central longitude of projection.
# resolution = 'c' means use crude resolution coastlines.
m = Basemap(projection='robin',lon_0=0,resolution='c')
m.drawcoastlines()
m.fillcontinents(color='coral',lake_color='aqua')
# draw parallels and meridians.
m.drawparallels(np.arange(-90.,120.,30.))
m.drawmeridians(np.arange(0.,360.,60.))
m.drawmapboundary(fill_color='aqua')
plt.title("Robinson Projection")
plt.show()
Source: https://matplotlib.org/basemap/users/robin.html
Applying plt.savefig("filename.pdf") only yields a white/emtpy file.

Is there a reason that plt.savefig(filename) wouldn't work?
By specifying the file format in the filename, you can make it any format you want. For example, if you want a .pdf you would type plt.savefig("filename.pdf").

Related

plotting streamplot is so slow in python3

I am using Anaconda, and I think this problem is even before I use Anaconda.
Everytime I want to plot streamline with basemap, it takes a very long time to plot one figure.
Here is an example:
import netCDF4 as nc
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
fuv = nc.Dataset('~/era5_uv850_20200601_00_01.nc')
uwnd = fuv.variables['u']
vwnd = fuv.variables['v']
uwnd = uwnd[0,:,:]
vwnd = vwnd[0,:,:]
lon= fuv.variables['longitude']
lat= fuv.variables['latitude']
map = Basemap(projection='cyl',llcrnrlat= 0.,urcrnrlat=20.,llcrnrlon=90.5,urcrnrlon=120.5,resolution='i')
x,y=map(*np.meshgrid(lon,lat))
plt.clf()
map.streamplot(x,y,uwnd,vwnd,30)
map.drawcoastlines()
map.drawcountries()
plt.show()
The data from ERA5 webpage: Copernicus Data Storage
or you can download it here: WeTransfer Link
Is the slowness a common issue?

Plot a FITS image over a grid of the entire sky

I would like to plot a FITS image over a sketch of the entire sky.
I got this far in plotting the entire sky:
import matplotlib.pyplot as plt
from astropy.utils.data import get_pkg_data_filename
from astropy.io import fits
image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')
image_data = fits.getdata(image_file, ext=0)
plt.subplot(111, projection='aitoff')
plt.grid(True)
plt.imshow(image_data, cmap='gray')
plt.show()
But I can't seem to get the FITS image properly aligned with the grid
The above code results in the following
But I'm trying to get something more like
where the blue square is the actual position of the image in image_file
The image_data is just an array of pixel values, and doesn't actually contain any information about the positions and orientations of the pixels on a sky map. You have to extract that information from the FITS file too.
An example would be (based in information from here and here):
import matplotlib.pyplot as plt
from astropy.utils.data import get_pkg_data_filename
from astropy.io import fits
from astropy.wcs import WCS
from astropy.visualization.wcsaxes.frame import EllipticalFrame
image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')
image_header = fits.open(image_file)[0].header # extract header info
image_data = fits.getdata(image_file, ext=0)
# use the WCS class to get coordinate info and projection axes to use
wcs = WCS(image_header)
# make the axes using the EllipticalFrame class
ax = plt.subplot(projection=wcs, frame_class=EllipticalFrame)
# add the image
im = ax.imshow(image_data, origin='lower')
# add a grid
overlay = ax.get_coords_overlay('fk5')
overlay.grid(color='white', ls='dotted')
As the particular image example doesn't extend across the whole sky you'll only see a small patch (but you can extend the plot using ax.set_xlim() and ax.set_ylim() as required, although I'm not sure what the units are, and it's also worth noting that this image actually just covers a very small patch of sky), but if your image was over the whole sky it would look like an Aitoff projection as shown here.
I think this only works with the latest version of astropy (v3.1), Matplotlib (v3.0.2), and therefore requires Python >= 3.5.
You may also want to look at healpy, although that can't read in the astropy example FITS file.

How to correctly combine PNG and FITS data into one Matplotlib figure in Python?

I have a FITS file of 4545x4545 pixels with a header containing its coordinate system. Since DS9 (another software to view and handle FITS images) handles the color map scaling better, I had the idea of:
open the FITS file using DS9 to tweak the color map of the image,
save this image in a PNG file,
load this PNG file in matplotlib and add the header from the original FITS file, so I can add the coordinate system to the PNG file.
But the coordinates are not showing correctly, because the pixelization changes to different values in every step. How can I do this correctly?
This the relevant part of my code:
from astropy.io import fits
import matplotlib.pyplot as plt
import aplpy
from wcsaxes import WCSAxes
from astropy import wcs
import sys
import matplotlib.image as mpimg
image_fits = 'image_in.fits'
image_png = 'image_in.png' # this came from the one before, has different pixelization
image_data_png = mpimg.imread(image_png)
image_head_fits = fits.getheader(image_fits)
hdu_list = fits.open(image_fits)
F = aplpy.FITSFigure(hdu_list, figure=plt.figure(1))
fig = plt.figure()
mywcs = wcs.WCS(image_head_fits)
ax = WCSAxes(fig,[0.1, 0.1, 0.8, 0.8],wcs=mywcs)
fig.add_axes(ax)
ax.imshow(image_data_png)
plt.savefig('image_out.png')

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.

Matplotlib-Imported PNG won't show in 3D plot

I'm trying to use matplotlib to generate 3D figures where the xy plane is an image, and then some 3D tracks are drawn on top (that part works just fine). The problem is, even though my imported PNG shows just fine with imshow, and even though I can plot an image on a 3D axis if I just use an example from the cookbook, my image just shows up as a featureless black box. I'm sure I'm missing something small- thanks in advance!
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D, art3d
from pylab import ogrid
import matplotlib.pyplot as plt
plt.ioff()
fig = plt.figure()
ay=fig.add_subplot(2,1,1)
rawim=plt.imread(r'G:\Path\myimage.png')
ay.imshow(rawim,cmap='gray')
ax=fig.add_subplot(2,1,2,projection='3d')
x,y= ogrid[0:rawim.shape[0],0:rawim.shape[1]]
ax.plot_surface(x,y,0,rstride=5,cstride=5,facecolors=rawim,cmap='gray')
ax.view_init(elev=45, azim=12)
plt.show()
The output comes out as this (edited to include image).
PS Running Matplotlib 1.2.1 in Spyder for Python 2.75
Edited to add- I was largely modeling my approach from this post, so if instead of
rawim=plt.imread(r'G:\Path\myimage.png')
I use
from matplotlib.cbook import get_sample_data
fn = get_sample_data("lena.png", asfileobj=False)
rawim=read_png(fn)
it works perfectly. I've tried several of my PNG outputs, produced a couple of different ways, and no love. And yes, they're greyscale between 0-1.
You should use an explicit color array for facecolors.
You want something having shape (Nx, Ny, 4) where the "4" dimension holds RGBA values.
Also, get rid of the cmap='gray' in the plot_surface invocation.

Categories