Ask Your Question
1

Plotting a 2d function

asked 2014-12-04 17:53:03 +0200

Darwin gravatar image

updated 2015-01-14 11:44:58 +0200

FrédéricC gravatar image

This should be easy, I want to make a nice colour plot of a 2d function where colour denotes the function value.

I can do

y=var('y')

f=y^2

plot(f)

and I get a nice plot.

If I try

y=var('y')

f=y^2+x^2

plot(f)

Then sage tells me it can't find the variable y. Which is ridiculous, since y is in the same place as when I tried to plot a 1d function of y. Anyway, I tried googling to find a function within sage to achieve this (without resorting to excessive matplotlib syntax) and had no success so thought I'd ask a question here. Intuitively, the second case is hardly more complicated than the former so there should be a simple function that can realize this for me.

Many thanks!

edit retag flag offensive close merge delete

Comments

One should point out that there is a default range of the variable in the former case as an aide to users, but that in the second case Sage does not try to guess what your intent was, nor to provide an auto-range. For instance, which axis should belong to which variable? So it's actually a feature that the second one doesn't "just work", and if it did, it would give you a 3d plot.

kcrisman gravatar imagekcrisman ( 2014-12-04 18:23:48 +0200 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2014-12-04 18:12:06 +0200

FrédéricC gravatar image

One way is like that:

sage: var('x,y')
(x, y)
sage: f=x**2+y**2
sage: contour_plot(f,(x,-2,2),(y,-2,2))

You can also use plot3d to see the function as a surface in space:

sage: plot3d(f,(x,-2,2),(y,-2,2))
edit flag offensive delete link more
1

answered 2014-12-04 18:16:01 +0200

slelievre gravatar image

You are probably looking for the contour_plot function, which is briefly mentioned in Sage guided tour's plotting section, documented at contour_plot documentation, and also featured in the contour_plot tutorial.

sage: contour_plot(lambda x,y: x^2 + y^2, (-2,2), (-2, 2))

You can see more options by accessing the documentation:

sage: contour_plot?

in particular you can specify some options such as colorbar etc.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2014-12-04 17:53:03 +0200

Seen: 481 times

Last updated: Dec 04 '14