网格线未显示
我写了下面的代码,用来读取一个 .graphml 文件,进行一些计算(特征值),然后把结果画出来。以下是我目前的代码:
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
# Read in the Data
G = nx.read_graphml("/home/user/DropBox_External_Datasets/JHU_Human_Brain/cat_brain_1.graphml")
nx.draw(G)
plt.savefig("test_graph.png")
Z = nx.to_numpy_matrix(G)
# Get Eigenvalues and Eigenvectors
# ----------------------------------------------------------------------------------
#
e_vals, e_vec = np.linalg.eigh(Z)
print("The eigenvalues of A are:", e_vals)
print("The size of the eigenvalues matrix is:", e_vals.shape)
# ----------------------------------------------------------------------------------
plt.plot(e_vals, 'g^')
plt.ylabel('Eigenvalues')
# plt.axis([-30, 300, -15, 30]) # Optimal settings for Rhesus data
# plt.axis([-0.07, 1, -0.2, 1.2]) # range to zoom in on cluster of points in Rhesus data
plt.grid(b=True, which='major', color='b', linestyle='-')
plt.show()
但是在图上没有显示网格线和坐标轴。我是不是还需要用其他的东西,而不是 plt.grid()
?
1 个回答
17
我发现使用 matplotlib
的面向对象API 是一种更可靠的方法,可以让事情按预期工作。Pyplot 实际上是对面向对象调用的一个大包装。 我写了一些应该是等价的代码:
import matplotlib.pyplot as plt
# ... your other code here
# Using subplots
fig, ax = plt.subplots(ncols=1, nrows=1) # These arguments can be omitted for one
# plot, I just include them for clarity
ax.plot(e_vals, 'g^')
ax.set_ylabel('Eigenvalues')
ax.grid(b=True, which='major', color='b', linestyle='-')
plt.show()