ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 04 Dec 2014 18:23:48 +0100Plotting a 2d functionhttps://ask.sagemath.org/question/25122/plotting-a-2d-function/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!Thu, 04 Dec 2014 17:53:03 +0100https://ask.sagemath.org/question/25122/plotting-a-2d-function/Comment by kcrisman for <p>This should be easy, I want to make a nice colour plot of a 2d function where colour denotes the function value.</p>
<p>I can do </p>
<blockquote>
<p>y=var('y')</p>
<p>f=y^2</p>
<p>plot(f)</p>
</blockquote>
<p>and I get a nice plot.</p>
<p>If I try</p>
<blockquote>
<p>y=var('y')</p>
<p>f=y^2+x^2</p>
<p>plot(f)</p>
</blockquote>
<p>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.</p>
<p>Many thanks!</p>
https://ask.sagemath.org/question/25122/plotting-a-2d-function/?comment=25127#post-id-25127One 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.Thu, 04 Dec 2014 18:23:48 +0100https://ask.sagemath.org/question/25122/plotting-a-2d-function/?comment=25127#post-id-25127Answer by slelievre for <p>This should be easy, I want to make a nice colour plot of a 2d function where colour denotes the function value.</p>
<p>I can do </p>
<blockquote>
<p>y=var('y')</p>
<p>f=y^2</p>
<p>plot(f)</p>
</blockquote>
<p>and I get a nice plot.</p>
<p>If I try</p>
<blockquote>
<p>y=var('y')</p>
<p>f=y^2+x^2</p>
<p>plot(f)</p>
</blockquote>
<p>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.</p>
<p>Many thanks!</p>
https://ask.sagemath.org/question/25122/plotting-a-2d-function/?answer=25125#post-id-25125You are probably looking for the `contour_plot` function, which is briefly mentioned in [Sage guided tour's plotting section](http://www.sagemath.org/doc/tutorial/tour_plotting.html), documented at [contour_plot documentation](http://www.sagemath.org/doc/reference/plotting/sage/plot/contour_plot.html), and also featured in the [contour_plot tutorial](http://www.sagemath.org/doc/prep/Advanced-2DPlotting.html#contour).
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.
Thu, 04 Dec 2014 18:16:01 +0100https://ask.sagemath.org/question/25122/plotting-a-2d-function/?answer=25125#post-id-25125Answer by FrédéricC for <p>This should be easy, I want to make a nice colour plot of a 2d function where colour denotes the function value.</p>
<p>I can do </p>
<blockquote>
<p>y=var('y')</p>
<p>f=y^2</p>
<p>plot(f)</p>
</blockquote>
<p>and I get a nice plot.</p>
<p>If I try</p>
<blockquote>
<p>y=var('y')</p>
<p>f=y^2+x^2</p>
<p>plot(f)</p>
</blockquote>
<p>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.</p>
<p>Many thanks!</p>
https://ask.sagemath.org/question/25122/plotting-a-2d-function/?answer=25124#post-id-25124One 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))Thu, 04 Dec 2014 18:12:06 +0100https://ask.sagemath.org/question/25122/plotting-a-2d-function/?answer=25124#post-id-25124