Matplotlib在保存两个独立的图形时将它们压缩为一个

2024-04-19 11:48:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python还不熟悉。在运行这些代码行时,我希望保存两个单独的数字。你知道吗

图1:

#3) Create the graph
N=int(raw_input('Number of nodes (sqrt): '))
G=nx.grid_2d_graph(N,N)
pos = dict( (n, n) for n in G.nodes() ) #Dictionary of all positions
labels = dict( ((i, j), i + (N-1-j) * N ) for i, j in G.nodes() )
nx.draw_networkx(G, pos=pos, labels=labels,with_labels=False, node_size=10)
#Plot the graph
plt.axis('off')
title_string=('Lattice Network') 
subtitle_string=(''+str(N)+'x'+str(N)+' = '+str(N*N)+' nodes')
plt.suptitle(title_string, y=0.99, fontsize=17)
plt.title(subtitle_string, fontsize=8)
plt.savefig('100x100_lattice.png', dpi=1000,bbox='tight') #Figure no. 1
plt.close()

图2:

#4) Plot the degree distribution
for data_dict in node_degree.values():
    x=node_degree.keys()
    y=node_degree.values()
from collections import Counter
occ=Counter(y)
for data_dict in occ.values():
    plotx=occ.keys()
    ploty=occ.values()
Pk=numpy.zeros((len(ploty)))
for i in range(0, len(ploty)):
    Pk[i]=numpy.around(ploty[i]/(N*N),3)
plt.scatter(plotx,Pk,color='red', edgecolors='darkred')
plt.show() 
plt.xlabel('Node degree k')
plt.ylabel('P(k)')
plt.xlim(0,10,1)
plt.xticks(numpy.arange(0, 11, 1.0))
plt.ylim(0,1) 
plt.yticks(numpy.arange(0, 1, 0.1))
title_string=('Degree Distribution')
subtitle_string=('Lattice, '+str(N)+'x'+str(N)+' nodes') 
plt.suptitle(title_string, y=0.99, fontsize=17)
plt.title(subtitle_string, fontsize=9)
plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom='on',      # ticks along the bottom edge are off
    top='on',         # ticks along the top edge are off
    labelbottom='on')
plt.savefig('100x100_lattice_Degree_Distrib.png', dpi=1000,bbox='tight') #Figure no. 2
plt.close()

相反,我看到的是:

enter image description here

我的问题。右边的图像(在代码中,Figure no. 2)是正确的。左边的那个(Figure no. 1)是错误的,因为它应该只显示你看到的规则结构,而不是红点,这些红点显然来自第二张图片。我的plt.show电话肯定有问题,但我找不到答案。谢谢你的帮助!你知道吗


Tags: theinnodeforstringlabelstitleplt
1条回答
网友
1楼 · 发布于 2024-04-19 11:48:33

问题是plt.散射()在你的第二个图形中绘制的是你在第一个图形中创建的图形,对吗?只需添加一个呼叫到plt.图()在呼叫之前plt.散射()在第二个图中。这应该能解决你的问题。你知道吗

相关问题 更多 >