gpt4 book ai didi

python-3.x - 使用 Bokeh : How does one plot variable size nodes, 和节点颜色?

转载 作者:行者123 更新时间:2023-12-03 18:33:10 25 4
gpt4 key购买 nike

我正在尝试使用 networkx 和 显示图形bokeh 12.7

  • 具有基于节点度数的节点大小
  • 基于另一个节点属性的颜色。

  • 期望输出:

    enter image description here

    数据设置
    import pandas as pd
    import numpy as np
    import networkx as nx
    import seaborn as sns

    from bokeh.io import show, output_notebook #output_file,
    from bokeh.plotting import figure
    from bokeh.models.graphs import from_networkx

    from bokeh.models import GraphRenderer, StaticLayoutProvider, LinearColorMapper, ColumnDataSource
    from bokeh.palettes import Spectral8, Spectral4

    G = nx.karate_club_graph()

    # Some Random index
    node_color = {k:v for k, v in enumerate(np.random.uniform(low=0, high=21, size=(G.number_of_nodes(),)).round(1))}

    # Set node attributes
    nx.set_node_attributes(G, 'node_color', node_color)
    nx.set_node_attributes(G, 'node_size', G.degree())

    尝试使用 bokeh 绘图与 cubehelix_palette
    # Map cubehelix_palette
    palette = sns.cubehelix_palette(21)
    pal_hex_lst = palette.as_hex()

    mapper = LinearColorMapper(palette=pal_hex_lst, low=0, high=21)

    # Initiate bokeh plot
    plot = figure(title="Resized Node Demo", x_range=(-1.1,1.1), y_range=(-1.1,1.1),
    tools="", toolbar_location=None)

    # Graph renderer using nx
    graph = from_networkx(G, nx.spring_layout, scale=2, center=(0,0))

    # Style node
    graph.node_renderer.glyph = Circle(size='node_size', fill_color={'field': 'node_color', 'transform': mapper})


    plot.renderers.append(graph)

    output_notebook()
    #output_file("networkx_graph.html")
    show(plot)

    这给出了这个错误: Glyph refers to nonexistent column name
    enter image description here

    我也试过这个:
    # 1. Create Plot container
    plot = figure(title=endNode, x_range=(-1.1,1.1), y_range=(-1.1,1.1),
    tools="", toolbar_location=None)

    # 2. Create graph plot comtainer
    graph = GraphRenderer()

    node_link_dict = nx.readwrite.json_graph.node_link_data(G)
    node_df = pd.DataFrame(node_link_dict['nodes'])

    node_cds = ColumnDataSource.from_df(graph_data.node_df)
    graph.node_renderer.data_source.data = node_cds


    # 3. Set Node Style
    graph.node_renderer.glyph = Circle(size='node_size', fill_color='node_color')

    有什么想法吗?

    最佳答案

    我也试图根据度中心性设置节点大小,并且能够使用

    graph.node_renderer.data_source = source

    我可以看到不同的尺寸和颜色(见附图),虽然我还没有找到以下错误的原因

    E-1010 (CDSVIEW_SOURCE_DOESNT_MATCH): CDSView used by Glyph renderer must have a source that matches the Glyph renderer's data source: GlyphRenderer(id='035dd78a-7bff-40d1-8357-d7193222ca02', ...)


        #just to make the sizes visible
    node_size = {k:5*v for k,v in G.degree().items()}


    ### set node attributes
    nx.set_node_attributes(G, 'node_color', node_color)
    nx.set_node_attributes(G, 'node_size', node_size)

    source=ColumnDataSource(pd.DataFrame.from_dict({k:v for k,v in G.nodes(data=True)},orient='index'))
    mapper = LinearColorMapper(palette=pal_hex_lst, low=0, high=21)

    ### Initiate bokeh plot
    plot = figure(title="Resized Node Demo", x_range=(-1.1,1.1), y_range=(-1.1,1.1),
    tools="", toolbar_location=None)

    # Graph renderer using nx
    graph = from_networkx(G, nx.spring_layout, scale=2, center=(0,0))

    # Style node
    graph.node_renderer.data_source = source
    graph.node_renderer.glyph = Circle(size='node_size', fill_color={'field': 'node_color', 'transform': mapper})


    plot.renderers.append(graph)

    The zPGap Image

    关于python-3.x - 使用 Bokeh : How does one plot variable size nodes, 和节点颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46397671/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com