问题
geo1 = go.Scatter(
x=geo['Year'],
y=geo['Number'],
mode='lines',
marker=dict(color=geo['Geographical region'],size=4, showscale=False),
name='geo',
showlegend=True)
data = [geo1]
layout = dict(
title='Working VISA in UK by Regions',
xaxis=dict(title='Year'),
yaxis=dict(title='Number'), showlegend=True)
fig = dict(data=data, layout=layout)
iplot(fig)
The result shows:
what I want is to use a similar function as 'hue' in seaborn:
how to do the plotly coding by regions in different colors?
回答1:
Problem solved:
traces=[]
for x, geo_region in geo.groupby('Geographical region'):
traces.append(go.Scatter(x=geo_region.Dates, y=geo_region.Number, name=x, mode='lines'))
fig = go.Figure(data=traces)
iplot(fig)
回答2:
Since you are using pandas, I recommend to use Plotly Express. In this case the code is very simple and intuitive:
import plotly.express as px
fig = px.line(geo, x="Year", y="Number", color="Geographical region",
line_group="Geographical region")
fig.show()
Or, from version 4.8, you can even replace the default plotting backend of pandas simply by writing:
import pandas as pd
pd.options.plotting.backend = "plotly" # just once at the beginning
geo.plot.line(x="Year", y="Number", color="Geographical region",
line_group="Geographical region")
Reference: https://plotly.com/python/plotly-express/
来源:https://stackoverflow.com/questions/52047884/similar-to-seaborns-hue-function-in-plotly