【问题标题】:Matplotlib 3D scatterplot cmapMatplotlib 3D散点图cmap
【发布时间】:2021-03-18 17:37:12
【问题描述】:

**已编辑以添加示例数据

我有一个用于制作 3d 散点图的数据框。

两列都有我想翻译成点属性的信息。

Exp 列是该点属于哪个系列 - 有 7 个。每个系列有 9 个点 - 这是 Day 列,范围从 0 到 8。

我希望每个系列都具有不同的颜色 - 下面的代码做得很好,但我希望每天都是 alpha,以便它们从 0=light 8=dark 开始。

或者,如果每个系列都有一个唯一的顺序颜色图,其渐变取自日列中的值,也可以使用。

简单得令人沮丧,但我不太明白!有任何帮助。

anp = df.loc[:, 'Exp'].values
set_res = set(anp) 
list_res = (list(set_res))

d = df.loc[:, 'Day'].values
set_d = set(d) 
list_d = (list(set_d))

fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(111, projection='3d') 
ax.set_xlabel('X', fontsize = 15)
ax.set_ylabel('Y', fontsize = 15)
ax.set_zlabel('Z', fontsize = 15)
ax.set_title('3D', fontsize = 20)
colors = ['r','g','y','b','m','k','c']
for target, color, day in zip(list_res,colors,list_d):
    indicesToKeep = df['Exp'] == target
    indicesToKeep = df['Day'] == day
    ax.scatter(data.loc[indicesToKeep, 'X']
               , data.loc[indicesToKeep, 'Y']
               , data.loc[indicesToKeep, 'Z']
               , c = color
               , alpha = day
               , s = 10)
ax.legend(list_res)

ax.grid()

plt.show()


    X               Y           Z               Day Exp
0   6.065799089     5.39606408  0.20680998      0   Exp1
1   -1.813171466    -0.14416587 1.152384796     1   Exp1
2   -3.676586344    1.358197191 0.882998403     2   Exp1
3   -5.074761537    2.214899107 -0.198255954    3   Exp1
4   -5.235090681    2.548416921 0.289574373     5   Exp1
5   -5.486962521    2.437908275 -0.496139886    4   Exp1
6   -5.939524094    2.939749551 -0.825422951    6   Exp1
7   -6.380253741    2.976652955 -1.3211511      7   Exp1
8   -5.952184603    2.80820602  -1.08558843     8   Exp1
9   6.501631702 6.755084113 0.056963689         0   Exp2
10  2.141323206 -2.942530182    0.276459756     1   Exp2
11  1.799786018 -2.761261089    -0.03426698     2   Exp2
12  1.58787416  -2.73071711 0.134785134         3   Exp2
13  1.731188598 -2.783161946    0.168444894     4   Exp2
14  1.959546043 -2.860391099    0.735327034     5   Exp2
15  1.763603911 -2.742837672    0.060296845     6   Exp2
16  1.84718194  -2.785950848    0.236620814     7   Exp2
17  1.986642504 -2.541517762    -0.773824298    8   Exp2

【问题讨论】:

  • 这将有助于查看您的数据框,最好作为 csv 文件。考虑将其发布到外部链接。
  • @pakpe 我从 7 个 Exp 系列的前 2 个中添加了一些示例数据

标签: python matplotlib scatter-plot


【解决方案1】:

我正在研究与提出的案例类似的图表。 我认为这会有所帮助:

fig = plt.figure(figsize = (12,12))
ax = fig.add_subplot(111, projection='3d') 

#colors = ['r', 'g', 'y', 'b','m','k','c']
colors_dict = {"Exp1":'r', "Exp2":'g', "Exp3":'y', "Exp4":'b', "Exp5":'m', "Exp6":'k', "Exp7":'c'}

ax.set_xlabel('X', fontsize = 15)
ax.set_ylabel('Y', fontsize = 15)
ax.set_zlabel('Z', fontsize = 15)
ax.set_title('3D', fontsize = 20)

for i in df.index:
    ax.scatter(df.X[i],
               df.Y[i],
               df.Z[i],
               color = colors_dict[df.Exp[i]],
               alpha = df.Day[i]/10,
               label = df.Exp[i],
               s=40)

# To avoid duplicated labels
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys(), loc = 6)

plt.show() 

另一个效果不错的解决方案是使用df.groupby(),然后按名称和组进行迭代。

希望对你有用。 问候

【讨论】:

    猜你喜欢
    • 2015-10-18
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 2021-10-21
    • 2019-02-06
    • 2011-07-26
    相关资源
    最近更新 更多