datacursormode for networkx - python

I have plotted a large graph with networkx and want to see the names of each vertex, unfortunately there are too many to read easily. Looking around I have found implementations of datacursormode in matplotlib but have been unable to make them work as networkx.draw does not return objects. Here's the original question
Is there a matplotlib equivalent of MATLAB's datacursormode?
Many Thanks
edit: I can get it to work by editting http://www.scipy.org/Cookbook/Matplotlib/Interactive_Plotting but would ideally have some idea which vertex I selected, so that I can change the colour of adjacent edges.

You can usually retrieve the artist in question using the following command in the callback function:
thisline = event.artist
And from there check things like labels (thisline.get_label()) or whatever other artist properties are of interest to you.
This, however, assumes that have some labeling system in place for the vertices already, or another way of determining which virtex is which. If you don't, that's another story altogether.

Related

How to make a plot that connect its points to its closest neighbors?

I am working on a code to project a given object onto a plane.
The code works fine (at least it seems like it) in achieving that purpose, the only issue I'm having is in plotting my results.
In the image below, for instance, I'm plotting the projection of a parallelepiped (its edges, to be more precise) in a plane of my choice.
I would like to make a plot where each point is connected to its closest neighbor. I'm not very confident that this approach would get the job done, but I think it would be worth the shot.
Different ideas to get there are also welcome!
Any thoughts?
Thanks in advance.
Note: I also tried using a solid line style when plotting as opposed to the pixel marker style, but the result I got was not quite what I expected to say the least:
When telling matplotlib to plot a sequence of points and join them with a line, it creates a straight line between two adjacent points in your input data. To create several lines, it's often easier to split your plot command into several ones. An alternative is to arrange your points such that they form the edges you want, but that would be much more complicated in your case.
As discussed in the comments, separating each edge into its own separate plot command worked for your case.

Using Matplotlib in Python, how to get a list of all drawn plt.scatter()?

When using matplotlib, you can easily access whatever curves you have drawn using plt.plot() by looking in the ax.lines list and remove n-th curve like so: del ax.lines[n].
Does the equivalent exist for plt.scatter()? After drawing several scatters the ax.lines is still empty so there must be another list somewhere.
For context, I am building a measurement interface using Qt Designer where the user might want to discard previous curves, so being able to choose via such a list which one is deleted (instead of clearing everything) is good.
Thanks in advance!
For such problems you may first just print out the return type of the object you deal with.
scatter = plt.scatter(x,y)
print(type(scatter))
This prints <class 'matplotlib.collections.PathCollection'>. From the name of the object we might deduce that the scatter is part of the axes' collections,
ax.collections
Indeed,
print(scatter)
print(plt.gca().collections[0])
print the same object.

Drawing clustered graphs in Python

I already have a way of clustering my graph, so the process of clustering isn't the issue here. What I want to do is, once we have all the nodes clustered - to draw the clustered graph in Python, something like this:
I looked into networkx, igraph and graph-tool, but they seem to do the clustering, but not the drawing. Any ideas and propositions of what library should I use for drawing the already clustered graph, which will minimize the number of crossing links?
Take a look at GraphViz
http://www.graphviz.org/Gallery/directed/cluster.html
There's a Python binding for that, but I have to say I always create the text files directly as they're easy enough to write. Don't be fooled by the plain-looking examples, every aspect of your graph is highly customizable and you can make some pretty nifty graph visualizations with it. Not sure about nested clusters though, never tried that out.

Best way to create a 2D Contour Map with Python

I am trying to create a 2D Contour Map in Python that looks like this:
In this case, it is a map of chemical concentration for a number of points on the map. But for the sake of simplicity, we could just say it's elevation.
I am given the map, in this case 562 by 404px. I am given a number of X & Y coordinates with the given value at that point. I am not given enough points to smoothly connect the line, and sometimes very few data points to draw from. It's my understanding that Spline plots should be used to smoothly connect the points.
I see that there are a number of libraries out there for Python which assist in creation of the contour maps similar to this.
Matplotlib's Pyplot Contour looks promising.
Numpy also looks to have some potential
But to me, I don't see a clear winner. I'm not really sure where to start, being new to this programming graphical data such as this.
So my question really is, what's the best library to use? Simpler would be preferred. Any insight you could provide that would help get me started the proper way would be fantastic.
Thank you.
In the numpy example that you show, the author is actually using Matplotlib. While there are several plotting libraries, Matplotlib is the most popular for simple 2D plots like this. I'd probably use that unless there is a compelling reason not to.
A general strategy would be to try to find something that looks like what you want in the Matplotlib example gallery and then modify the source code. Another good source of high quality Matplotlib examples that I like is:
http://astroml.github.com/book_figures/
Numpy is actually a N-dimensional array object, not a plotting package.
You don't need every pixel with data. Simply mask your data array. Matplotlib will automatically plot the area that it can and leave other area blank.
I was having this same question. I found that matplotlib has interpolation which can be used to smoothly connect discrete X-Y points.
See the following docs for what helped me through:
Matplotlib's matplotlib.tri.LinearTriInterpolator docs.
Matplotlib's Contour Plot of Irregularly Spaced Data example
How I used the above resources loading x, y, z points in from a CSV to make a topomap end-to-end

Interactive 2D plotting in python

I'm trying to write a program that creates a 2D grid in which the user chooses some edges between the points in the grid. Then the program dynamically manipulate those edges(eg. flip them, connect them, ...) till it converges to a particular shape.
Now my question is there any particular module that suits this kind of interactive plotting.
So far I've looked into PyQwt, GuiQwt, and Chaco but cannot figure out which one is more applicable for my program. It be great if someone could compare and contrast them, or suggest new modules.
Thanks
Have you tried PyGame or SFML?
Both support 'setting pixels' on a canvas.

Categories