使用NetworkX和Matplotlib的图形的高分辨率图像 [英] High Resolution Image of a Graph using NetworkX and Matplotlib

查看:580
本文介绍了使用NetworkX和Matplotlib的图形的高分辨率图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python代码来生成300个节点和200条边的随机图并显示它

I have a python code to generate a random graph of 300 nodes and 200 edges and display it

import networkx as nx
import matplotlib.pyplot as plt

G = nx.gnm_random_graph(300,200)
graph_pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, graph_pos, node_size=10, node_color='blue', alpha=0.3)
nx.draw_networkx_edges(G, graph_pos)
nx.draw_networkx_labels(G, graph_pos, font_size=8, font_family='sans-serif')
plt.show()

但是,由于节点太多,我需要有更多像素,以便可以放大并保存它.

However, because there are too many nodes , I need to have more pixels so that I can zoom in and save it.

如果我放

plt.figure(figsize=(18,18))

在显示之前,没有出现节点和边.有什么问题,我该如何解决?

before showing, then the nodes and edges don't appear. What is the problem and How do I fix this?

推荐答案

绘图窗口使用屏幕分辨率,您可以缩放它而不会丢失信息.如果在显示figure之前放一个figure,则唯一应该发生的事情是将出现两个窗口(因为您已经有一个图形在工作).因此,如果您想自定义图形大小,只需在填充绘图之前进行一下操作即可:

The plot window uses screen resolution and you can zoom it without loss of information. If you put a figure just before showing it the only thing that should happen is that two windows will appear (because you already have one figure working). So if you want to customize figure size just do it before populating your plot:

import networkx as nx
import matplotlib.pyplot as plt

plt.figure(figsize=(18,18))

G = nx.gnm_random_graph(300,200)
graph_pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, graph_pos, node_size=10, node_color='blue', alpha=0.3)
nx.draw_networkx_edges(G, graph_pos)
nx.draw_networkx_labels(G, graph_pos, font_size=8, font_family='sans-serif')

plt.show()

要获得可以缩放并避免看到大像素的图像(此处的分辨率很重要),您可以:

To have an image (here resolution is important) that you can zoom and avoid seeing big pixels you can either:

plt.savefig("plot.png", dpi=1000)

,以非常大的dpi保存png.或者:

, saving a png with a very large dpi. Or:

plt.savefig("plot.pdf")

,这会将图另存为矢量图形(pdf). Matplotlib绘图窗口具有一个工具,可用于放大绘图的任何部分:

, which will save the plot as vector graphic (into pdf). Matplotlib plot window has a tool which allows you to zoom in to any part of the plot:

这篇关于使用NetworkX和Matplotlib的图形的高分辨率图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆