在曲线上

时间:2018-06-17 18:27:08

标签: python-3.x plotly

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

Country, GNI, CarSalesPerCap。我正在使用kmeans来创建集群。在算法中,我使用两个数字列传递数据框:'GNI', 'CarSalesPerCap'

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

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)

1 个答案:

答案 0 :(得分:0)

您可以在跟踪中添加text元素,它可以覆盖您想要的任何内容。如果您添加国家/地区列,则会在悬停时显示。如果您想要永久标签,可以添加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)

enter image description here