简体   繁体   English

如何在 python 中使用 pyplot 通过 arrays 绘制 plot 图?

[英]How to plot graph by arrays using pyplot in python?

I'm studying about building the GCN and now i implement this code below.我正在研究构建 GCN,现在我在下面实现此代码。

I run it on jupyternotebook.我在 jupyternotebook 上运行它。

In [1]在 [1]

import numpy as np
from networkx import karate_club_graph, to_numpy_matrix
zkc = karate_club_graph()
order = sorted(list(zkc.nodes()))
A = to_numpy_matrix(zkc, nodelist=order)
I = np.eye(zkc.number_of_nodes())
A_hat = A + I
D_hat = np.array(np.sum(A_hat, axis=0))[0]
D_hat = np.matrix(np.diag(D_hat))

In [2]在 [2]

X = np.matrix([
        [i, -i]
        for i in range(A.shape[0])
    ], dtype=float)

In [3]在 [3]

 W = np.matrix([
         [1, -1],
         [-1, 1]
     ])

In [4]在 [4]

  W_1 = np.random.normal(
    loc=0, scale=1, size=(zkc.number_of_nodes(), 4))
  W_2 = np.random.normal(
    loc=0, size=(W_1.shape[1], 2))

In [5]在 [5]

  def gcn_layer(A_hat, D_hat, X, W):
      return D_hat**-1 * A_hat * X * W
  H_1 = gcn_layer(A_hat, D_hat, I, W_1)
  H_2 = gcn_layer(A_hat, D_hat, H_1, W_2)
      output = H_2

In [6]在[6]

 feature_representations = {
  node: np.array(output)[node] 
  for node in zkc.nodes()}

This is all code and i want to create graph like this but i don't know how to plot it by using arrays on matplotlib.pyplot thankyou.这是所有代码,我想创建这样的图形,但我不知道如何通过在 matplotlib.pyplot 上使用 arrays 来 plot 它。

enter image description here在此处输入图像描述

As I understand, you want to plot values from feature_representations , but the problem is that it is dictionary where values are arrays.据我了解,您想要 plot 值来自feature_representations ,但问题是它是字典,其中值是 arrays。 In order to use matplotlib.pyplot.scatter properly ( https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.scatter.html ), you have to convert your dictionary to two arrays that can be passed as x and y arguments. In order to use matplotlib.pyplot.scatter properly ( https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.scatter.html ), you have to convert your dictionary to two arrays that can be passed as x 和 y arguments。

Maybe try the following code in the [7] cell:也许在 [7] 单元格中尝试以下代码:

from matplotlib import pyplot as plt
arrays = feature_representations.values()
X = [el[0] for el in arrays]
Y = [el[0] for el in arrays]
plt.scatter(X, Y)

It gives the following output:它给出了以下 output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM