Colour scatter plot by column Plotly

nilsinelabore

I would like to create a scatter plot with 3 variables: Age, Value and City. How can I colour the plot by City?

Current output is a simple scatter plot of Value against Age: enter image description here

Current Code:

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()

Update:

Tried:

import plotly.express as px

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

and caught error:

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

Update:

Age and Value have been cleaned. Here are some unique values for City(sorry to change the column). There are some messy figures.

       ['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'...]
Konqui

Inside the go.Scatter definition you should specify the color parameter as color=data1['Continent']. See the Plotly documentation for more information.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related