【问题标题】:Gridlines not showing up网格线不显示
【发布时间】:2014-08-23 19:44:34
【问题描述】:

我设置了以下代码来读取 .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(),我还需要使用其他东西吗?

【问题讨论】:

    标签: python matplotlib gridlines


    【解决方案1】:

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

    【讨论】:

    • 调用ax.grid (b=true) 而不是ax.grid () 对我来说已经足够了。 It seems to be the right way to use it: "如果提供了任何 kwargs,则假定您希望打开网格并且 b 将设置为 True。如果 b 为 None 并且没有 kwargs,这将切换行的可见性。 i>"
    猜你喜欢
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多