类似于Seaborn的色相功能?

时间:2018-08-27 22:47:45

标签: python time-series plotly

enter image description here

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)

结果显示: enter image description here

我想在seaborn中使用与'hue'类似的功能: enter image description here

如何按不同颜色的区域进行绘图编码?

2 个答案:

答案 0 :(得分:2)

问题已解决:

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)

答案 1 :(得分:1)

由于您使用的是熊猫,所以我建议使用Plotly Express。在这种情况下,代码非常简单直观:

import plotly.express as px

fig = px.line(geo, x="Year", y="Number", color="Geographical region",
              line_group="Geographical region")
fig.show()

或者,从4.8版开始,您甚至可以简单地通过编写以下内容来替换默认的熊猫绘图后端:

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

参考:https://plotly.com/python/plotly-express/