Matplotlib contour plot does not extract exact contours

Viewed 65

I am having difficulties extracting the exact contour of the following data: (you can already see the contour by just looking at the data)

data = np.array(
      [[ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277]])

If I plot it:

plt.imshow(data)

enter image description here

However, when I try to extract the contours using:

plt.contour(data, levels = np.unique(data))

enter image description here

As you can see the contour does not follow the sharp corner of the actual data. If I overlay both plots:

enter image description here

Here is the complete code:

import numpy as np
import matplotlib.pyplot as plt

data = np.array([[ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277]])

plt.imshow(data)
plt.show()
plt.contour(data, levels=np.unique(data), cmap="jet")
plt.colorbar()
2 Answers

The contours are drawn using a marching squares algorithm to compute contour locations, it interpolates between the grid points.

Maybe you're looking for the discrete region boundaries: these can be retrieved like this:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

data = np.array([[ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277]])


def region_borders(data, value, color, **kwargs):
    v = np.argwhere(np.diff((data == value).T, axis=0))
    vlines = np.array(list(zip(v + [.5, -.5], v + [.5, .5])))
    
    h = np.argwhere(np.diff((data == value).T, axis=1))
    hlines = np.array(list(zip(h + [-.5, .5], h + [.5, .5])))
    
    if len(vlines) and len(hlines):
        lines = np.vstack((vlines, hlines))
    elif len(vlines):
        lines = vlines
    else:
        lines = hlines
    return mpl.collections.LineCollection(lines, colors=color, **kwargs)


contours = np.unique(data)

fig, ax = plt.subplots(ncols=len(contours)+1, sharex=True, sharey=True, layout='constrained')
im = ax[0].matshow(data, cmap='jet', aspect='auto')
fig.colorbar(im, ax=ax[-1])

norm = mpl.colors.Normalize(data.min(), data.max())
for i, value in enumerate(contours, 1):
    ax[i].add_collection(region_borders(data, value, mpl.cm.jet(norm(value)), lw=2))
    ax[i].set_title(value)

enter image description here

Here is how I am solving it (there is still a slight offset between contour and color)

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

import cv2

def change_into_int(data):
    unique = np.unique(data)
    int_data = np.zeros_like(data)
    for i, unique_value in enumerate(unique):
        int_data[data == unique_value] = i+1
    
    return int_data.astype(np.uint8)


def find_contours(int_data):
    
    # For every "color" in the dataset find the contour
    # by eliminating all the other colors one after each other and finding the 
    # individual contours
    contours = []
    
    
    for color in np.unique(int_data):
      
        # Create temporary canvas, and 
        # set all entries equal to 1 that have the same postion as the 
        # original color
        
        mask = np.zeros_like(int_data)
        mask[int_data == color] = 1 # white object on black background
        
    
        c, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        contours.append(c)
    
    return contours

data = np.array([
       [ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  1.        ,  1.        ],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277],
       [ 1.46184395,  1.46184395,  1.46184395,  4.24552277,  4.24552277]])



## Main
int_data = change_into_int(data)
contours = find_contours(int_data)

## Draw 
canvas = np.zeros_like(int_data)

for contour in contours:
    coords = np.reshape(contour[0].flatten(), (-1,2))
    x = list(coords[:,0])
    x.append(x[0])
    
    y = list(coords[:,1])
    y.append(y[0])
    plt.plot(x,y)


plt.imshow(data, origin="lower")

enter image description here

Related