gpt4 book ai didi

python - 如何使用networkx nx.draw()使x轴和y轴出现?

转载 作者:行者123 更新时间:2023-12-01 00:31:05 24 4
gpt4 key购买 nike

我有一些创建图表的代码。但是,我不明白如何让 x 轴和 y 轴出现。我已经尝试过ax.tick_params(left=True, bottom=True, labelleft=True, labelbottom=True)但它不起作用。

我的代码

import networkx as nx
import matplotlib.pyplot as plt

Adj = np.random.randint(0,2,(5,5))

x = np.random.uniform(0,1,5)
y = np.random.uniform(0,1,5)
# convert to list of tuples
M_pos = list(zip(x,y))
# give each neuron a number, and put in a dictionary
nums = list(range(0,5))
pos_dict = dict(zip(nums,M_pos))
# construct the graph from the neuron positions and adjacency matrix
GR = nx.from_numpy_matrix(Adj, pos_dict);

figure(figsize=(10, 5))
plt.title('Neuron spatial location')
#nx.draw_networkx_labels(GR, pos_dict)
nx.draw(GR, pos_dict, node_size=40, node_color='b');

最佳答案

您可以创建一个轴实例并传递给nx.draw:

fig, ax = plt.subplots(figsize=(10, 5))
plt.title('Neuron spatial location')
#nx.draw_networkx_labels(GR, pos_dict)
nx.draw(GR, pos_dict, node_size=40, node_color='b', ax=ax)

# turn the axis on
ax.set_axis_on()
ax.tick_params(left=True, bottom=True, labelleft=True, labelbottom=True)

输出:

enter image description here

关于python - 如何使用networkx nx.draw()使x轴和y轴出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58173295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com