3

I'm trying to change the thickness and transparency of the lines that make up the grid in the background of a surface plot like this example from Matplotlib's website:

Example 3D surface plot

Here's the source code:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np

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

# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                   linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

I've tried calling ax.grid(linewidth=x) but that doesn't seem to make a difference. Is there some other way to change the thickness?

Sean
  • 1,346
  • 13
  • 24

2 Answers2

4

A way to set the grid parameters in mplot3d is to update the _axinfo dictionary of the respective axis.

To set the linewidth of the grid in y direction, use e.g.

ax.yaxis._axinfo["grid"]['linewidth'] = 3.

Here is a general example:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_xlabel("x"); ax.set_ylabel("y"); ax.set_zlabel("z")
print ax.xaxis._axinfo

ax.xaxis._axinfo["grid"].update({"linewidth":1, "color" : "green"})

ax.yaxis._axinfo["grid"]['linewidth'] = 3.

ax.zaxis._axinfo["grid"]['color'] = "#ee0009"
ax.zaxis._axinfo["grid"]['linestyle'] = ":"


plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thanks for that. It seems like there's a bug in the surface plot code that overrides this setting. Your example works, but using it in my own example doesn't. I'll investigate it further, but in the mean time I'm accepting your answer. – Sean Jan 29 '17 at 22:15
  • I am having the same problem and this solution didn't work for me. Is there any other way to do it? – Khushboo Nov 05 '18 at 01:43
0
plt.rcParams['grid.linewidth'] = 3
SYuan
  • 161
  • 1
  • 6