Changing grid line thickness in 3D surface plot in

2020-07-22 18:12发布

问题:

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:

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?

回答1:

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()



回答2:

plt.rcParams['grid.linewidth'] = 3