Drawing a huge graph with networkX and matplotlib - python

I am drawing a graph with around 5K nodes in it using networkX and matplotlib. The GTK window by matplotlib has tools to zoom and visualise the graph.
Is there any way, I can save a magnified version for proper visualisation later?
import matplotlib.pyplot as plt
import networkx as nx
pos=nx.spring_layout(G) #G is my graph
nx.draw(G,pos,node_color='#A0CBE2',edge_color='#BB0000',width=2,edge_cmap=plt.cm.Blues,with_labels=True)
#plt.show()
plt.savefig("graph.png", dpi=500, facecolor='w', edgecolor='w',orientation='portrait', papertype=None, format=None,transparent=False, bbox_inches=None, pad_inches=0.1)

You have two easy options:
Up the DPI
plt.savefig("graph.png", dpi=1000)
(larger image file size)
Save as a PDF
plt.savefig("graph.pdf")
This is the best option, as the final graph is not rasterized. In theory, you should be able to zoom in indefinitely.

While not in GTK, you might want to check out NetworkX Viewer.

You may want to check this out:
plt.savefig("name.svg")
the quality is magnificent. although the dpi option is still alive.

Use this plt.savefig('name.svg') and then just upload the file to Figma -https://www.figma.com

Related

How can I make matplotlib plot rendering faster

I want to work with a scatter plot within a FigureCanvasQTAgg. The scatter plot may have 50,000 or more data points. The user wants to draw a polygon in the plot to select the data points within the polygon. I've realized that by setting points via mouse clicks and connect them with lines using Axis.plot(). When the user has set all points the polygon is drawn. Each time I add a new point I call FigureCanvasQTAgg.draw() to render the current version of the plot. This is slow, because the scatter plot has so much data.
Is there a way to make this faster?
Two possible solutions:
Don't show a scatter plot, but a hexbin plot instead.
Use blitting.
(In case someone wonders about the quality of this answer; mind that the questioner specifially asked for this kind of structure in the comments below the question.)
I can try to convert the scatter plot into an image using matplotlib to render it and display the image with imshow:
import matplotlib
matplotlib.use('QT4AGG')
import matplotlib.pyplot as plt
import Image # PIL
from io import BytesIO
from matplotlib import image
plt.scatter(xdata, ydata)
plt.axis('off')
plt.subplots_adjust(0, 0, 1, 1, 0, 0)
stream = BytesIO()
plt.savefig(stream, format='raw')
pilImage = Image.fromstring('RGBA',size=(640, 480), data = stream.getvalue())
plt.imshow(image.pil_to_array(pilImage))

Axes missing when plotting in matplotlib using vispy as backend

I have been watching the development of the python graphing library vispy with excitement. I just discovered that I can use vispy as a backend to matplotlib, which is great because I am plotting hundreds of thousands of points. I ran the example and I noticed there are no axes. I also ran the following code.
import vispy.mpl_plot as plt
plt.plot([1,2,3,4], [1,4,9,16], 'ro')
plt.show()
The axes were present until the plt.show() command, and they disappeared. I understand that the vispy project is under development, but plotting with axes would be extremely valuable to me, so I was wondering how I could do this. Also, less urgently, the border around the graph becomes huge. Is there a way to shrink the border?
The support for the matplotlib backend was dropped in version 0.5:
https://github.com/vispy/vispy/pull/1224

Matplotlib: poor resolution of PDF figures with hatching

When using Matplotlib to generate figures with hatching (e.g. pie, bar, bubble charts), I'm having some trouble getting decent resolution out of the PDF version of the figure. Saving as EPS is fine, but as soon as I use epstopdf or MPL's savefig(*.pdf), the hatching becomes pixellated and distored... the vector nature of the image appears to have been lost.
See minimal code below.
from matplotlib import pyplot as plt
# Define hatching styles
hatching = ["/", "o"]
fig, ax = plt.subplots()
wedges, texts = ax.pie([0.4, 0.6], colors=("SteelBlue", "Tomato"))
# Apply the hatching
for j, patch in enumerate(wedges): patch.set_hatch(hatching[j])
fig.savefig("hatchtest.pdf")
I've used Gimp to zoom in on a part of the plot to illustrate the difference...
Zoomed in on the EPS figure
Zoomed in on the PDF figure
As for system specific details, I'm using Ubuntu 13.04, Python 2.7.4 with MPL 1.2.1. I've tried different backends but nothing seems to resolve this. I'd ideally like to have nice vector images in EPS and PDF so that it's all journal-friendly. Any pointers would be much appreciated.
Just a problem with Evince PDF viewer. Viewing in Adobe Reader or printing the plot gives the desired result.

large graph visualization with python and networkx

I am having trouble with large graph visualization in python and networkx. The graph is wish to visualize is directed, and has an edge and vertex set size of 215,000 From the documenation (which is linked at the top page) it is clear that networkx supports plotting with matplotlib and GraphViz. In matplotlib and networkx the drawing is done as follows:
import
networkx as nx
import matplotlib.pyplot as plt
#Let g be a graph that I created
nx.draw(g)
I get a memory error after nx.draw(g), afterwards you would normally do plt.show() or plt.[some_function] to save the file in a format for efficient and so forth.
Next I tried GraphViz. From the wikipedia page the dot format is used for directed graphs and I created a dot file:
nx.write_dot(g, "g.dot")
This worked well and I had a dot file in my current directory that is 12 megabytes. Next I ran the dot program (part of graphviz to create a postscript file):
dot -Tps g.dot -o g.ps
This slows down my computer, runs for a few minutes and then display Killed in the terminal. So it never could execute... While reading the documentation for graphviz it seems that only undirected graphs were supported for large graph visualization.
Question:
With these two unsuccessful attempts can anyone show me how to visualize my large graph using python and networkx with about 215,000 vertices and 215,000 edges? I suspect as with Graphviz I will have to output into an intermediate format (although this shouldn't be that hard it won't be as easy as a builtin function) and then use another tool to read the intermediate format and then output a visualization.
So, I am looking for the following:
Output graph from networkx into an intermediate format
With new package/software/tool (ideally python-interactive) read intermediate format and visualize the large graph
If you need more information let me know!
from matplotlib import pylab
import networkx as nx
def save_graph(graph,file_name):
#initialze Figure
plt.figure(num=None, figsize=(20, 20), dpi=80)
plt.axis('off')
fig = plt.figure(1)
pos = nx.spring_layout(graph)
nx.draw_networkx_nodes(graph,pos)
nx.draw_networkx_edges(graph,pos)
nx.draw_networkx_labels(graph,pos)
cut = 1.00
xmax = cut * max(xx for xx, yy in pos.values())
ymax = cut * max(yy for xx, yy in pos.values())
plt.xlim(0, xmax)
plt.ylim(0, ymax)
plt.savefig(file_name,bbox_inches="tight")
pylab.close()
del fig
#Assuming that the graph g has nodes and edges entered
save_graph(g,"my_graph.pdf")
#it can also be saved in .svg, .png. or .ps formats
This answers your first issue.
Networkx does not have the facility to zoom into nodes. Use Gephi for this functionality.
Gephi accepts an edge list in CSV format and produces a visualization, where zooming can be done interactively.

Matplotlib to generate such circular plots?

does anyone know if it's possible to generate such plots in matplotlib?
the image is taken from the following page. If not, there another 'python' solution to generate such plots?
You want to use the packange networkx which will take care of the layout and plotting (via matplotlib if you want).

Categories

Resources