I am trying to remove the border of node vertices in igraph, but I can't find a way. I found many references on google that in R, you can change this attribute:
vertex.frame.color
but in igraph for python, I could not find a way. Any ideas?
Thanks
If you look into the documentation, it's done in basically the same way. Example:
import igraph
g = igraph.Graph([(0,1), (0,2), (2,1)])
igraph.plot(g, vertex_frame_color='red')
Related
I am trying to learn if in Python or R, there exist within the graph-theory related modules features that would enable one to start from a degree distribution (or expressed as a sequence once we set the number of vertices), and generate (random) graphs that satisfy the prescribed degree sequence.
As an example, we might be given the following distribution: p=(0.179,0.49,0.34) which are the probabilities of degree values 1,2 and 3 respectively. So we can set the number of vertices, n=500, map p to a degree sequence deseq list: filled with 0.179*n times of 1, and so on for the rest.
Any pointers towards previously discussed cases for such problems or library suggestions would be very helpful.
Here's an attempt to answer my own question after having learned how to use igraph in R and Python for generating the desired type of graphs.
In R:
For the purposes of this example, let's suppose the following degree sequence: total nodes n=20, 5,10 and 5 nodes with degrees 1,2, and 3 respectively. We create the degree sequence using c() and rep(). Then we'll use sample_degseq() from igraph to generate a graph corresponding to the above degree sequence. Then we'll draw its degree histogram to sanity check.
First install and call the igraph module in the R console using:
install.packages("igraph")
library(igraph)
Now we can proceed as described:
degreels <- c(rep(1,5),rep(2,10),rep(3,5))
graph <- sample_degseq(degreels, method="simple")
degreehist <- hist(degree(graph))
is.connected(graph)
In Python:
Now let's do the exact same thing with the igraph module in Python:
To install see here.
import igraph as ig
import matplotlib.pyplot as plt
degcounts = [5,10,5]
degreels = []
for i in range(len(degcounts)):
degreels += degcounts[i]*[i+1]
graph = ig.GraphBase.Degree_Sequence(degreels,method="simple")
plt.hist(graph.degree())
plt.show()
and the obtained histogram:
I don't know how much have you already researched on the topic but there is a pretty wellknown package in R and module in Python called igraph. It might have what you seek for.
For Python, the networkx package also provides what you are looking for.
In particular, the functionalities regarding random graphs from degree sequences (e.g. configuration_model with the idea from #Alex_6's comment) might be helpful.
I need to calculate the amount of two oval intersects in a python program.
I know in shaply there is a function that return true if two object has intersects. As like as this:
from shapely.geometry import Polygon
p1=Polygon([(0,0),(1,1),(1,0)])
p2=Polygon([(0,1),(1,0),(1,1)])
print p1.intersects(p2)
is there any library or function That help me?
Thanks.
Is this what you are looking for? (the polygon that results from the intersection)
x = p1.intersection(p2)
x.area
Find more information in the documentation here
I'm wondering how, given contour lines generate trough the contour() function from Matplotlib, one can iterate to each level to get its vertices. I know that I can iterate over the paths with this code:
cs = plt.contour(x, y, (f - g))
for collection in cs.collections:
paths = collection.get_paths()
for path in paths:
print(path.vertices.shape)
plt.show()
However, how could I find the level of each path, or directly iterate over each level of a contour?
Thank you.
This is maybe very clear to you, but I would like to highlight that care is needed with the proposed code.
See also:
https://github.com/matplotlib/matplotlib/issues/367
Each path may be just an array of vertices corresponding to a single open or closed polygon, which is what most people expect, but a path may also correspond to 2 or more polygons if the member codes is set to indicate at which indices the polygons start. Naive use of the paths may not be what is wanted; often the use of path.to_polygons() is better.
In short, this code will work as expected on most simple examples but might cause problems for complex cases. A better alternative is:
cs = plt.contour(x, y, (f - g))
for collection in cs.collections:
for path in collection.get_paths():
print path.to_polygons()
I would like to find all vertices (vertex id's) sharing the same edge attribute (so there can be tons of vertices like this) by using Igraph. This would be very convenient when I want to find all "villages" (the vertices of my graph) on a "road", let's say "Route 69" (an edge attribute).
Is there a simple way in Igraph to do this? Maybe I've overcomplicated it.
Actually what I need is the opposite of: g.es.select(_within=g.vs[2:5]) or
>>> men = g.vs.select(gender="m")
>>> women = g.vs.select(gender="f")
>>> g.es.select(_between=(men, women))
because I know the edge attribute but I don't know the vertices.
I will select the edge and hope that it will return all related vertices.
Maybe I'm only tired now, but I don't find my way around this problem. I appreciate if somebody helps me out with the right way. Or maybe there is a method I miss in tutorial and documentation. It smells like there is a very simple method to this. Thank you in advance for any advice!
First, select all the edges on Route 69:
edges = g.es.select(name="Route69")
Then iterate through the selected edges and collect the endpoints of the vertices:
vertices = set()
for edge in edges:
vertices.update(edge.tuple)
This will give you a set containing the vertex IDs of all the vertices that are incident on at least one edge with name Route69. If you need a VertexSeq, you can simply do this:
vertices = g.vs[sorted(vertices)]
You should be able to do the following:
r69edges = g.es.select(name_eq='Route69') #Select all edges where name=='Route69'
sg = r69edges.subgraph() #Create a subgraph containing only the selected edges (and attached vertices)
village_verts = sg.vs.select(name_eq='villages') #Select the vertices where name=='villages'
This assumes that 'villages' and 'Route69' are stored in an attribute called 'name' on the vertices and edges... Adjust appropriately to match your attributes.
Of course - you can squash this all into one line if you want:
village_verts = g.es.select(name_eq='Route69').subgraph().vs.select(name_eq='villages')
Not sure if this is the most efficient way (though I'm not seeing any shortcuts in the documentation), but it should get you what you're after.
I have a Graph in Python like this one:
# Each element is a tuple with coordinates (x,y,z).
# The index is the id of the vertex
vertexList = [(0,0,0),(1,0,0),(1,1,0),(0,1,0),
(0,0,1),(1,0,1),(1,1,1),(0,1,1)]
# Each element is a tuple with the vertex-ids and a weight (vertexId1, vertexId2, weight)
edgeList = [(0,1,1), (1,2,1), (2,3,1), (3,0,1),
(0,4,1),
(4,5,1), (5,6,1), (6,7,1), (7,4,1)]
graph = (vertexList, edgeList)
This is a small example. The application I wrote uses graphs with about 100 vertexes and 300 edges.
I would like to visualize this with python, preferably with a library which is available for Ubuntu. It would be great if it were possible to move the graph in the 3D-visualisation.
What I've done so far
At the moment I use UBIGRAPH. The visualization and interaction is very good, but I can't specify coordinates for the vertexes:
def visulizeGraph(Graph):
vertexList, edgeList = Graph
server_url = 'http://127.0.0.1:20738/RPC2'
server = xmlrpclib.Server(server_url)
G = server.ubigraph;
G.clear()
for identifier, vertex in enumerate(vertexList):
G.new_vertex_w_id(identifier)
for vertex1, vertex2, weight in edgeList:
x1, y1, z1 = vertexList[vertex1]
x2, y2, z2 = vertexList[vertex2]
G.new_edge(vertex1, vertex2)
matplot
I've found matplotlib, but its very big. I didn't find an example which does what I like, but I might have missed it. Its available for Ubuntu.
vtk
The same problem as with matplot. If you could give me some working examples it might be the best solution.
When you looked at matplotlib, did you see mplot3d? This may be what you require.
MPlot3D
If not, I'm sorry I probably can't help any more than that.
I've not used this myself yet (beyond running example scripts) but mayavi2 looks promising. It comes bundled with the enthought python distribution.
Also, a little of topic as it's not in your question, but networkx is pretty nice if your working with graphs in python.
Hope this helps a bit.