Search code examples
pythonmatplotlib2dhistogramcolorbar

Organising 3 dimensional data for Matplotlib 2D plot


I am analysing network graph connectedness, measuring in_degrees and out_degrees of all nodes.

I am seeking to show the results as shown in this video at 02:00

As you can see the lecturer has plotted in_degrees on the x-axis and out_degrees on the y-axis. The colour scale represents the number of nodes which have those x,y characteristics. His example is simple with a minimum of 0 and a maximum of 3 on each axis.

I have produced data in the format of

Counter({(0, 0): 7, (1, 3): 2, (19, 0): 2, (0, 2): 2, 
etc. 
(27, 42): 1, (25, 43): 1, (26, 36): 1, (23, 46): 1})

The data could run to 5,000 or so nodes. The counter object above is unsorted.

My example above takes the form

{(in_degree, out_degree): count, (in_degree, out_degree): count, etc. }

So, you can see there are 7 nodes with 0 in_degrees and 0 out_degrees, 2 nodes with 1 in_degree and 3 out degrees ..... and finally 1 node with 23 in_degrees and 46 out_degrees.

I am trying to work out how to get this into a state to plot it, using something like this example.

How should I arrange my data for plotting - and how do I work out bin sizes dynamically when I don't know the upper limits for x and y axes?

Any pointers, not necessarily solutions, would be welcomed please!


Solution

  • Here is an example to obtain a heat-map graph using the count data:

    %matplotlib inline
    import matplotlib.pylab as plt
    
    import numpy as np
    
    data = {(0, 0): 7, (1, 3): 2, (19, 0): 2, (0, 2): 2, 
            (3, 3): 2, (2, 3): 6, (8, 6): 2, (9, 4): 2, 
            (10, 12): 1, (15, 13): 1, (16, 16): 1, (13, 15): 1}
    
    # Create an empty array:
    max_x = max( xy[0] for xy in data.keys() ) 
    max_y = max( xy[1] for xy in data.keys() ) 
    
    count_grid = np.zeros((max_x+1, max_y+1))
    
    # Populate the array:
    for xy, count in data.items():
        x, y = xy
        count_grid[x, y] = count
    
    # Plot
    plt.pcolor(count_grid); plt.colorbar()
    plt.xlabel('x'); plt.ylabel('y'); plt.title('count');
    

    Which gives this graph:

    heatmap