在轴值中添加辅助描述,情节情节、轴值中

2023-09-06 07:18:42 作者:一笑泯恩仇ゝ

我正在使用包含以下列的数据框:

国家、GNI、CarSalesPerCap.我正在使用 kmeans 创建集群.在算法中,我通过两个数字列传递数据帧:'GNI', 'CarSalesPerCap'.

如何在excel图表中添加次坐标轴

然后我使用 plotly 创建散点图,其中 x 轴是 CarsalesPerCap,Y 轴是 GNI.我的问题是,我将如何为图表上绘制的每个点添加相应的国家/地区.

df = pd.read_sql_query(query,conn)df = df.dropna()#对数据进行聚类kmeans = KMeans(n_clusters=6, random_state=0).fit(df1)标签 = kmeans.labels_#粘回原始数据df['clusters'] = 标签#让我们分析集群打印 (df)cluster0=df.loc[df['clusters'] == 0]cluster1=df.loc[df['clusters'] == 1]cluster2=df.loc[df['clusters'] == 2]cluster3=df.loc[df['clusters'] == 3]cluster4=df.loc[df['clusters'] == 4]cluster5=df.loc[df['clusters'] == 5]p0 = go.Scatter(x=cluster0['CarSalesPerCap'],y = cluster0 ['GNI'],模式='标记',标记=字典(颜色='黑色'))p1 = go.Scatter(x=cluster1['CarSalesPerCap'],y = cluster1 ['GNI'],模式='标记',标记=dict(颜色='蓝绿色'))p2 = go.Scatter(x=cluster2['CarSalesPerCap'],y = cluster2 ['GNI'],模式='标记',标记=dict(颜色='灰色'))p3 = go.Scatter(x=cluster3['CarSalesPerCap'],y = cluster3 ['GNI'],模式='标记',标记=dict(颜色='粉红色'))p4 = go.Scatter(x=cluster4['CarSalesPerCap'],y = cluster4 ['GNI'],模式='标记',标记=dict(颜色='紫色'))p5 = go.Scatter(x=cluster5['CarSalesPerCap'],y = cluster5 ['GNI'],模式='标记',标记=字典(颜色='橙色​​'))布局 = go.Layout(xaxis=dict(ticks='',showticklabels=真,零线=真,标题 = 'CarSalesPerCap'),yaxis=dict(ticks='',showticklabels=真,零线=真,标题='GNI'),showlegend=False, hovermode='最近的')fig = go.Figure(data=[p0,p1,p2,p3,p4,p5], layout=layout)py.offline.plot(图)

解决方案

你可以添加一个 text 元素到你的跟踪中,它可以让你覆盖任何你想要的东西.如果您添加您的国家/地区列,那么它将在悬停时显示.如果你想要一个永久标签,你可以添加

I am using a dataframe which includes the following columns:

Country, GNI, CarSalesPerCap. I am using kmeans to create clusters. In the algorithm i pass the dataframe with the two numeric columns: 'GNI', 'CarSalesPerCap'.

Then i am using plotly to create a scatter plot, where x-axis is the CarsalesPerCap and Y-axis is GNI. My question is, how am i going to add to the plot the corresponding country for each point plotted on the graph.

df = pd.read_sql_query(query,conn)
df = df.dropna()



#Cluster the data
kmeans = KMeans(n_clusters=6, random_state=0).fit(df1)
labels = kmeans.labels_

#Glue back to originaal data
df['clusters'] = labels


#Lets analyze the clusters
print (df)
cluster0=df.loc[df['clusters'] == 0]
cluster1=df.loc[df['clusters'] == 1]
cluster2=df.loc[df['clusters'] == 2]
cluster3=df.loc[df['clusters'] == 3]
cluster4=df.loc[df['clusters'] == 4]
cluster5=df.loc[df['clusters'] == 5]

p0 = go.Scatter(x=cluster0['CarSalesPerCap'],
                y= cluster0['GNI'],
                mode='markers',
                marker=dict(color='black')
                )

p1 = go.Scatter(x=cluster1['CarSalesPerCap'],
                y= cluster1['GNI'],
                mode='markers',
                marker=dict(color='teal')
                )

p2 = go.Scatter(x=cluster2['CarSalesPerCap'],
                y= cluster2['GNI'],
                mode='markers',
                marker=dict(color='grey')
                )
p3 = go.Scatter(x=cluster3['CarSalesPerCap'],
                y= cluster3['GNI'],
                mode='markers',
                marker=dict(color='pink')
                )
p4 = go.Scatter(x=cluster4['CarSalesPerCap'],
                y= cluster4['GNI'],
                mode='markers',
                marker=dict(color='purple')
                )
p5 = go.Scatter(x=cluster5['CarSalesPerCap'],
                y= cluster5['GNI'],
                mode='markers',
                marker=dict(color='orange')
                )

layout = go.Layout(xaxis=dict(ticks='',
                              showticklabels=True,
                              zeroline=True,
                              title = 'CarSalesPerCap'),

                   yaxis=dict(ticks='',
                              showticklabels=True,
                              zeroline=True,
                              title='GNI'),
                   showlegend=False, hovermode='closest')

fig = go.Figure(data=[p0,p1,p2,p3,p4,p5], layout=layout)

py.offline.plot(fig)

解决方案

You can add a text element to your trace and it will allow you to overlay anything you want. If you add your country column then it will be displayed on hover. If you want a permanent label you can add annotations

import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
import pandas as pd
df = pd.DataFrame({'country':["USA", "MEXICO", "CANADA"], 'x':[1, 2, 4], 'y':[5, 6, 7]})
p0 = go.Scatter(
    x=df.x,
    y= df.y,
    mode='markers',
    marker=dict(
        color='#E90',
        size=15
    ),
    text = df.country,    
)

data = [p0]

iplot(data)