I am working to practice on some simple parameterized curve given by:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(6, 3))

t = np.linspace(-4, 4, 100)

x = t
y = t**3 - 9*t 

ax.plot(x, y, label="curve")
ax.legend()

plt.show()

original

My problem: I would like to construct and show for some points on the plot:

  • the unit tangent vector T
  • the unit normal vector N, pointing towards the "concavity" (is it the right word?) of the curve

What I would like to have is depicted for 2 arbitrary points on the plot below (artificially constructed):

wanted

How could I have this, in a "pythonic" way? I am just aware that np.gradient gives the derivatives, such as:

dydx = np.gradient(y, x)

But I have no practice in using it and don't know how to plot unit vectors and, of course, normal vectors...

4 Replies 4

I don't know of a matplotlib-builtin way to do this and I don't think there is. Numerical differentiation can be tricky and is out-of-scope for matplotlib.

You could calculate the slope of T as the derivative of your function and from that, draw the arrows yourself with matplotlib.

The unit tangent vector can be calculated by normalising successive differences (or centred differences if you want a higher-order method). The unit normal can then be computed by rotating 90 degrees. You can draw the arrows with, e.g., ax.annotate.

However …

You will have major problems with this if your aspect ratio is different from 1 (as in your plot). Although the arrows that you have drawn for T and N look 90 degrees apart that would not be the case if you used equal x and y ranges.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.