[英]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)
如何按不同顏色的區域進行繪圖編碼?
由於您使用的是熊貓,我建議使用 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.