Matplotlib中更改3D表面图中的网格线粗细

Matplotlib中更改3D表面图中的网格线粗细

本文介绍了在Python Matplotlib中更改3D表面图中的网格线粗细的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改表面图背景中构成网格的线条的粗度和透明度,例如示例:

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:

这是源代码:

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

我尝试调用ax.grid(linewidth=x),但这似乎没有什么不同.还有其他改变厚度的方法吗?

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?

推荐答案

在mplot3d中设置网格参数的一种方法是更新相应轴的_axinfo词典.

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

要在y方向上设置网格的线宽,请使用例如

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

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

这是一个一般示例:

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

这篇关于在Python Matplotlib中更改3D表面图中的网格线粗细的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:30