Matplotlib: `pcolormesh.get_array ()` returns a flattened array - how to return 2D data?

I am trying to get data values ​​along a string (like this hint ). This example uses imshow() , but currently I'm using pcolormesh() to build.

I found that the get_array() function to capture the constructed data from pcolormesh() returns a 1-D, flattened array of my data instead of the original (or truncated) two-dimensional data.

For example:

 D = np.genfromtxt(DataFilePath, skip_header=4, delimiter=',', unpack=True) print( D.shape ) : (500, 500) ...more code... img = ax[0].pcolormesh( np.arange( len(D[0,:]) ), np.arange(len(D[:,0])), D) >>> D : array([[ 42.38, 41.93, 41.92, ..., 41.73, 41.74, 41.51], [ 41.88, 42.24, 42.21, ..., 41.88, 41.67, 41.64], [ 42.4 , 41.47, 41.49, ..., 41.92, 42.07, 41.49], ..., [ 44.24, 44.14, 44.17, ..., 40.2 , 40.68, 40.67], [ 44.59, 44.24, 44.3 , ..., 40.91, 40.92, 40.95], [ 44.2 , 44.27, 44.27, ..., 40.82, 40.91, 40.94]]) >>> img.get_array() : array([ 42.38, 41.93, 41.92, ..., 40.85, 40.91, 40.92]) 

Since I am trying to capture user clicks on the plot and then redo using data values ​​by clicking (for example, this hint ), I would like to use a function / class that will not have global access to the source data , but has access to the img object.

Any idea how to get 2D data from pcolormesh() using only img (QuadMesh) object? It does not even have length / shape values ​​x / y, for me it is possible to recover data from 1-D get_array() .

Thank!

+1
python numpy matplotlib
Jan 17 '16 at 15:59
source share
2 answers

The shape of the array, stored in private attributes, _meshWidth and _meshHeight . However, since these attributes are not part of the public API, it would be better to keep the form of the source data than to rely on them if possible.

 import matplotlib.pyplot as plt import numpy as np D = np.random.uniform(0, 100, size=(5, 5)) fig, ax = plt.subplots() h, w = D.shape img = ax.pcolormesh( np.arange(h+1), np.arange(w+1), D) D2 = img.get_array().reshape(img._meshWidth, img._meshHeight) assert np.array_equal(D, D2) 

Note also that if you want to restore the original D array, then the np.arange(h+1) , np.arange(w+1) coordinate arrays must have a length greater than the D shape. Otherwise, img.get_array() returns an array of form (499, 499) when D has form (500, 500) .

+3
Jan 17 '16 at 18:44
source share

Yes, it activates the input:

https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes/_axes.py

  # convert to one dimensional arrays C = C.ravel() X = X.ravel() Y = Y.ravel() 

If you know the desired 2d form, you can unravel with a simple reshape call.

If the result should have the same form as D , use:

 img.get_array().reshape(D.shape) 

If the size of the bitmap C can change, this will not work.




If I create an array D that is (10,20) and build it

 img = pyplot.pcolormesh(D) 

img._A - (200), the array returned by img.get_array() .

 img._meshHeight, img._meshWidth # 10, 20 

Thus, the array can be changed as follows:

 img._A.reshape(img._meshHeight, img._meshWidth) 

img._coordinates is an array (11,21,2), the coordinates in the x and y direction plus one point. This way you can get C conversion information from _coordinates . I do not see any public API method for extracting these attributes, but this does not stop the "serious" Python programmers. In this test case, it generated coordinates from form D

This Quadmesh was created using

  coords = np.zeros(((Nx * Ny), 2), dtype=float) coords[:, 0] = X coords[:, 1] = Y collection = QuadMesh( Nx - 1, Ny - 1, coords, ...) .... collection.set_array(C) 

A search for get_array in the matplotlib github repository matplotlib not receive many hits.




I dug a little into the pcolor code. It returns PolyCollections img, not a square. It contains information for drawing a collection of quadrangles.

For example, in my 10x20 test case, img._paths is a list of 200 Path objects

 In [486]: img1._paths[0] Out[486]: Path(array([[ 0., 0.], [ 0., 1.], [ 1., 1.], [ 1., 0.], [ 0., 0.], [ 0., 0.]]), array([ 1, 2, 2, 2, 2, 79], dtype=uint8)) 

It has five coordinate pairs, xy points, necessary to draw the border of the square, which will have a color value corresponding to C[0] (in an uninhibited form).

So, all the X Y grid information is now encoded in these Path objects. Instead of laying a grid, it displays 200 colored squares (quads). The PolyCollections code PolyCollections not assume that the squares are in any order or even touch each other. The big picture was replaced with a bunch of independent small images.

Perhaps you can collect these squares into a grid, find matching vertices, etc. But it will be a lot of work.

+2
Jan 17 '16 at 18:16
source share



All Articles