python - Changing 3D scatter plot color based on specific column
问题描述
I have some example of information as shown below and I want to make 3D scatter plot with different color of the scatter based on the "clusters" (e.g. 0,1,2)
ID TP ALB BUN clusters
1 153 101 698 1
2 100 90 400 0
3 50 199 500 1
4 113 102 340 2
Currently I have tried:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(10, 8))
ax1 = fig.add_subplot(111,projection='3d')
for i in range(len(df_tr)):
x, y, z = df_tr.iloc[i]['BUN'], df_tr.iloc[i]['ALB'], df_tr.iloc[i]['TP']
ax1.scatter(x, y, z, c=['blue'])
ax1.text(x, y, z, '{0}'.format(df_tr.iloc[i]
['clusters']), size=12)
ax1.set_xlabel('BUN')
ax1.set_ylabel('ALB')
ax1.set_zlabel('TP')
ax1.legend('012')
plt.show()
Getting the result of the scatter has the cluster information (0,1, and 2), but is there anyway to change the scatter color based on the ifnormation on specific column using Axes3D?
解决方案
根据您期望的不同颜色的预期数量clusters
(只要不是很高)制作一个颜色列表,例如
colors = ['blue', 'green', 'red']
然后只需使用该clusters
值作为列表的索引来获取颜色;
colors = ['blue', 'green', 'red']
for i in range(len(df_tr)):
x, y, z = df_tr.iloc[i]['BUN'], df_tr.iloc[i]['ALB'], df_tr.iloc[i]['TP']
ax1.scatter(x, y, z, c=colors[int(df_tr.iloc[i]['clusters'])])
推荐阅读
- r - 第一个和最后一个变量的平均值
- vuejs2 - 如何从 Vue 组件中的 beforeRouteEnter 访问羽毛服务?
- php - 遍历我的关注者,以便从消息数据库中获取他们的消息
- php - php-cgi.bin (0.0) - [3425] 远程主机拒绝尝试连接操作。崩溃的服务器
- ios - Add Gradient Layer to UIImageView in IOS thru CAGradientLayer
- powerbi - 结果应用数字切片器会给出不正确的值
- jquery - 带有 Jquery 的选项卡
- c - How to optimize reversing the order of groups of bits
- wordpress - 如何使 Redux 主题选项面板正常工作?
- amazon-web-services - AWS secure REST API with mutual authentication