在 matplotlib 中将 3D 背景更改为黑色 [英] Change 3D background to black in matplotlib

查看:175
本文介绍了在 matplotlib 中将 3D 背景更改为黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将3d图形的背景更改为黑色.这是我目前的代码.当我将 facecolor 设置为黑色时,它会将图形内部更改为灰色,这不是我想要的.

I'm having trouble changing the background of my 3d graph to black. This is my current code. When I do set facecolor to black, it changes the inside of the graph to be grey, which is not what I want.

fig = plt.figure()
fig.set_size_inches(10,10)
ax = plt.axes(projection='3d')
ax.grid(False)
ax.xaxis.pane.set_edgecolor('b')
ax.yaxis.pane.set_edgecolor('b')
ax.zaxis.pane.set_edgecolor('b')

# plt.gca().patch.set_facecolor('white')
# plt.axis('On')
fig.patch.set_facecolor('black')

ax.scatter(xs = Z['PC1'], ys = Z['PC2'], zs = Z['PC3'], c = Z['color'], s = 90, depthshade= False)
ax.set(title = 'test', xlabel = 'PC1', ylabel = 'PC2', zlabel = 'PC3')

推荐答案

如果要保留线框轴,则可以

If you want to keep the wireframe axis you can do

ax.w_xaxis.pane.fill = False
ax.w_yaxis.pane.fill = False
ax.w_zaxis.pane.fill = False

这是一个完整的例子:

fig = plt.figure()
ax = plt.axes(projection='3d')
x, y, z = np.random.randint(10, size=(3, 250))

ax.scatter(x, y, z, c=np.random.randn(250))

fig.set_facecolor('black')
ax.set_facecolor('black') 
ax.grid(False) 
ax.w_xaxis.pane.fill = False
ax.w_yaxis.pane.fill = False
ax.w_zaxis.pane.fill = False

或者您可以将它们与

ax.w_xaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
ax.w_yaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
ax.w_zaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))

完整示例:

fig = plt.figure()
ax = plt.axes(projection='3d')
x, y, z = np.random.randint(10, size=(3, 250))

ax.scatter(x, y, z, c=np.random.randn(250))

fig.set_facecolor('black')
ax.set_facecolor('black')
ax.grid(False)
ax.w_xaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
ax.w_yaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
ax.w_zaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))

这篇关于在 matplotlib 中将 3D 背景更改为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆