High Resolution Image of a Graph using NetworkX and Matplotlib

2024/10/13 23:20:59

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 pltG = 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.

If I put

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?

Answer

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 pltplt.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)

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

plt.savefig("plot.pdf")

, 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:

Zoom tool in matplotlib plot

https://en.xdnf.cn/q/69482.html

Related Q&A

how to read an outputted fortran binary NxNxN matrix into Python

I wrote out a matrix in Fortran as follows:real(kind=kind(0.0d0)), dimension(256,256,256) :: dense[...CALCULATION...]inquire(iolength=reclen)dense open(unit=8,file=fname,& form=unformatted,access=d…

python argparse subcommand with dependency and conflict

I want to use argparse to build a tool with subcommand. The possible syntax could be/tool.py download --from 1234 --interval 60 /tool.py download --build 1432 /tool.py clean --numbers 10So I want to us…

Running django project without django installation

I have developed a project with Django framework(python and mysql DB) in Linux OS(Ubuntu 12.04), I want to run this project in localhost in another machine with Linux(Ubuntu 12.04) without installing D…

redefine __and__ operator

Why I cant redefine the __and__ operator?class Cut(object):def __init__(self, cut):self.cut = cutdef __and__(self, other):return Cut("(" + self.cut + ") && (" + other.cut +…

How to find class of bound method during class construction in Python 3.1?

i want to write a decorator that enables methods of classes to become visible to other parties; the problem i am describing is, however, independent of that detail. the code will look roughly like this…

Multi-Threaded NLP with Spacy pipe

Im trying to apply Spacy NLP (Natural Language Processing) pipline to a big text file like Wikipedia Dump. Here is my code based on Spacys documentation example:from spacy.en import Englishinput = open…

Django Tastypie throws a maximum recursion depth exceeded when full=True on reverse relation.

I get a maximum recursion depth exceeded if a run the code below: from tastypie import fields, utils from tastypie.resources import ModelResource from core.models import Project, Clientclass ClientReso…

Adding a colorbar to two subplots with equal aspect ratios

Im trying to add a colorbar to a plot consisting of two subplots with equal aspect ratios, i.e. with set_aspect(equal):The code used to create this plot can be found in this IPython notebook.The image …

Why is C++ much faster than python with boost?

My goal is to write a small library for spectral finite elements in Python and to that purpose I tried extending python with a C++ library using Boost, with the hope that it would make my code faster. …

pandas: How to get .to_string() method to align column headers with column values?

This has been stumping me for a while and I feel like there has to be a solution since printing a dataframe always aligns the columns headers with their respective values.example:df = pd.DataFrame({Fir…