19

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.

Heberto Mayorquin
  • 10,605
  • 8
  • 42
  • 46
  • Possible duplicate of [How to set opacity of background colour of graph wit Matplotlib](http://stackoverflow.com/questions/4581504/how-to-set-opacity-of-background-colour-of-graph-wit-matplotlib) – hitzg Oct 23 '15 at 13:32
  • It kind of overlaps but the other is more oriented to the **figure** as a whole whereas this is about a particular **axis**. From the answer of the other question, however, you can get the answer to this. Anyway, I solved my problem so if you want to close it follow your rules. – Heberto Mayorquin Oct 23 '15 at 15:28

1 Answers1

33

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

Heberto Mayorquin
  • 10,605
  • 8
  • 42
  • 46