python - 网格线不显示

标签 python matplotlib gridlines

我已经设置了以下代码以读取 .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()我需要使用什么?

最佳答案

我发现使用 matplotlib object oriented 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()

关于python - 网格线不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24540915/

相关文章:

jfreechart - 将静态网格线添加到 JFreeChart 时间序列图表

php - 缩放数据库中的临时记录以计算总数以停止过度订阅

python - 用户如何将自己的值添加到 dash_core_components.Dropdown 中?

python - 如何解决六角形图中的边缘问题?

python - 如何在 Matplotlib 的堆叠水平条形图中显示数据值

javascript - 在 HTML 和 Javascript 中添加网格线到背景,类似于 VisualStudios 网格

python - 极坐标图 - 将一条网格线加粗

python - 迭代列表python

python - 解决 python 的 json 模块不喜欢循环引用

python - 如何使用 Matplotlib 在 Python 中计算轮廓内的面积?