【问题标题】:add secondary description in axis values, plotly在轴值中添加辅助描述,情节
【发布时间】:2018-11-26 16:53:35
【问题描述】:

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

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)

【问题讨论】:

  • 您可以对国家/地区进行颜色编码或为不同的国家/地区使用不同的标记类型。

标签: python-3.x plotly


【解决方案1】:

您可以将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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2013-01-23
    • 2019-09-19
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多