简体   繁体   中英

Changing grid line thickness in 3D surface plot in Python Matplotlib

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 :

示例 3D 曲面图

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?

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 eg

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM