[英]Plotly: How to manually set the color of points in plotly express scatter plots?
[英]How to create 3d scatter plots for clustering in plotly with having each point color set to color of our choice?
我正在研究由 k-means 中的預測 0 和 1 表示的數據集聚類。 我現在正在尋找 plot 這些點以便更好地可視化它們。 我也將我的 dataframe 轉換為 3d。
import plotly
import plotly.graph_objects as go
import numpy as np
plotly.offline.init_notebook_mode(connected=True)
%matplotlib inline
colors = ["red", "gray"]
for i in range(len(x_pca)):
fig = go.Figure(data=[go.Scatter3d(x=x,y=y,z=z, mode='markers',marker=
dict(size=12,color=colors[p[i]],opacity=0.8))])
fig.show()
### p is a list containing values in the form of 0,1,0,0,1.......
# x,y,z are all list containing the points convereted into 3d using pca.
使用上面的代碼,我可以打印 3d plot 但所有點的顏色都是紅色。
我希望將兩種類型的點變成紅色和灰色。 有沒有辦法做到這一點?
df3 dataframe 是使用 x,y,z 和 p 列表制作的。
import plotly.graph_objects as go
import plotly.graph_objects as go
PLOT = go.Figure()
for C in list(df3d.p.unique()):
PLOT.add_trace(go.Scatter3d(x = df3d[df3d.p == C]['x'],
y = df3d[df3d.p == C]['y'],
z = df3d[df3d.p == C]['z'],
mode = 'markers', marker_size = 8, marker_line_width = 1,
name = 'Cluster ' + str(C)))
您可以為磁盤 colors 創建 function。 例如,假設我們有以下數據集(xyzd):
2 2 2 0
3 3 3 1
4 4 4 1
2 2 2 0
my_data = np.genfromtxt('data.txt', delimiter = '\t')
def Data_Color(i):
if (i >0):
return "red"
elif (i <= 0):
return "gray"
fig=go.Figure(go.Scatter3d(x=my_data[:,0],y=my_data[:,1],z=my_data[:,2],
showlegend=True,
mode='markers',
marker=dict(symbol='circle',
size=10,
color=list(map(Data_Color, my_data[:,3]))
)
)
)
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.