使用 Plotly 自定义标签
Customize labels with Plotly
我正在尝试自定义悬停时出现的数据标签:
这是给我上面输出的代码:
import pandas as pd
import plotly.plotly as py
import plotly.graph_objs as go
# Create random data
labels = ['A', 'B', 'C']
N = 20
df = pd.DataFrame(index = range(N))
standardized_cols = []
for col in labels:
df[col] = np.random.randn(N)
standardized_colname = col + "_standardized"
standardized_cols.append(standardized_colname)
df[standardized_colname] = (df[col]-df[col].mean())/df[col].std()
# Cluster
c = KMeans(n_clusters=3, random_state=1).fit(df[standardized_cols]).labels_
# Plot
trace = go.Scatter3d(
x=df.A_standardized,
y=df.B_standardized,
z=df.C_standardized,
mode='markers',
marker=dict(
size=5,
color=c,
colorscale='Viridis',
),
name= 'test',
text= c
)
data = [trace]
fig = go.Figure(data=data, layout=layout)
iplot(fig)
我的数据:
]
该图表显示了标准化列的聚类。
但是当鼠标悬停在数据上时,我想在标签中看到未标准化的数据,i.e.something like
A: 0,999
B: 0,565
C: 0,765
Cluster: 2
我进行了试验,但不知道如何实现。这可能吗?
你可以做一些列表理解并添加你想要的任何列text
见下面的示例(注意,我正在离线绘制):
# data
np.random.seed(1)
labels = ['A', 'B', 'C']
N = 20
df = pd.DataFrame(index = range(N))
standardized_cols = []
for col in labels:
df[col] = np.random.randn(N)
standardized_colname = col + "_standardized"
standardized_cols.append(standardized_colname)
df[standardized_colname] = (df[col]-df[col].mean())/df[col].std()
c = KMeans(n_clusters=3, random_state=1).fit(df[standardized_cols]).labels_
剧情:
import plotly as py
import plotly.graph_objs as go
trace = go.Scatter3d(
x=df.A_standardized,
y=df.B_standardized,
z=df.C_standardized,
mode='markers',
marker=dict(
size=5,
color=c,
colorscale='Viridis',
),
name= 'test',
# list comprehension to add text on hover
text= [f"A: {a}<br>B: {b}<br>C: {c}" for a,b,c in list(zip(df['A'], df['B'], df['C']))],
# if you do not want to display x,y,z
# hoverinfo='text'
)
layout = dict(title = 'TEST',)
data = [trace]
fig = dict(data=data, layout=layout)
py.offline.plot(fig, filename = 'stackTest.html')
您可以修改列表推导式以显示您想要的任何内容
如果不想显示x,y,z
则添加hoverinfo='text'
我正在尝试自定义悬停时出现的数据标签:
import pandas as pd
import plotly.plotly as py
import plotly.graph_objs as go
# Create random data
labels = ['A', 'B', 'C']
N = 20
df = pd.DataFrame(index = range(N))
standardized_cols = []
for col in labels:
df[col] = np.random.randn(N)
standardized_colname = col + "_standardized"
standardized_cols.append(standardized_colname)
df[standardized_colname] = (df[col]-df[col].mean())/df[col].std()
# Cluster
c = KMeans(n_clusters=3, random_state=1).fit(df[standardized_cols]).labels_
# Plot
trace = go.Scatter3d(
x=df.A_standardized,
y=df.B_standardized,
z=df.C_standardized,
mode='markers',
marker=dict(
size=5,
color=c,
colorscale='Viridis',
),
name= 'test',
text= c
)
data = [trace]
fig = go.Figure(data=data, layout=layout)
iplot(fig)
我的数据:
该图表显示了标准化列的聚类。 但是当鼠标悬停在数据上时,我想在标签中看到未标准化的数据,i.e.something like
A: 0,999
B: 0,565
C: 0,765
Cluster: 2
我进行了试验,但不知道如何实现。这可能吗?
你可以做一些列表理解并添加你想要的任何列text
见下面的示例(注意,我正在离线绘制):
# data
np.random.seed(1)
labels = ['A', 'B', 'C']
N = 20
df = pd.DataFrame(index = range(N))
standardized_cols = []
for col in labels:
df[col] = np.random.randn(N)
standardized_colname = col + "_standardized"
standardized_cols.append(standardized_colname)
df[standardized_colname] = (df[col]-df[col].mean())/df[col].std()
c = KMeans(n_clusters=3, random_state=1).fit(df[standardized_cols]).labels_
剧情:
import plotly as py
import plotly.graph_objs as go
trace = go.Scatter3d(
x=df.A_standardized,
y=df.B_standardized,
z=df.C_standardized,
mode='markers',
marker=dict(
size=5,
color=c,
colorscale='Viridis',
),
name= 'test',
# list comprehension to add text on hover
text= [f"A: {a}<br>B: {b}<br>C: {c}" for a,b,c in list(zip(df['A'], df['B'], df['C']))],
# if you do not want to display x,y,z
# hoverinfo='text'
)
layout = dict(title = 'TEST',)
data = [trace]
fig = dict(data=data, layout=layout)
py.offline.plot(fig, filename = 'stackTest.html')
您可以修改列表推导式以显示您想要的任何内容
如果不想显示x,y,z
则添加hoverinfo='text'