【问题标题】:Change 3D background to black in matplotlib在 matplotlib 中将 3D 背景更改为黑色
【发布时间】:2018-06-29 19:05:45
【问题描述】:

我无法将 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')

【问题讨论】:

    标签: python matplotlib mplot3d


    【解决方案1】:

    如果你想保留线框轴,你可以这样做

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

    【讨论】:

      【解决方案2】:

      使用 Matplotlib 的深色背景 style sheet,它将白色用于通常为黑色的元素(文本、边框等)。它也适用于 3D 绘图。

      import numpy as np
      import matplotlib.pyplot as plt
      
      
      plt.style.use('dark_background')
      
      fig, ax = plt.subplots()
      
      L = 6
      x = np.linspace(0, L)
      ncolors = len(plt.rcParams['axes.prop_cycle'])
      shift = np.linspace(0, L, ncolors, endpoint=False)
      for s in shift:
          ax.plot(x, np.sin(x + s), 'o-')
      ax.set_xlabel('x-axis')
      ax.set_ylabel('y-axis')
      ax.set_title("'dark_background' style sheet")
      
      plt.show()
      

      【讨论】:

      • plt.style.use 将为 jupyter notebook 中的所有绘图设置样式,我只想为特定绘图执行此操作
      • 然后将样式重置为默认值。 plt.style.use('default')
      猜你喜欢
      • 2020-04-03
      • 2016-08-17
      • 2013-04-20
      • 2011-02-12
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      • 1970-01-01
      相关资源
      最近更新 更多