Draw graph in NetworkX - python

I'm trying to draw any graph in NetworkX, but get nothing, not even errors:
import networkx as nx
import matplotlib.pyplot as plt
g1=nx.petersen_graph()
nx.draw(g1)

Add to the end:
plt.show()
import networkx as nx
import matplotlib.pyplot as plt
g1 = nx.petersen_graph()
nx.draw(g1)
plt.show()
When run from an interactive shell where plt.ion() has been called, the plt.show() is not needed. This is probably why it is omitted in a lot of examples.
If you run these commands from a script (where plt.ion() has not been called), the plt.show() is needed. plt.ion() is okay for interactive sessions, but is not recommended for scripts.

in ipython notebook, just type in magic
%matplotlib inline
or
%matplotlib notebook

You can easily plot with networkx graphs using jupyter notebook. See first example.
OR, you can use Bokeh to plot graphs, which adds useful features.
The package holoviews makes it even simpler to plot a graphs with bokeh. It adds features like automatic highlighting and show of labels while hovering over nodes. However, editing colors etc. seems to be an issue.
%pylab inline
# `pylab notebook` # for interactive plots
import pandas as pd
import networkx as nx
import holoviews as hv
G=nx.Graph()
ndxs = [1,2,3,4]
G.add_nodes_from(ndxs)
G.add_weighted_edges_from( [(1,2,0), (1,3,1) , (1,4,-1) , (2,4,1) , (2,3,-1), (3,4,10) ] )
nx.draw(G, nx.spring_layout(G, random_state=100))
And here the example with bokeh and holoview:
hv.extension('bokeh')
%opts Graph [width=400 height=400]
padding = dict(x=(-1.1, 1.1), y=(-1.1, 1.1))
hv.Graph.from_networkx(G, nx.layout.spring_layout).redim.range(**padding)
You should give it a try and plot it in your notebook to see the difference.

It works fine by adding:
import matplotlib.pyplot as plt
plt.show()
to your code. mine worked fine.

Related

How could i fix error when i am drawing the network

errors nx.draw
I coded python for drawing network. It shown errors as this picture.
Please help me.
All you have to do is fix the last line to plt.show instead of plot.show:
import networkx as nx
from matplotlib import pyplot as plt
g1 = nx.petersen_graph()
nx.draw(g1)
plt.show()

How to prevent an osmnx figure from being shown in Python (jupyter)

Using matplotlib we can prevent a figure from being shown using the close function
import matplotlib.pyplot as plt
a = range(0,5)
b = range(0,10,2)
plt.plot(a,b, 'r-*')
plt.close()
Because osmnx uses matplotlib in background, I though the close function could have been used in the same way
import matplotlib.pyplot as plt
import osmnx as ox
graph = ox.graph_from_bbox(41.97278, 41.97614, -87.73993, -87.73755, network_type='drive')
ox.plot_graph(graph,
bbox=[41.97278, 41.97614, -87.73993, -87.73755],
)
plt.close()
Unfortunately the figure is still displayed.
Is there a way to prevent the figure from being shown ?
Thanks in advance.
Did you read the OSMnx documentation for that function?
show (bool) – if True, call pyplot.show() to show the figure
close (bool) – if True, call pyplot.close() to close the figure
The use of these parameters are also demonstrated in the OSMnx usage examples.
import osmnx as ox
bbox = 41.97278, 41.97614, -87.73993, -87.73755
G = ox.graph_from_bbox(*bbox, network_type='drive')
fig, ax = ox.plot_graph(G, bbox=bbox, show=False, close=True)

Figure not displayed with matplotlib.use('Agg')

I work with matplotlib. When I add the following lines, the figure is not displayed.
import matplotlib
matplotlib.use('Agg')
here is my code :
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plot
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plot.figure(figsize=(12,9))
def convert_sin_cos(x):
fft_axes = fig.add_subplot(331)
y = np.cos(x)
fft_axes.plot(x,y,'g*')
for i in range(3):
fft_axes = fig.add_subplot(332)
x=np.linspace(0,10,100)
fft_axes.plot(x,i*np.sin(x),'r+')
plot.pause(0.1)
convert_sin_cos(x)
Thanks
That's the idea!
When I run your code, the console says:
matplotlibAgg.py:15: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
How can it be useful? When you're running matplotlib code in a terminal with no windowing system, for example: a cluster (running the same code with different inputs, getting lot of results and without the need to move the data I can plot whatever I need).

Jupyter NoteBook: draw a graph on the Notebook instead of save the file to the disk

I have the following code:
Is it possible to print the graph just inside the Notebook instead of saving as a file? Thanks!
If you are looking to plot the plots inline inside your jupiter-notebook:
import matplotlib.pyplot as plt
%matplotlib inline
Yes, use matplotlib:
import matplotlib.pyplot as plt
nx.draw(G)
plt.show()
source
Use draw in the following manner :
draw(G, show='ipynb')
If you want all graphs to be drawn inline, then you can set a global parameter.
from nxpd import nxpdParams
nxpdParams['show'] = 'ipynb'
draw(G)
But im not sure this will work with Jupyter, this will work for ipython though.

How to show node labels when using matplotlib.pyplot?

I'm using Python to conduct social network analysis, very simple kind, and as a newbie (to both SNA and Python).
When drawing a graph using Terminal on my mac, I've tried every method I can but still can only draw nodes and edges, but no label of nodes in or beside them.
What scripts should I use to make the labels visible?
>>> import networkx as nx
>>> import networkx.generators.small as gs
>>> import matplotlib.pyplot as plt
>>> g = gs.krackhardt_kite_graph()
>>> nx.draw(g)
>>> plt.show()
EdChum gave a good answer. Another option which will by default not show the axes and produces a graph that takes up slightly more of the figure is to use nx.draw but give it the argument with_labels = True. (for nx.draw, you need to set with_labels to True, but for nx.draw_networkx it defaults to True).
import networkx as nx
import networkx.generators.small as gs
import matplotlib.pyplot as plt
g = gs.krackhardt_kite_graph()
nx.draw(g,with_labels=True)
plt.savefig('tmp.png')
Be aware that there is a bug such that sometimes plt.show() will not show the labels. From what I've been able to tell, it's not in networkx, but rather has something to do with the rendering. It saves fine, so I haven't worried about following up on it in detail. It shows up for me using ipython on a macbook. Not sure what other systems it's on. More detail at pylab/networkx; no node labels displayed after update
Try using draw_networkx:
import networkx as nx
import networkx.generators.small as gs
import matplotlib.pyplot as plt
g = gs.krackhardt_kite_graph()
nx.draw_networkx(g)
plt.show()
This results in:
with_labels is by default True so not necessary to specify

Categories

Resources