6

First off, I'm new to python and matplotlib. I need to plot several systems of implicit equations in one figure.

The equations are in form of:

3x+2y=1

Is there an easy way to plot these, other than first making the equations explicit (i.e. y=...)?

vestland
  • 55,229
  • 37
  • 187
  • 305
Milo Wielondek
  • 4,164
  • 3
  • 33
  • 45

2 Answers2

7
import numpy as np
import matplotlib.pyplot as plt
# Note the order of y,x.
y,x=np.ogrid[-5:5:100j,-5:5:100j]
plt.contour(x.ravel(),y.ravel(),3*x+2*y,[1])
plt.show()

alt text

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • And for three space dimensions, I suggest using [Mayavi](http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/auto/mlab_helper_functions.html#enthought.mayavi.mlab.contour3d) instead. (I'll delete my answer, because yours has a picture :) – Sven Marnach Jan 14 '11 at 11:52
  • @Sven Marnach: I like your solution because it uses less memory. Perhaps undelete it? – unutbu Jan 14 '11 at 11:58
  • OK, on your special request :) – Sven Marnach Jan 14 '11 at 12:44
  • Thanks both of you. One thing seems to be wrong however; for instance, 3x+2y=1 & 3x+4y=5 should intercept at (-1,2). MPL however gives med an intersection point of (2,-1). What's wrong? – Milo Wielondek Jan 14 '11 at 14:30
  • To clarify - the above graph is not the same as the graph for y=(1-3x)/2 (the explicit version of 3x+2y=1), hence the intersection points are wrong as a result of this. This is the main problem though - your solution doesn't give the correct graph. – Milo Wielondek Jan 14 '11 at 14:41
  • @mewoshh: Thanks for pointing out my mistake. I like Sven's answer, but just want correct mine for my own sanity. – unutbu Jan 14 '11 at 19:22
4

You can use contour() to do implicit plots in two space dimensions:

x = numpy.linspace(-2., 2.)
y = numpy.linspace(-2., 2.)[:, None]
contour(x, y.ravel(), 3*x + 2*y, [1])

In 3 dimensions, I suggest using Mayavi instead of matplotlib.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • Hm.. This seems to generate a different graph than the graph for y=(1-3x)/2 which should be the same. Any ideas why? – Milo Wielondek Jan 14 '11 at 15:11
  • @mewoshh: Could you show your exact code, please? It should be something like `contour(x.ravel(), y, y-(1-3*x)/2, [0])`. And the graph should be the same as for the code above. – Sven Marnach Jan 14 '11 at 15:23
  • It is the same as `contour(x.ravel(), y, y-(1-3*x)/2, [0])`. The problem is that this is not the same as the factual graph of `y=(1-3x)/2`. Compare this with `x=np.arange(-6,6,0.01); y=(1-3*x)/2; plot(x,y)`. – Milo Wielondek Jan 14 '11 at 15:38
  • @mewoshh: You are right, confused rows and columns! Will be corrected in a second... – Sven Marnach Jan 14 '11 at 15:44
  • It plotted the transposed graph. Fixed now. – Sven Marnach Jan 14 '11 at 15:45