首页 > 解决方案 > 类似于seaborn在情节中的色调功能?

问题描述

在此处输入图像描述

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)

结果显示: 在此处输入图像描述

我想要的是在 seaborn 中使用与“hue”类似的功能: 在此处输入图像描述

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

标签: pythontime-seriesplotly

解决方案


由于您使用的是 pandas,我建议使用 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 版本开始,您甚至可以简单地通过编写以下代码来替换 pandas 的默认绘图后端:

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/


推荐阅读