python - 在 matplotlib 中将 3D 背景更改为黑色
问题描述
我无法将 3d 图形的背景更改为黑色。这是我当前的代码。当我将 facecolor 设置为黑色时,它会将图形内部更改为灰色,这不是我想要的。
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')
解决方案
如果你想保持线框轴,你可以做
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))
推荐阅读
- linux - 在没有包装脚本的情况下将 stderr 和 stdout 写入文件
- azure-devops - Azure Devops OpenShift 并行发布
- c++ - 使用 registerConstructor 注入 Mock
- javascript - 递归方法继续前进,但方向相反。为什么?
- docker - Nginx 反向代理无法与容器通信
- c++ - 如何找到当前用户名和系统盘?
- flutter - ElevatedButton 不接受 SizedBox 的限制
- java - 将文档保存到数据存储中的特定集合
- subdomain - 如何将第三方 PWA 链接到域名
- javascript - 如何在多步 javascript 表单中添加单选按钮、复选框和选择字段?