ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 16 Nov 2016 13:20:38 -0600Contour plot restricted to a region defined by multiple inequalitieshttp://ask.sagemath.org/question/35616/contour-plot-restricted-to-a-region-defined-by-multiple-inequalities/I'm trying to use `contour_plot` for plotting contours defined only in a region of the parameter space, namely only in the triangle defined by the vertices $(0,0), (1/2,\sqrt{3}/2), (1,0)$. The faces of this triangle are defined by the intersections of the x-axis and the lines of equation $y=\sqrt{3}x$ and $y=-\sqrt{3}x+\sqrt{3}$.
Contour_plot has the nice option `region`, but it seems that `region` only accepts one condition, i.e. only one boundary because the documentation specifies that `region` must be of the form $f(x,y)$ and this is interpreted by the code of `countour_plot` as $f(x,y)>0$, i.e. `countour_plot` plots all points such that $f(x,y)>0$. Is there a way to do what I need with this version of `contour_plot`?
The following represents what I’d like to do but it does not work. It does not throw an error but only takes into account the first condition
`contour_plot((x^2) * cos(x*y), (x,-10,5), (y,-5,5), region=(-sqrt(3)*x-y and -sqrt(3)*x+sqrt(3)-y), fill=True, contours=30)`
Note that there are other imperfect solutions to this problem like using a white polygon to hide the part of the plot I do not want to see. However, this "solution" is not desirable because the maximum and minimum values of `contour_plot` will not be defined with respect to the region I am trying to show (I hope this is clear; I can show an example if this is not).Wed, 16 Nov 2016 09:52:57 -0600http://ask.sagemath.org/question/35616/contour-plot-restricted-to-a-region-defined-by-multiple-inequalities/Answer by paulmasson for <p>I'm trying to use <code>contour_plot</code> for plotting contours defined only in a region of the parameter space, namely only in the triangle defined by the vertices $(0,0), (1/2,\sqrt{3}/2), (1,0)$. The faces of this triangle are defined by the intersections of the x-axis and the lines of equation $y=\sqrt{3}x$ and $y=-\sqrt{3}x+\sqrt{3}$.</p>
<p>Contour_plot has the nice option <code>region</code>, but it seems that <code>region</code> only accepts one condition, i.e. only one boundary because the documentation specifies that <code>region</code> must be of the form $f(x,y)$ and this is interpreted by the code of <code>countour_plot</code> as $f(x,y)>0$, i.e. <code>countour_plot</code> plots all points such that $f(x,y)>0$. Is there a way to do what I need with this version of <code>contour_plot</code>?</p>
<p>The following represents what I’d like to do but it does not work. It does not throw an error but only takes into account the first condition</p>
<p><code>contour_plot((x^2) * cos(x*y), (x,-10,5), (y,-5,5), region=(-sqrt(3)*x-y and -sqrt(3)*x+sqrt(3)-y), fill=True, contours=30)</code></p>
<p>Note that there are other imperfect solutions to this problem like using a white polygon to hide the part of the plot I do not want to see. However, this "solution" is not desirable because the maximum and minimum values of <code>contour_plot</code> will not be defined with respect to the region I am trying to show (I hope this is clear; I can show an example if this is not).</p>
http://ask.sagemath.org/question/35616/contour-plot-restricted-to-a-region-defined-by-multiple-inequalities/?answer=35619#post-id-35619Since both `sqrt(3)*x-y` and `-sqrt(3)*x+sqrt(3)-y` must be positive to define your region, multiply the two together:
var('x y')
contour_plot((x^2) * cos(x*y), (x,0,1), (y,0,sqrt(3)/2),
region=(sqrt(3)*x-y)*(-sqrt(3)*x+sqrt(3)-y), fill=True, contours=30)
Here's a [live example](http://sagecell.sagemath.org/?z=eJwrSyzSUK9QqFTX5OVKzs8ryS8tii_IyS_R0KiIM9JU0FJIzi_WqNCq1NRR0KjQMdAxBDEqgYziwqISDWNNfSNNHV4uBWRQlJqemZ9nqwFVoVWhW6mppaGgC-drQ1m6IFPTMnNybEOKSlN1FKD2F9saGwBdAwDeBCrL&lang=sage).Wed, 16 Nov 2016 12:22:16 -0600http://ask.sagemath.org/question/35616/contour-plot-restricted-to-a-region-defined-by-multiple-inequalities/?answer=35619#post-id-35619Comment by paulmasson for <p>Since both <code>sqrt(3)*x-y</code> and <code>-sqrt(3)*x+sqrt(3)-y</code> must be positive to define your region, multiply the two together:</p>
<pre><code>var('x y')
contour_plot((x^2) * cos(x*y), (x,0,1), (y,0,sqrt(3)/2),
region=(sqrt(3)*x-y)*(-sqrt(3)*x+sqrt(3)-y), fill=True, contours=30)
</code></pre>
<p>Here's a <a href="http://sagecell.sagemath.org/?z=eJwrSyzSUK9QqFTX5OVKzs8ryS8tii_IyS_R0KiIM9JU0FJIzi_WqNCq1NRR0KjQMdAxBDEqgYziwqISDWNNfSNNHV4uBWRQlJqemZ9nqwFVoVWhW6mppaGgC-drQ1m6IFPTMnNybEOKSlN1FKD2F9saGwBdAwDeBCrL&lang=sage">live example</a>.</p>
http://ask.sagemath.org/question/35616/contour-plot-restricted-to-a-region-defined-by-multiple-inequalities/?comment=35621#post-id-35621You can make the boundaries smoother by increasing plot points: `plot_points=500`Wed, 16 Nov 2016 13:20:38 -0600http://ask.sagemath.org/question/35616/contour-plot-restricted-to-a-region-defined-by-multiple-inequalities/?comment=35621#post-id-35621Comment by Slimane for <p>Since both <code>sqrt(3)*x-y</code> and <code>-sqrt(3)*x+sqrt(3)-y</code> must be positive to define your region, multiply the two together:</p>
<pre><code>var('x y')
contour_plot((x^2) * cos(x*y), (x,0,1), (y,0,sqrt(3)/2),
region=(sqrt(3)*x-y)*(-sqrt(3)*x+sqrt(3)-y), fill=True, contours=30)
</code></pre>
<p>Here's a <a href="http://sagecell.sagemath.org/?z=eJwrSyzSUK9QqFTX5OVKzs8ryS8tii_IyS_R0KiIM9JU0FJIzi_WqNCq1NRR0KjQMdAxBDEqgYziwqISDWNNfSNNHV4uBWRQlJqemZ9nqwFVoVWhW6mppaGgC-drQ1m6IFPTMnNybEOKSlN1FKD2F9saGwBdAwDeBCrL&lang=sage">live example</a>.</p>
http://ask.sagemath.org/question/35616/contour-plot-restricted-to-a-region-defined-by-multiple-inequalities/?comment=35620#post-id-35620This solves the problem, thanks!
Also, one might still want to use `polygon` to have better-looking boundaries.Wed, 16 Nov 2016 12:36:19 -0600http://ask.sagemath.org/question/35616/contour-plot-restricted-to-a-region-defined-by-multiple-inequalities/?comment=35620#post-id-35620