python-3.xmatplotlibmplcursors

show coordinates of an image stored in an array using mathplotlib


    from nptdms import TdmsFile as td  
    from matplotlib import pyplot as plt
    import numpy as np
    import skimage.color
    import skimage.filters
    import mplcursors
    from skimage.feature import corner_harris,corner_peaks
    
    file = 'sample.tdms' 
    with td.open(file) as tdms_file:
     img = tdms_file.as_dataframe()
    cropped_list = []
    sel=cropped_list.append(img.iloc[700:1250,450:1550:])
coords=corner_peaks(corner_harris(sel),min_distance=10,threshold_rel=0.02)
    fig, ax = plt.subplots()
    ax.imshow(sel, cmap='gray')
    plotted_points =ax.plot(coords[:, 1], coords[:, 0], color='cyan', marker='o',linestyle='None', markersize=2) 
    mplcursors.cursor(plotted_points, hover=True)
    plt.show(

I tried using mplcursors. But nothing is shown on plot when mouse is hovered. The arrow is not showing any coordinates. I tried by just plotting points without image, still its not showing any coordinates.

plot_withoutimage

[106 190] [205 167] 249 280] [ 80 301] [343 294] [207 151] [293 300] [ 30 298] [116 301] [290 261] [ 38 81] [295 316] [209 262] [210 285] [205 323] [282 244] [128 158] [255 26] [133 708] [213 243] [214 690] [ 62 241] [ 68 181] [254 692] [ 35 21] [ 74 318] [290 275] [284 178] [185 21] [207 107] [ 25 323] [331 88] [113 273] [157 293] [112 241] [164 323] [250 301] [137 20] [ 35 645] [284 484] [244 317] [ 27 26 9] [356 331] [356 25] [131 279] [206 296] [ 35 490] [139 85] [215 396] [116 150] [139 644] [338 305] [183 330] [140 488] [327 331] [357 550] [187 707] [260 705] [180 646] [282 552] [ 61 554] [113 485] [163 269] [114 642] [287 645] [281 287] [140 241] [327 181] [ 36 552] [242 263] [257 87] [ 52 18] [358 61] [110 397] [359 692] [139 176] [212 23] [140 332] [271 704] [254 359] [ 63 19] [ 37 396] [110 20] [ 65 82] [ 36 176] [211 706] [108 162] [ 65 397] [212 552] [ 62 645] [109 550] [256 240] [283 24] [185 552] [286 86] [186 242] [355 180] [141 550] [ 64 707] [283 704] [285 330] [257 398] [185 398] [285 398] [109 126] [356 89] [ 37 709] [ 64 331] [111 316] [ 35 334] [214 487] [328 241] [119 172] [212 88] [356 244] [253 332] [ 39 240] [328 27] [359 395] [ 77 266] [358 641] [211 642] [110 706] [181 151] [140 396] [110 332] [ 64 489] [182 486] [328 396] [254 485] [195 274] [256 549] [111 83] [328 639] [253 179] [339 270] [206 135] [325 488]] this is the coords array. So how can I save the coordiantes in a variable according to shape of image. Only rectangles in first row. My image is a series of rectangle shaped sensors. Thats why i tried to find the coordinates by mouse click.


Solution

  • mplcursors automatically create annotations with the x and y positions. Only when you need extra information, you'd need to write a function to change the annotation depending on the selected point.

    Here is some code, using a random image to show how it would work:

    import matplotlib.pyplot as plt
    import numpy as np
    import mplcursors
    
    coords = np.array([[301,557],[301,378],[201,426],[77,58],[243,12],[466,87],[161,75],[302,489],[197,507],[157,400],[131,85],[379,253],[386,525],[215,500],[165,963],[210,381],[308,254],[285,378],[217,628],[77,500],[156,629],[527,67],[514,1010],[234,264],[523,717],[282,630],[523,799],[355,783],[379,474],[353,711],[351,473],[416,719],[232,249],[185,75],[451,409],[130,247],[384,509],[309,473],[202,937],[130,561],[340,549],[310,627],[348,256],[381,869],[258,555],[342,417],[204,393],[385,489],[523,483],[303,784],[448,476],[228,939],[229,559],[454,260],[158,939],[109,521],[158,248],[160,561],[425,319],[129,939],[526,263],[221,389],[236,754],[203,628],[335,492],[453,319],[160,313],[197,76],[420,410],[129,874],[279,250],[130,310],[206,472],[352,1017],[228,508],[117,555],[278,783],[306,937],[424,475],[157,874],[235,407],[78,552],[129,625],[128,781],[423,934],[349,316],[202,781],[378,935],[475,543],[204,547],[391,546],[424,253],[202,251],[170,549],[236,781],[132,471],[75,959],[128,407],[200,356],[309,716],[232,715],[422,629],[274,562],[201,563],[234,471],[149,89],[277,713],[290,12],[236,313],[161,720],[383,626],[350,867],[451,782],[379,783],[450,934],[166,467],[347,626],[450,559],[451,712],[418,875],[379,318],[156,1018],[306,871],[379,408],[278,471],[203,715],[161,782],[203,872],[277,936],[234,874],[348,933],[235,627],[420,561],[438,502],[204,314],[263,499],[279,317],[421,780],[453,627],[334,959],[174,496],[125,500],[131,542],[159,545],[352,499],[79,1012],[417,501],[501,128],[295,340],[215,543],[218,358],[168,1020],[112,504],[158,502]])
    
    fig, ax = plt.subplots()
    
    img = np.random.rand(550, 1050)
    ax.imshow(img, cmap='gray')
    plotted_points = ax.plot(coords[:, 1], coords[:, 0], color='cyan', marker='o', linestyle='None', markersize=2)
    mplcursors.cursor(plotted_points, hover=True)
    plt.show()
    

    mplcursors to show point coordinates