Python Forum

Full Version: how to show the distance between two curves in a graph
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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
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()
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()
No, I'd like to have this (attachment)
Is this homework?
Do you mean a graph like this example: https://matplotlib.org/3.1.1/gallery/lin..._demo.html