# Compare two plots by finding the minimum distance among points

I have a question about comparing the points within two plots.

I would like to compare two plots and find the minimum distance among their points, in order to find the nearest/common points (i.e., those ones with minimum or zero-distance) and plot it (overlapping).

What I did was to extract the coordinates of the respectively points. But I do not know how to compare them and/or the two plots. I used the following line of code, but the result is completely different from what I am looking for.

Outer[EuclideanDistance, seq1, seq2, 1] // Flatten

seq1 = {{160.5, 262.5}, {105.5, 241.5}, {247.5, 241.5}, {333.5,
220.5}, {34.5, 199.5}, {239.5, 178.5}, {58.5, 136.5}, {159.5,
73.5}, {281.5, 178.5}, {124.5, 262.5}, {196.5, 152.5}, {92.5,
194.5}, {153.5, 239.5}, {120.5, 236.5}, {105.5, 173.5}, {88.5,
131.5}, {26.5, 110.5}, {96.5, 110.5}, {152, 89.5}, {2.5,
68.5}, {49.5, 47.5}, {281.5, 221.5}, {217.5, 200.5}, {172.5,
158.5}, {296.5, 179.5}, {51.5, 300.5}, {60.5, 279.5}, {171.5,
279.5}, {311, 216}, {350.5, 216.5}, {83.5, 153.5}, {239.5,
132.5}, {75.5, 111.5}, {79.5, 195.5}, {110.5, 195.5}, {126.5,
195.5}, {183.5, 153.5}, {49.5, 90.5}, {53.5, 158.5}, {111.5,
216.5}, {244.5, 258.5}, {110.5, 69.5}, {221.5, 237.5}, {276.5,
237.5}, {147.5, 299.5}, {165.5, 195.5}, {84.5, 299.5}, {92.5,
299.5}, {21.5, 257.5}, {29.5, 257.5}, {77.5, 89.5}, {60.5,
68.5}, {68.5, 47.5}, {76.5, 47.5}, {139.5, 257.5}, {36.5,
175.5}, {185.5, 175.5}, {99.5, 154.5}, {43.5, 133.5}, {43.5,
70.5}, {129.5, 70.5}, {4.5, 49.5}, {254.5, 195.5}, {264.5,
90.5}, {342.5, 90.5}, {175.5, 215.5}, {214.5, 215.5}, {307.5,
174.5}, {230.5, 258.5}, {144.5, 216.5}, {42.5, 153.5}, {190.5,
132.5}, {42.5, 111.5}, {66.5, 90.5}, {121.5, 90.5}, {96.5,
69.5}, {174.5, 48.5}, {228.5, 278.5}}

seq2 = {{160.5, 262.5}, {105.5, 241.5}, {247.5, 241.5}, {333.5,
220.5}, {34.5, 199.5}, {239.5, 178.5}, {58.5, 136}, {159.5,
73.5}, {281.5, 178.5}, {128, 262.5}, {196.5, 152.5}, {92,
194.5}, {153.5, 239.5}, {120.5, 236.5}, {105.5, 173.5}, {88.5,
131.5}, {26.5, 110.5}, {96.5, 110.5}, {152.5, 89.5}, {2.5,
68.5}, {49.5, 47.5}, {281.5, 221.5}, {217.5, 200.5}, {172.5,
158.5}, {296.5, 179.5}, {51.5, 300.5}, {60.5, 279.5}, {171.5,
279.5}, {311.5, 216.5}, {350.5, 216.5}, {83.5, 153.5}, {239.5,
132.5}, {75.5, 111.5}, {79.5, 195.5}, {110.5, 200.5}, {126.5,
195.5}, {183.5, 153.5}, {49.5, 90.5}, {53.5, 158.5}, {111.5,
216.5}, {244.5, 258.5}, {114.5, 69.5}, {221.5, 237.5}, {276.5,
237.5}, {147.5, 299.5}, {165.5, 195.5}, {84.5, 299.5}, {92.5,
299.5}, {22.5, 257.5}, {29.5, 257.5}, {77.5, 89.5}, {60.5,
68.5}, {68.5, 47.5}, {76.5, 47.5}, {139.5, 257.5}, {36.5,
175.5}, {185.5, 175.5}, {99.5, 154.5}, {43.5, 133.5}, {43.5,
70.5}, {129.5, 70.5}, {4.5, 49.5}, {254.5, 195.5}, {264.5,
90.5}, {342.5, 90.5}, {175.5, 215.5}, {214.5, 215.5}, {307.5,
174.5}, {230.5, 258.5}, {144.5, 216.5}, {42.5, 153.5}, {190.5,
132.5}, {42.5, 111.5}, {66.5, 90.5}, {121.5, 90.5}, {96.5,
69.5}, {174.5, 48.5}, {228.5, 278.5}}


The result should show the points on the plot equal (almost in common) between the two plots.

• Some working code to reproduce the problem would make it more likely that someone would help. Sep 23, 2019 at 23:13
• I added the sequences that I am considering. Thanks Michael E2.
– Val
Sep 23, 2019 at 23:27

Since most of the points in your two data sets coincide and none of them has a nearest neighbor that is very far away when the total scale of the two plots is taken into consideration, I recommend visualizing the spacial relation between the points by plotting the points of one dataset and showing the offset of the nearest point in the other by color. Like so:

Data

seq1 =
{{160.5, 262.5}, {105.5, 241.5}, {247.5, 241.5}, {333.5, 220.5},
{34.5, 199.5}, {239.5, 178.5}, {58.5, 136.5}, {159.5, 73.5},
{281.5, 178.5}, {124.5, 262.5}, {196.5, 152.5}, {92.5, 194.5},
{153.5, 239.5}, {120.5, 236.5}, {105.5, 173.5}, {88.5, 131.5},
{26.5, 110.5}, {96.5, 110.5}, {152, 89.5}, {2.5, 68.5},
{49.5, 47.5}, {281.5, 221.5}, {217.5, 200.5}, {172.5, 158.5},
{296.5, 179.5}, {51.5, 300.5}, {60.5, 279.5}, {171.5, 279.5},
{311, 216}, {350.5, 216.5}, {83.5, 153.5}, {239.5, 132.5},
{75.5, 111.5}, {79.5, 195.5}, {110.5, 195.5}, {126.5, 195.5},
{183.5, 153.5}, {49.5, 90.5}, {53.5, 158.5}, {111.5, 216.5},
{244.5, 258.5}, {110.5, 69.5}, {221.5, 237.5}, {276.5, 237.5},
{147.5, 299.5}, {165.5, 195.5}, {84.5, 299.5}, {92.5, 299.5},
{21.5, 257.5}, {29.5, 257.5}, {77.5, 89.5}, {60.5, 68.5},
{68.5, 47.5}, {76.5, 47.5}, {139.5, 257.5}, {36.5, 175.5},
{185.5, 175.5}, {99.5, 154.5}, {43.5, 133.5}, {43.5, 70.5},
{129.5, 70.5}, {4.5, 49.5}, {254.5, 195.5}, {264.5, 90.5},
{342.5, 90.5}, {175.5, 215.5}, {214.5, 215.5}, {307.5, 174.5},
{230.5, 258.5}, {144.5, 216.5}, {42.5, 153.5}, {190.5, 132.5},
{42.5, 111.5}, {66.5, 90.5}, {121.5, 90.5}, {96.5, 69.5},
{174.5, 48.5}, {228.5, 278.5}};
seq2 =
{{160.5, 262.5}, {105.5, 241.5}, {247.5, 241.5}, {333.5, 220.5},
{34.5, 199.5}, {239.5, 178.5}, {58.5, 136}, {159.5, 73.5},
{281.5, 178.5}, {128, 262.5}, {196.5, 152.5}, {92, 194.5},
{153.5, 239.5}, {120.5, 236.5}, {105.5, 173.5}, {88.5, 131.5},
{26.5, 110.5}, {96.5, 110.5}, {152.5, 89.5}, {2.5, 68.5},
{49.5, 47.5}, {281.5, 221.5}, {217.5, 200.5}, {172.5, 158.5},
{296.5, 179.5}, {51.5, 300.5}, {60.5, 279.5}, {171.5, 279.5},
{311.5, 216.5}, {350.5, 216.5}, {83.5, 153.5}, {239.5, 132.5},
{75.5, 111.5}, {79.5, 195.5}, {110.5, 200.5}, {126.5, 195.5},
{183.5, 153.5}, {49.5, 90.5}, {53.5, 158.5}, {111.5, 216.5},
{244.5, 258.5}, {114.5, 69.5}, {221.5, 237.5}, {276.5, 237.5},
{147.5, 299.5}, {165.5, 195.5}, {84.5, 299.5}, {92.5, 299.5},
{22.5, 257.5}, {29.5, 257.5}, {77.5, 89.5}, {60.5, 68.5},
{68.5, 47.5}, {76.5, 47.5}, {139.5, 257.5}, {36.5, 175.5},
{185.5, 175.5}, {99.5, 154.5}, {43.5, 133.5}, {43.5, 70.5},
{129.5, 70.5}, {4.5, 49.5}, {254.5, 195.5}, {264.5, 90.5},
{342.5, 90.5}, {175.5, 215.5}, {214.5, 215.5}, {307.5, 174.5},
{230.5, 258.5}, {144.5, 216.5}, {42.5, 153.5}, {190.5, 132.5},
{42.5, 111.5}, {66.5, 90.5}, {121.5, 90.5}, {96.5, 69.5},
{174.5, 48.5}, {228.5, 278.5}};


Offset plot

nf = Nearest[seq1]
clusters = GroupBy[seq2, {# - nf[#][[1]]} &];
colors = ColorData[24] /@ Range @ Length @ clusters
Legended[
Graphics[
{AbsolutePointSize[10], MapThread[{#1, Point[#2]} &, {colors, Values @ clusters}]},
Frame -> True,
ImageSize -> Large],
SwatchLegend[colors, Keys @ clusters, LegendLabel -> "Offset"]]


Second thoughts

Perhaps using ListPlot with markers makes for a better visualization.

nf = Nearest[seq1];
clusters = GroupBy[seq2, {# - nf[#][[1]]} &];
ListPlot[Values @ clusters,
PlotMarkers -> {Automatic, 15},
PlotLegends ->
PointLegend[HoldForm /@ (Keys @ clusters)[[All, 1]], LegendLabel -> "Offset"],
Frame -> True,
ImageSize -> Large]


• Thank you so much, m_goldberg. I did not think to use different colours to represent the differences and their distance. The result is even better than what I expected
– Val
Sep 24, 2019 at 18:07
distance = DistanceMatrix[seq1, seq2]

points = Position[distance, x_ /; x < 0.1]

ListPlot[{seq1[[First /@ points]], seq2[[Last /@ points]]}]