0

In matplotlib, what is the best way to plot a family of curves which is a solution to a differential equation. How can I plot for example,

enter image description here

and to overlay family of orthogonal trajectories over this family of curves

enter image description here

Finally I want to be able to produce a figure similar to the followingenter image description here

I am aware of contour plots of implicit functions, but how are they related to the constant C in the above equations. I could produce following figure based on the suggestion, But I can't to seem to have the contour labels on the overlaid parabolas.

enter image description here

CODE:

import matplotlib.pyplot as plt
from numpy import arange, meshgrid

delta = 0.025
x_range = arange(-20.0, 20.0, delta)
y_range = arange(-20.0, 20.0, delta)
X, Y = meshgrid(x_range,y_range)

fig=plt.figure()
ax=fig.add_subplot(111)
ax.set_xlim(xmin=-6, xmax=6)
ax.set_ylim(ymin=-4, ymax=4)
ax.axhline(y=0, color='k',linewidth=1)
ax.axvline(x=0, color='k',linewidth=1)
# F is one side of the equation, G is the other
F = (X**2)/2.0+(Y**2)
colrs_tuple=('k','c','c','k')
#C = range(6,14,3)
C = (2,6,9,12)
#print(type(C))
CS = plt.contour(X, Y, F, C, colors=colrs_tuple)
labels = plt.clabel(CS)
xy = [t.get_position() for t in labels]
print(xy)

F=Y
G=X**2
C = (-5,-3,-1,1,3,5)
##for c1 in C:
##    CS=plt.contour(X, Y, F/G- c1,[0], colors='c',linestyles='dashed')
##    labels = plt.clabel(CS)
##    xy = [t.get_position() for t in labels]
##    print(xy)
##plt.show()
CS1=plt.contour(X, Y, F/G,levels=(-15,-3,-1,-0.5,0.5,1,3,15), colors='c',linestyles='dashed')
labels1 = plt.clabel(CS1)
plt.show()
srinivasu u
  • 1,393
  • 11
  • 12
  • Two comments: (1) the equation you show is not a differential equation. (2) Your equation can be expressed as a function, `f(x,y) = 0.5*x**2+y**2-c`. Producing a `meshgrid` of x-y values and an array with the function values, you can use these arrays to produce a contour plot. If in the contour plot you pass the keyword (`levels = [0]`), you should get exactly what you want. The second function (`y=C*x**2`) can just be drawn on top with plot() – Thomas Kühn Jan 29 '18 at 14:27
  • Thank you for your ideas. But I did not say it is a differential equation, I said it is solution to a differential equation. When I did as you said, It is saying the constant C is not defined. That means I have to manually give C value for each curve? – srinivasu u Jan 30 '18 at 04:48
  • If you have several values for `C`, put them in your contour plot command. I.e. use `f(x,y)=0.5*x**2+y**2` and then in `plot.contour()` use `levels = [c1,c2,c3, ...]`. – Thomas Kühn Jan 30 '18 at 08:36
  • You completely changed the question. Please do not do that. At first you asked about how to plot implicit functions. Now the question asks about why not labels are shown in the plot. This is completely unrelated. If you have a new question, ask a new question. Here the answer is simple: the labels are outside of the area which is shown in the plot. Solution: plot the complete area (`ax.set_xlim(xmin=-20, xmax=20); ax.set_ylim(ymin=-20, ymax=20)`), or limit your data (`x_range = arange(-6.0, 6.0, delta); y_range = arange(-4.0, 4.0, delta)`). – ImportanceOfBeingErnest Jan 31 '18 at 00:48
  • Sorry. I am getting a little confused. Should I remove this question and ask a new one? I saw it to be marked duplicate, but because someone answered it and I continued this stuff. – srinivasu u Jan 31 '18 at 04:57
  • @srinivasuu No, leave this question as it is. Duplicates are not a bad thing. Different users use different phrases for their searches and by linking questions as duplicates, it makes it easier for the answer to be found. For the part about the contour labels please write a new question. – Thomas Kühn Jan 31 '18 at 13:38

0 Answers0