Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make axes transparent in matplotlib?

So I have the following example that plots a figure and an inset:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()

ax1 = fig.add_subplot(1, 1, 1)
x = np.arange(100).reshape((10, 10))
ax1.imshow(x)

ax2 = fig.add_axes([0.5, 0.5, 0.3, 0.3])
t = np.arange(0, 1, 0.01)
s = np.sin(t)
ax2.plot(t, s, linewidth=2)

This produces the following figure: enter image description here

What I would like to have thought is the inset to be a little bit transparent (say alpha=0.5). I want something along the lines of what they do in the documentation for the legends in the matplotlib documentation:

enter image description here

http://matplotlib.org/users/recipes.html#transparent-fancy-legends

Is that possible? does anyone know how to do that?

All the best,

P.D. As mentioned in the comments the answer to this questions can be derived from the answer to the linked question. It is just a little bit different conceptually (figure vs axis) and far more straightforward here IMO.

like image 620
Heberto Mayorquin Avatar asked Oct 23 '15 08:10

Heberto Mayorquin


People also ask

How do I make a plot transparent in Matplotlib?

Matplotlib allows you to regulate the transparency of a graph plot using the alpha attribute. By default, alpha=1. If you would like to form the graph plot more transparent, then you'll make alpha but 1, such as 0.5 or 0.25.

How do you change the transparency of a line in Python?

Matplotlib allows you to adjust the transparency of a graph plot using the alpha attribute. If you want to make the graph plot more transparent, then you can make alpha less than 1, such as 0.5 or 0.25. If you want to make the graph plot less transparent, then you can make alpha greater than 1.

Which parameter should you use to change a plot's transparency?

In order to change the transparency of a graph plot in matplotlib we will use the matplotlib. pyplot. plot() function. The plot() function in pyplot module of matplotlib library is used to make 2D illustrations.


1 Answers

Ok, so just after playing a little bit I discovered that this can be achieved by controlling the patch property or the axes.

Adding the following the above code produces the desired result:

ax2.patch.set_alpha(0.5)

enter image description here

This works because patch is just the figure (the Artist in matplotlib parlance) that represents the background as I understand from the documentation:

Documentation

like image 65
Heberto Mayorquin Avatar answered Sep 22 '22 04:09

Heberto Mayorquin