gpt4 book ai didi

python - 类似于plotly中seaborn的hue函数?

转载 作者:行者123 更新时间:2023-12-01 01:39:31 25 4
gpt4 key购买 nike

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

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

最佳答案

由于您使用的是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/

关于python - 类似于plotly中seaborn的hue函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52047884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com