Python Forum
how to show the distance between two curves in a graph
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to show the distance between two curves in a graph
#1
I have these two curves. How could I plot a segment (and the number) whitch rapresent the distance between them (5 in this case) in the same graph?

import matplotlib.pyplot as plt

X_ev = [0, 1, 2, 3]
Y1_ev = [10, 15, 20, 25]
Y2_ev = [2, 10, 10, 15]
plt.plot(X_ev,Y1_ev, 'g', label='Curve 1')
plt.plot(X_ev,Y2_ev, 'r', label='Curve 2')
plt.legend(numpoints=3)
plt.show()
Reply
#2
if looking for Euclidean distance between,
import numpy
import matplotlib.pyplot as plt
 
X_ev = [0, 1, 2, 3]
Y1_ev = [10, 15, 20, 25]
Y2_ev = [2, 10, 10, 15]
dist = numpy.linalg.norm(Y1_ev-Y2_ev)
untested, but I think this is correct
Reply
#3
Sorry, I didn't explain myself well.
My aim is to draw the minimum distance between the points in the plot and not the curves.
I'd like to plot the line (length = SEGMENT) which links two points of different curves.

import matplotlib.pyplot as plt
import numpy as np

X_ev = np.array([0, 1, 2, 3])
Y1_ev = np.array([10, 15, 20, 25])
Y2_ev = np.array([2, 10, 10, 15])
plt.plot(X_ev,Y1_ev, 'g', label='Curve 1')
plt.plot(X_ev,Y2_ev, 'r', label='Curve 2')

SEGMENT = min(Y1_ev-Y2_ev)
print('SEGMENT')
print(SEGMENT)

plt.legend(numpoints=3)
plt.show()
Reply
#4
This?
import matplotlib.pyplot as plt
import numpy as np


def list_diff(l1, l2):
    new_list = []
    for n, item in enumerate(l1):
        new_list.append(item - l2[n])
    return new_list

X_ev = np.array([0, 1, 2, 3])
Y1_ev = np.array([10, 15, 20, 25])
Y2_ev = np.array([2, 10, 10, 15])

print(f"distance between points: {list_diff(Y1_ev, Y2_ev)}")

plt.plot(X_ev,Y1_ev, 'g', label='Curve 1')
plt.plot(X_ev,Y2_ev, 'r', label='Curve 2')
 
SEGMENT = min(Y1_ev-Y2_ev)
print('SEGMENT')
print(SEGMENT)

plt.legend(numpoints=3)
plt.show()
Reply
#5
No, I'd like to have this (attachment)

Attached Files

Thumbnail(s)
   
Reply
#6
Is this homework?
Reply
#7
Do you mean a graph like this example: https://matplotlib.org/3.1.1/gallery/lin..._demo.html
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Plot multiple 2D Curves in 3D stc 5 945 Jun-11-2023, 05:48 PM
Last Post: snippsat
  PIL Image im.show() no show! Pedroski55 2 958 Sep-12-2022, 10:19 PM
Last Post: Pedroski55
  PIL Image im.show() no show! Pedroski55 6 4,879 Feb-08-2022, 06:32 AM
Last Post: Pedroski55
  Level curves don't match after rotation schniefen 1 1,531 Dec-14-2020, 09:56 PM
Last Post: schniefen
  smallest Cosine distance in Graph vino689 3 2,312 Jan-12-2020, 08:06 AM
Last Post: rmspacedashrf
  Visualize Geo Map/Calculate distance zarize 1 1,890 Dec-05-2019, 08:36 PM
Last Post: Larz60+
  euclidean distance jenya56 3 2,801 Mar-29-2019, 02:56 AM
Last Post: scidam
  organizing by distance gonzo620 7 3,891 Oct-16-2018, 01:41 AM
Last Post: stullis
  Python Ble Distance Problem aleynasarcanli 10 12,495 Feb-09-2018, 10:48 PM
Last Post: aleynasarcanli
  Finding the intersection of interpolated curves Tina 1 4,153 Mar-23-2017, 05:33 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020