【问题标题】:similar to seaborn's hue function in plotly?类似于情节中seaborn的色调功能?
【发布时间】:2019-02-02 11:56:56
【问题描述】:

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”类似的功能:

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

【问题讨论】:

    标签: python time-series plotly


    【解决方案1】:

    问题解决了:

    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)
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2020-11-11
        • 2012-12-15
        • 2020-02-14
        • 2018-11-09
        • 2012-12-19
        • 1970-01-01
        • 2012-10-04
        • 2019-03-31
        相关资源
        最近更新 更多