在坐标轴值中添加次要描述 [英] add secondary description in axis values, plotly

查看:98
本文介绍了在坐标轴值中添加次要描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using a dataframe which includes the following columns:

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

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

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

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)

推荐答案

您可以在跟踪中添加text元素,该元素将允许您覆盖所需的任何内容.如果添加国家/地区列,则该列将显示在悬停上.如果您想要一个永久标签,可以添加注释

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)

这篇关于在坐标轴值中添加次要描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆