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)
如何按不同颜色的区域进行绘图编码?
答案 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")