similar to seaborn's hue function in plotly?

风格不统一 提交于 2020-06-17 01:53:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!