Also this year the Packt Publishing gives me the opportunity to review an interesting book about scientific computing in Python. The last year I had the pleasure to review Numpy 1.5 Beginner's Guide from Ivan Idris and I was surprised about the number of code examples and the straightforward approach to the explanation of the topics. Now I am waiting for Numpy Cookbook, which is from the same author and I can't wait to write the review. Stay tuned ;)
Thursday, December 13, 2012
Thursday, December 6, 2012
3D stem plot
A two-dimensional stem plot displays data as lines extending from a baseline along the x axis. In the following snippet we will see how to make a three-dimensional stem plot using the mplot3d toolkit. In this case we have that data is displayed as lines extending from the x-y plane along the z direction.Let's go with the code:
from numpy import linspace, sin, cos from pylab import figure, show from mpl_toolkits.mplot3d import Axes3D # generating some data x = linspace(-10,10,100); y = sin(x); z = cos(x); fig = figure() ax = Axes3D(fig) # plotting the stems for i in range(len(x)): ax.plot([x[i], x[i]], [y[i], y[i]], [0, z[i]], '--', linewidth=2, color='b', alpha=.5) # plotting a circle on the top of each stem ax.plot(x, y, z, 'o', markersize=8, markerfacecolor='none', color='b',label='ib') ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') show()And this is the result
Saturday, November 17, 2012
First steps with networkx
One of my favorite topics is the study of structures and, inspired by the presentation of Jacqueline Kazil and Dana Bauer at PyCon US, I started to use networkx in order to analyze some networks. This library provides a lot facilities for the creation, the visualization and the mining of structured data. So, I decided to write this post that shows the first steps to start with it. We will see how to load a network from the gml format and how to prune the network in order to visualize only the nodes with a high degree. In the following examples the coappearance network of characters in the novel Les Miserables, freely available here, will be used. In this network each node represents a character and the connection between two characters represent the coappearance in the same chapter.
Let's start with the snippets. We can load and visualize the network with the following code:
It's easy to see that the graph is not really helpful. Most of the details of the network are still hidden and it's impossible to understand which are the most important nodes. Let's plot an histogram of the number of connections per node:
Looking at this histogram we can see that only few characters have more than ten connections. Then, we decide to visualize only them:
Let's start with the snippets. We can load and visualize the network with the following code:
# read the graph (gml format) G = nx.read_gml('lesmiserables.gml',relabel=True) # drawing the full network figure(1) nx.draw_spring(G,node_size=0,edge_color='b',alpha=.2,font_size=10) show()This should be the result:
It's easy to see that the graph is not really helpful. Most of the details of the network are still hidden and it's impossible to understand which are the most important nodes. Let's plot an histogram of the number of connections per node:
# distribution of the degree figure(2) d = nx.degree(G) hist(d.values(),bins=15) show()The result should be as follows:
Looking at this histogram we can see that only few characters have more than ten connections. Then, we decide to visualize only them:
def trim_nodes(G,d): """ returns a copy of G without the nodes with a degree less than d """ Gt = G.copy() dn = nx.degree(Gt) for n in Gt.nodes(): if dn[n] <= d: Gt.remove_node(n) return Gt # drawing the network without # nodes with degree less than 10 Gt = trim_nodes(G,10) figure(3) nx.draw(Gt,node_size=0,node_color='w',edge_color='b',alpha=.2) show()In the graph below we can see the final result of the analysis. This time the graph makes us able to observe which are the most relevant characters and how they are related to each other according to their coappearance through the chapters.
Subscribe to:
Posts (Atom)