【问题标题】:How to generate this weird plot? What type of plot is it anyway?如何生成这个奇怪的情节?它到底是什么类型的情节?
【发布时间】:2022-01-21 18:52:08
【问题描述】:

两个函数生成两组解。左侧列出了一个函数的解决方案,右侧列出了另一组解决方案。

我想重现这种表现形式。我不知道它是什么样的情节,或者如何编程,或者使用什么技术(但我最熟悉matplotlib)。请指导我。

【问题讨论】:

标签: matplotlib plot graph


【解决方案1】:

我不确定数据是什么样子,但这里有一个使用 networkx 的解决方案。

import networkx as nx
import matplotlib.pyplot as plt
import numpy as np

# random data
bi = np.random.uniform(-5, 5, size=(20,))
vi = np.random.uniform(-5, 5, size=(20,))


def make_biGraph(S1, S2):
    # Create the Bipartite Graph using the solution sets
    B = nx.Graph()
    edgelist = [(s1, s2) for s1, s2 in zip(S1, S2)]
    B.add_edges_from(edgelist)
    return B

B = make_biGraph(bi, vi)

def draw_graph(B, S1, S2):
    pos = {}
    pos.update((node, (1, node)) for node in S1)
    pos.update((node, (2, node)) for node in S2)

    fig, ax = plt.subplots()
    nx.draw_networkx(B, pos=pos,
                     node_size=20,
                     node_color='k',
                     with_labels=False,
                     ax=ax
                     )
    ax.tick_params(left=True, bottom=True, labelleft=True, labelbottom=True)
    ax.set_yticks([-5, 0, 5])
    ax.set_xticks([1, 2])
    ax.set_xticklabels([r'$\delta W,BI$', r'$\delta W,VI$'])
    plt.show()

draw_graph(B, bi, vi)

附注
这看起来像一个梯形图,我找不到任何基于 python 的解决方案。
这是使用 r 的solution

【讨论】:

    【解决方案2】:

    用 plt.scatter 可以实现作为标记的同心圆。 然后,您可以单独绘制线条。

    a = np.random.uniform(-.5,.5,10)
    b = np.random.uniform(-.5,.5,10)
    
    plt.scatter([0]*len(a), a, s=100, c='k')
    plt.scatter([0]*len(a), a, s=75, c='white')
    plt.scatter([0]*len(a), a, s=35, c='k')
    
    plt.scatter([1]*len(b), b, s=100, c='k')
    plt.scatter([1]*len(b), b, s=75, c='white')
    plt.scatter([1]*len(b), b, s=35, c='k')
    
    for x1,y1 in zip(a,b):
        plt.plot([0,1], [x1,y1], c='k')
    
    plt.xlim(-.25,1.25)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 2014-06-22
      • 2022-01-19
      • 2018-10-07
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      相关资源
      最近更新 更多