Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set transparency (alpha) of matplotlib 3d grid

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.

like image 967
Nathan Explosion Avatar asked May 22 '26 06:05

Nathan Explosion


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

like image 94
jf_ Avatar answered May 23 '26 21:05

jf_



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!