按列绘制颜色散点图 Plotly

Colour scatter plot by column Plotly

我想创建一个包含 3 个变量的散点图:AgeValueCity。如何通过 City?

为绘图着色

当前输出是 ValueAge 的简单散点图:

当前代码:

import datetime
import plotly.offline as py
import plotly
import plotly.graph_objects as go
fig = go.Figure()

fig.add_trace(go.Scatter(x= data1['Age'], y = data1['Value'], mode='markers', name='lines+markers'))
fig.show()

更新:

尝试过:

import plotly.express as px

fig = px.scatter(data1, x=data1['Age'], y=data1['Value'], color=data1['City']) 
fig.show()

并发现错误:

KeyError: (nan, '', '', '', '')

更新:

AgeValue 已清理。以下是 City 的一些唯一值(抱歉更改列)。有一些乱七八糟的数字。

       ['NT', 'WAIKATO', 'VICTORIA', 'South Australia', 'OTHER', 'ON',
       'Nsw', 'IL', 'MD - MARYLAND', 'ABU DHABI', 'VIENNA', 'TX',
       'VILKAVISKIS', 'NY', 'BALEARES', 'UK', 'GLOUCESTERSHIRE',
       'LA MANCHE', 'TEXAS', 'DUBAI', 'ENGLAND', 'ITALY', nan,
       'GREATER LONDON', 'BEDFORDSHIRE', 'HEREFORDSHIRE',
       'BADEN-WÃ?RTTEMBERG', 'Australian Capital Territory',
       'ABERDEENSHIRE', 'OXFORDSHIRE', 'LONDON', 'BC', 'SK',
       'NOORD-HOLLAND', 'UNITED KINGDOM', 'New South Wales', 'Brookdale',
       'Western Australia', 'GALWAY', 'Queensland', 'TOKYO',
       'HAUTE-GARONNE', 'WORCESTERSHIRE', 'CALIFORNIA', 'JAPAN',
       'NORTHUMBERLAND', 'NJ - NEW JERSEY', 'GLOS', 'DORSET', 'TENNESSEE',
       'BANGKOK', 'CANTERBURY', 'WEXFORD', 'MIDDLESEX', 'SURREY', 'MI',
       'NEVADA', 'KENTUCKY', 'NEW YORK', 'ZUID-HOLLAND', 'HONG KONG',
       'ESSEX', 'FL', 'LILLEHAMMER', 'DEVON', 'NEW TERRITORIES', 'KENT',
       'THAILAND', 'Pyrmont', 'SINGAPORE', 'FRIBOURG', 'CAIRO',
       'QUEENSLAND', 'HAMPSHIRE', 'NEW JERSEY', 'WEST MIDLANDS',
       'MICHIGAN', 'NONE', 'WI', 'BARNET', 'STAFFS', 'WARWICKSHIRE'...]

go.Scatter 定义中,您应该将 color 参数指定为 color=data1['Continent']。有关详细信息,请参阅 Plotly documentation