Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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?

like image 787
Sean Avatar asked Apr 07 '26 06:04

Sean


1 Answers

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

like image 163
ImportanceOfBeingErnest Avatar answered Apr 08 '26 20:04

ImportanceOfBeingErnest



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!