Networkx图表在不同平台上的大小不同

时间:2017-11-19 16:43:36

标签: python matplotlib networkx

Mac iPython notebook Windows Pycharm我有一个简短的代码,用于绘制图表:

plt.figure(figsize=(15,15),dpi=300)
ax = plt.subplot(111)
nd = nx.draw_networkx_nodes(G, pos, node_color=node_cols, linewidths=1, node_size=node_sizes)
nd.set_edgecolor('w') #<- set the edgecolor to red on the node markers
nx.draw_networkx_edges(G, pos, edge_color=edge_cols,width=edge_sizes)
nx.draw_networkx_labels(G, pos, labels, font_size=fs, font_weight='bold')
plt.tick_params(
    axis='both',          # changes apply to both axes
    which='both',      # both major and minor ticks are affected
    bottom='off',      # ticks along the bottom edge are off
    top='off',         # ticks along the top edge are off
    labelbottom='off', # labels along the bottom edge are off
    left='off',
    labelleft='off')
#plt.show()
plt.savefig('foo.pdf')

在iPython笔记本电脑上运行Mac时,以及从pycharm在Windows上运行时,尺寸会有所不同。有办法解决这个问题吗?我如何明知指定edge_sizes和node_sizes? 谢谢!

1 个答案:

答案 0 :(得分:1)

jupyter笔记本中matplotlib数字的默认数字大小和dpi与通常的常用脚本不同。

您可以通过

找到答案
import matplotlib.pyplot as plt
print(plt.rcParams["figure.figsize"])
print(plt.rcParams["figure.dpi"])

应为jupyter笔记本打印[6.0, 4.0] 72.0,为脚本打印[6.4, 4.8] 100.0

要设置这些参数,请使用

plt.rcParams["figure.figsize"] = 6,4
plt.rcParams["figure.dpi"] = 100

为所有输出设置它们, 或

plt.figure(figsize=(6,4), dpi=100) 

单个数字。这样,您可以确保脚本和笔记本都具有相同的输出。