【问题标题】:Adding legends to Axes3D plot将图例添加到 Axes3D 图
【发布时间】:2022-01-23 23:11:57
【问题描述】:

我正在尝试向 3D 绘图添加图例

import matplotlib.pyplot as plt
import matplotlib.lines as lines
from mpl_toolkits.mplot3d import Axes3D

plt.rcParams["figure.figsize"] = (12, 10)
fig = plt.figure(1)
plt.clf()
ax = Axes3D(fig, 
           rect = [0, 0, .95, 1],
           elev = 48, 
           azim = 134,
           )
plt.cla()


ax.scatter(df_labeled['frequency'], df_labeled['recency'], df_labeled['monetary'], 
          c = df_labeled['label'],
          s = 200,
          alpha = 0.5,
          edgecolor = 'darkgrey',
          label = df_labeled['label'].unique()
          )

colors = ['blue', 'red', 'green']

ax.set_xlabel('Frequency', 
             fontsize = 16)
ax.set_ylabel('Recency',
             fontsize = 16)
ax.set_zlabel('Monetary',
             fontsize = 16)
ax.legend()
plt.show()

我最终得到了这个:

图例错误地显示了一种颜色和一个列表,而不是每个标签的 3 个单独的图例。我做错了什么?

【问题讨论】:

    标签: python matplotlib data-visualization mplot3d


    【解决方案1】:

    您可以通过分别绘制每种颜色来做到这一点,然后图例将为您制作的每个图提供一个条目。这是一个带有 2 个图的最小示例:

    import matplotlib.pyplot as plt
    from numpy.random import rand
    
    fig = plt.figure()
    ax = fig.add_subplot(projection='3d')
    
    n = 100
    
    ax.scatter(rand(n), rand(n), rand(n), label='Plot 1')
    ax.scatter(rand(n), rand(n), rand(n), label='Plot 2')
    
    plt.legend()
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2023-02-10
      • 2018-05-11
      • 2021-09-23
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多