【问题标题】:Creating chord diagram in Python在 Python 中创建和弦图
【发布时间】:2023-04-01 21:40:01
【问题描述】:

我想为以下数据集创建一个和弦图,其中前两列作为物理位置,第三列显示有多少人访问了这两个数据集。

Place1   Place2    Count
US       UK        200
FR       US        450
UK       US        200
NL       FR        150
IT       FR        500

我尝试使用 Holoviews,但无法使用

nodes = hv.Dataset(df, 'Place1', 'Place2')
chord = hv.Chord((df, nodes), ['Place1', 'Place2'], ['Count'])
graph = chord.select(selection_mode='nodes')

但我收到以下错误:DataError: 没有可用的存储后端能够支持提供的数据格式。

如何使用此数据框创建和弦图?

【问题讨论】:

    标签: python pandas holoviews chord-diagram


    【解决方案1】:

    一个可能的解决方案如下。请记住,您共享的数据不是很大,并且生成的和弦图非常难看。

    import holoviews as hv
    chords = chord.groupby(by=["Place1", "Place2"]).sum()[["Count"]].reset_index()
    chords = chords.sort_values(by="Count", ascending=False)
    
    CChord = hv.Chord(chords)
    print(CChord)
    hv.extension("bokeh")
    CChord
    
    

    最后一部分hv.extension("bokeh") 对可视化至关重要。您甚至可以使用以下方式添加标签:

    cities = list(set(chords["Place1"].unique().tolist() + chords["Place2"].unique().tolist()))
    cities_dataset = hv.Dataset(pd.DataFrame(cities, columns=["City"]))
    

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 2021-02-14
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 2013-12-02
      • 2011-12-23
      • 1970-01-01
      • 2014-02-19
      相关资源
      最近更新 更多