5

basically I want to graph two functions

g1 = x*cos(x*pi)
g2 = 1 - 0.6x^2

and then plot the intersection, I already have a module that takes inputs close to the two lines intersections, and then converges to those points (there's four of them)

but I want to graph these two functions and their intersections using matplotlib but have no clue how. I've only graphed basic functions. Any help is greatly appreciated

1
  • If you already have a routine that finds the intersection, why don't you just plot these points? Commented May 3, 2013 at 4:42

1 Answer 1

12

Assuming you can get as far as plotting one function, with x and g1 as numpy arrays,

pylab.plot(x,g1)

just call plot again (and again) to draw any number of separate curves:

pylab.plot(x,g2)

finally display or save to a file:

pylab.show()

To indicate a special point such as an intersection, just pass in scalars for x, y and ask for a marker such 'x' or 'o' or whatever else you like.

pylab.plot(x_intersect, y_intersect, 'x', color="#80C0FF")

Alternatively, I often mark a special place along x with a vertical segment by plotting a quick little two-point data set:

pylab.plot( [x_special, x_special], [0.5, 1.9], '-b' )

I may hardcode the y values to look good on a plot for my current project, but obviously this is not reusable for other projects. Note that plot() can take ordinary python lists; no need to convert to numpy arrays.

If you can't get as far as plotting one function (just g1) then you need a basic tutorial in matplot lib, which wouldn't make a good answer here but please go visit http://matplotlib.org/ and google "matplotlib tutorial" or "matplotlib introduction".

Sign up to request clarification or add additional context in comments.

1 Comment

if yop want to draw lines across the whole plot use ax.axhline and ax.axvline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.