Set transparency (alpha) of matplotlib 3d grid

Viewed 1555

I would like to change the transparency of the grid in matplotlib 3d plot.

But I find that it is not as easy as in 2d, which is simply plt.grid(alpha=0.2).

Here I give a mini code

import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(3, 100)

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

ax.scatter(data[0], data[1], data[2])

# How to change the grid transparency?

plt.show()

enter image description here

How to set the transparency of the x,y,z-grids?


I have tried:

  1. Using ax.zaxis._axinfo['grid'].update({"alpha": 0.1}). But it appears that it does not have the key alpha.

  2. I checked the source code of ax.grid() here in github. From the comments, it seems that the alpha functionality is not implemented for 3d case at all.

1 Answers

plt.grid does not seem to do anything for 3d plots. But you can set the color as a RGB+Alpha tuple using rcparams:

import numpy as np
import matplotlib.pyplot as plt

# fourth parameter is alpha=0.1
plt.rcParams['grid.color'] = (0.5, 0.5, 0.5, 0.1)

data = np.random.randn(3, 100)

fig = plt.figure()
ax = plt.axes(projection ="3d")

ax.scatter(data[0], data[1], data[2])

plt.show()

Result:

enter image description here

Related