# How to create and save a matplotlib figure without frame and labels ?

Published: May 15, 2023

Updated: May 16, 2023

Tags: Python; Matplotlib;

This article will show you how to create an image using matplotlib without any outlines or labels.

## Removing frame and labels of a matplotlib figure

Let's look at this example of using matplotlib

````import numpy as np`
`import matplotlib.cm as cm`
`import matplotlib.pyplot as plt`

`def bivariate_normal(X, Y, sigmax=1.0, sigmay=1.0, mux=0.0, muy=0.0, sigmaxy=0.0):`
`    Xmu = X-mux`
`    Ymu = Y-muy`
`    rho = sigmaxy/(sigmax*sigmay)`
`    z = Xmu**2/sigmax**2 + Ymu**2/sigmay**2 - 2*rho*Xmu*Ymu/(sigmax*sigmay)`
`    denom = 2*np.pi*sigmax*sigmay*np.sqrt(1-rho**2)`
`    return np.exp(-z/(2*(1-rho**2))) / denom`

`delta = 0.025`
`x = y = np.arange(-3.0, 3.0, delta)`
`X, Y = np.meshgrid(x, y)`
`Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)`
`Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)`
`Z = Z2-Z1  # difference of Gaussians`

`my_dpi=100`

`Shape = Z.shape`

`fig = plt.figure(figsize=(Shape[1]/my_dpi, Shape[0]/my_dpi), dpi=my_dpi)`

`im = plt.imshow(Z, interpolation='bilinear', cmap=cm.RdYlGn,`
`                origin='lower', extent=[-3,3,-3,3],`
`                vmax=abs(Z).max(), vmin=-abs(Z).max())`

`fig.savefig('MatplotlibImageNoFrame01.png', dpi=my_dpi)`
`plt.show()`
```

You can then remove the frame or outline of an image by adding the set_axis_off() command:

````ax = plt.Axes(fig, [0., 0., 1., 1.])`
`ax.set_axis_off()`
`fig.add_axes(ax)`
```

Example

````import numpy as np`
`import matplotlib.cm as cm`
`import matplotlib.pyplot as plt`

`def bivariate_normal(X, Y, sigmax=1.0, sigmay=1.0, mux=0.0, muy=0.0, sigmaxy=0.0):`
`    Xmu = X-mux`
`    Ymu = Y-muy`
`    rho = sigmaxy/(sigmax*sigmay)`
`    z = Xmu**2/sigmax**2 + Ymu**2/sigmay**2 - 2*rho*Xmu*Ymu/(sigmax*sigmay)`
`    denom = 2*np.pi*sigmax*sigmay*np.sqrt(1-rho**2)`
`    return np.exp(-z/(2*(1-rho**2))) / denom`

`delta = 0.025`
`x = y = np.arange(-3.0, 3.0, delta)`
`X, Y = np.meshgrid(x, y)`
`Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)`
`Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)`
`Z = Z2-Z1  # difference of Gaussians`

`my_dpi=100`

`Shape = Z.shape`

`fig = plt.figure(figsize=(Shape[1]/my_dpi, Shape[0]/my_dpi), dpi=my_dpi)`

`ax = plt.Axes(fig, [0., 0., 1., 1.])`
`ax.set_axis_off()`
`fig.add_axes(ax)`

`im = plt.imshow(Z, interpolation='bilinear', cmap=cm.RdYlGn,`
`                origin='lower', extent=[-3,3,-3,3],`
`                vmax=abs(Z).max(), vmin=-abs(Z).max())`

`fig.savefig('MatplotlibImageNoFrame02.png', dpi=my_dpi)`

`plt.savefig("test.png", bbox_inches='tight', dpi=200, facecolor='red', pad_inches = 0)`

`plt.show()`
```

## Saving matplotlib figure

Once you have created your matplotlib figure, you can save it as an image file for use in other applications. To have the figure fit perfectly, include the parameters "bbox_inches='tight'" and "pad_inches=0" in save():

````plt.savefig("MatplotlibImageNoFrame02.png", bbox_inches='tight', dpi=200, facecolor='red', pad_inches = 0)`
```

## Example of use

Create an interactive map using Folium and add a Cartopy-created image as an overlay to the map. Step1: create a mercator map with cartopy:

````import cartopy.crs as ccrs`
`import matplotlib.cm as cm`
`import matplotlib.pyplot as plt`
`import matplotlib as mpl`

`plt.figure(figsize=(16,9))`

`ax = plt.axes(projection=ccrs.Mercator(min_latitude=-85.0, max_latitude=85.0, globe=None, latitude_true_scale=1))`

`ax.set_axis_off()`

`ax.coastlines(resolution='110m')`

`plt.savefig("cartopy_mercator.png", bbox_inches='tight', dpi=200, pad_inches = 0)`

`plt.show()`
```

Step2: overlay image

````import os`
`import folium`

`from folium.raster_layers import ImageOverlay`

`m = folium.Map([0,0], zoom_start=1, crs='EPSG3857')`
`merc = "cartopy_mercator.png"`

`folium.TileLayer(`
`        tiles = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',`
`        attr = 'Esri',`
`        name = 'Esri Satellite',`
`        overlay = False,`
`        control = True`
`       ).add_to(m)`

`if not os.path.isfile(merc):`
`    print(f"Could not find {merc}")`
`else:`
`    img = folium.raster_layers.ImageOverlay(`
`        name="Mercator projection SW",`
`        image=merc,`
`        bounds=[[[-85,-180]],[85,180]],`
`        opacity=0.5,`
`        interactive=True,`
`        cross_origin=False,`
`        zindex=1,`
`    )`

`    folium.Popup("I am an image").add_to(img)`

`    img.add_to(m)`
`    folium.LayerControl().add_to(m)`

`m`
```