I have a list of edges.
(1,2),(1,3),(1,4),(1,5),(1,6),(2,4),(2,7),(3,4),(3,7),(4,5),(4,7),(5,6),(6,7)
How can I get an image of this graph?
It should be automatic, because there are over 9000(not kidding) those lists.
I have always used graphviz for this sort of stuff.
You can draw it with Python and networkx.
import networkx
import pylab
edges = [(1,2),(1,3),(1,4),(1,5),(1,6),(2,4),(2,7),(3,4),(3,7),(4,5),(4,7),(5,6),(6,7)]
G = networkx.Graph(data=edges)
networkx.draw(G)
pylab.show()
You should read pylab's documentation on how to save the graph as an image without using the GUI. You can use ast.literal_eval to parse the original lists. For example, if it stored as one graph on a line in a file, you can do:
with open('edges.txt') as f:
for line in f:
edges = list(ast.literal_eval(line))
# drawing goes here
Related
I am attempting to convert a rasterized line to a polyline. I have skeletonized the raster, but wish to export it as a shapefile (polyline feature) without resorting to ArcGIS. In ArcGIS there is a single tool 'raster to polyline' which completes this task. I've tried a few pythonic approaches, but they all seem to produce polygons rather than a single line feature as observed when running the skeletonizsation tool from skimage (below).
Any suggestions would be much appreciated.
The code I have up to the question raised above is posted below:
rasterClines = rasterpath + ClineRasterName
print(rasterClines)
raster = gdal.Open(rasterClines)
band = raster.GetRasterBand(1)
data = band.ReadAsArray()
final = morphology.skeletonize(data)
plt.figure(figsize=(15,15))
plt.imshow(final, cmap='gray')
#Method for exporting 'final' to .shp file
The plot looks correct, but I just can't find a method to export it.
I am using the latest networkx on Python3 with Linux Mint 17. I am using the pcalg method (https://github.com/keiichishima/pcalg) to build directed acyclic graphs from data. I checked to be sure that the graph has edges using the number_of_edges() function.
My code looks like this:
skel_1, sep_1 = alg.estimate_skeleton(ci.ci_test_dis, Mat_1, 0.1)
dag_1 = alg.estimate_cpdag(skel_1, sep_1)
num_edge1 = dag_1.number_of_edges()
print(num_edge1)
nx.draw_networkx(dag_1, arrows=True, edge_color='b')
plt.savefig("Target1.png")
plt.close()
When I execute this code, the nodes show up, but not the edges. Is this because I did not include the pos parameter?
I want to automate a network topology diagram using python. I'm new to python so please bear with me. After doing some research I found out that I can use python to create graphml files which can be read by yEd.
I'm learning how to use Networkx to create the graphml files. So far I'm able to create nodes, connect them and add labels to the nodes (these labels would be the hostnames). Now I need to know how I can add labels to the edges (these labels would be the interfaces). For example:
Topology example
If possible I would like to know how to add a custom image for every node (by default the shape is a square but I would like to use a router png file).
If it is not possible then it would be helpful to know how to edit the height and width of the shape and also disabling arrows.
I've reviewed the docs on networkx website but I haven't found how to do these changes directly to the graph object. The only way I've seen it done is when drawing the graph, for example using the following function: nx.draw_networkx_labels(G, pos, labels, font_size=15, arrows=False), but this is not what I need because this is not saved to the graphml file.
If someone can guide me through this it would be really helpful, I'm attaching my code:
import networkx as nx
import matplotlib
import matplotlib.pyplot as plt
g = nx.DiGraph()
g.add_node('Hostname_A')
g.add_node('Hostname_B')
g.add_node('Hostname_C')
g.add_node('Hostname_D')
g.add_edge('Hostname_A','Hostname_B')
g.add_edge('Hostname_A','Hostname_C')
g.add_edge('Hostname_B','Hostname_D')
g.add_edge('Hostname_B','Hostname_C')
for node in g.nodes():
g.node[node]['label'] = node
nx.readwrite.write_graphml(g, "graph.graphml")
This is the solution:
for edge in g.edges():
g.edges[edge]['source'] = 'int gi0/0/0'
g.edges[edge]['destination'] = 'int gi0/0/1'
After exporting a .dot file using scikit-learn's handy export_graphviz function.
I am trying to render the dot file using Graphviz into a cell in my Jupyter Notebook:
import graphviz
from IPython.display import display
with open("tree_1.dot") as f:
dot_graph = f.read()
display(graphviz.Source(dot_graph))
However the out[ ] is just an empty cell.
I am using graphviz 0.5 (pip then conda installed), iPython 5.1, and Python 3.5
The dot file looks correct here are the first characters:
digraph Tree {\nnode [shape=box, style="filled", color=
iPython display seems to work for other objects including Matplotlib plots and Pandas dataframes.
I should note the example on Graphviz' site also doesn't work.
It's possible that since you posted this, changes were made so you might want to update your libraries if that's possible.
The versions of relevance here I used are:
Python 2.7.10
IPython 5.1.0
graphviz 0.7.1
If you have a well formed .dot file, you can display it to the jupyter out[.] cell by the following:
import graphviz
with open("tree_1.dot") as f:
dot_graph = f.read()
# remove the display(...)
graphviz.Source(dot_graph)
this solution allows you to insert DOT text directly (without saving it to file first)
# convert a DOT source into graph directly
import graphviz
from IPython.display import display
source= '''\
digraph sample {
A[label="AL"]
B[label="BL"]
C[label="CL"]
A->B
B->C
B->D
D->C
C->A
}
'''
print (source)
gvz=graphviz.Source(source)
# produce PDF
#gvz.view()
print (gvz.source)
display(gvz)
Try to use pydotplus.
import pydotplus
by (1.1) Importing the .dot from outside
pydot_graph = pydotplus.graph_from_dot_file("clf.dot")
or (1.2) Directly using the .export_graphviz output
dt = tree.DecisionTreeClassifier()
dt = clf.fit(x,y)
dt_graphviz = tree.export_graphviz(dt, out_file = None)
pydot_graph = pydotplus.graph_from_dot_data(dt_graphviz)
(2.) and than display the pyplot graph using
from IPython.display import Image
Image(pydot_graph.create_png())
try to reinstall graphviz
conda remove graphviz
conda install python-graphviz
graphviz.Source(dot_graph).view()
graphviz.Source(dot_graph).view()
To make a plot, I have written my code in the following fashion:
from pylab import *
x = [1,2,3]
y = [1,2,3]
matplotlib.pyplot.scatter(x,y,label='Blah')
matplotlib.pyplot.legend(title='Title')
matplotlib.pyplot.show()
I want to change the font size of the legend title. The way to go about this is to get the legend object and then change the title that way (e.g., How to set font size of Matplotlib axis Legend?)
Instead of rewriting all my code using ax.XXX, figure.XXX, etc, is there any way to get at the legend object from the code I have written, and then go from there?
That is to say, how do I define
Legend
from my original piece of code, such that
Title = Legend.get_title()
Title.set_fontsize(30)
would get at the title object and then allow me to play with .get_title()?
I think I'm on the verge of a eureka moment regarding object-orientated languages. I have a feeling a good answer will give me that eureka moment!
cheers,
Ged
First, in your code you should stick to using either from pylab import * and then use the imported methods directly, or import matplotlib.pyplot as plt and then plt.* instead of matplotlib.pyplot.*. Both these are "conventions" when it comes to working with matplotlib. The latter (i.e. pyplot) is generally preferred for scripting, as pylab is mainly used for interactive plotting.
To better understand the difference between pylab and pyplot see the matplotlib FAQ.
Over to the problem at hand; to "get" an object in Python, simply assign the object to a variable.
from pylab import *
x = [1,2,3]
y = [1,2,3]
scatter(x,y,label='Blah')
# Assign the Legend object to a variable leg
leg = legend(title='Title')
leg_title = leg.get_title()
leg_title.set_fontsize(30)
# Optionally you can use the one-liner
#legend(title='Title').get_title().set_fontsize(30)
show()
Visual comparison (rightmost subplot produced with the above code):