簡體   English   中英

類似於 seaborn 在 plotly 中的 hue 功能?

[英]similar to seaborn's hue function in plotly?

在此處輸入圖像描述

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 中使用與“色調”類似的功能: 在此處輸入圖像描述

如何按不同顏色的區域進行繪圖編碼?

由於您使用的是熊貓,我建議使用 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/

問題解決了:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM