ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 17 Mar 2012 09:28:27 -0500multiple region_plots in one plothttps://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/Hi,
I want to plot two region_plots into one plot. The idea is if you use different colors for the inequalities you can see how the regions change. A minimal example:
var('x,y')
plot1=region_plot(x<y,(x,0,1),(y,0,1),incol='red')
plot2=region_plot(2*x<y,(x,0,1),(y,0,1),incol='blue')
show(plot2+plot1)
I do not know how to manage this, since zorder or opacity are not working for region_plot. Does anybody know how to make this work (by the way I use sagenb.org)? Thanks in advanceThu, 15 Mar 2012 03:11:55 -0500https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/Comment by kcrisman for <p>Hi,
I want to plot two region_plots into one plot. The idea is if you use different colors for the inequalities you can see how the regions change. A minimal example:</p>
<pre><code>var('x,y')
plot1=region_plot(x<y,(x,0,1),(y,0,1),incol='red')
plot2=region_plot(2*x<y,(x,0,1),(y,0,1),incol='blue')
show(plot2+plot1)
</code></pre>
<p>I do not know how to manage this, since zorder or opacity are not working for region_plot. Does anybody know how to make this work (by the way I use sagenb.org)? Thanks in advance</p>
https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?comment=20122#post-id-20122I suspect we are using contourf inappropriately with two patches - Jason Grout, any ideas?. Possibly related are http://www.mailinglistarchive.com/html/matplotlib-users@lists.sourceforge.net/2010-04/msg00168.html and http://old.nabble.com/contourf-creats-white-like-lines-%28or-gaps%29-between-each-two-color-patches-td27982822.html#a28210898Thu, 15 Mar 2012 03:49:07 -0500https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?comment=20122#post-id-20122Comment by god.one for <p>Hi,
I want to plot two region_plots into one plot. The idea is if you use different colors for the inequalities you can see how the regions change. A minimal example:</p>
<pre><code>var('x,y')
plot1=region_plot(x<y,(x,0,1),(y,0,1),incol='red')
plot2=region_plot(2*x<y,(x,0,1),(y,0,1),incol='blue')
show(plot2+plot1)
</code></pre>
<p>I do not know how to manage this, since zorder or opacity are not working for region_plot. Does anybody know how to make this work (by the way I use sagenb.org)? Thanks in advance</p>
https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?comment=20123#post-id-20123@kcrisman : ok, but could you please explain what you mean with <not so useful plot>?Thu, 15 Mar 2012 03:43:21 -0500https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?comment=20123#post-id-20123Comment by kcrisman for <p>Hi,
I want to plot two region_plots into one plot. The idea is if you use different colors for the inequalities you can see how the regions change. A minimal example:</p>
<pre><code>var('x,y')
plot1=region_plot(x<y,(x,0,1),(y,0,1),incol='red')
plot2=region_plot(2*x<y,(x,0,1),(y,0,1),incol='blue')
show(plot2+plot1)
</code></pre>
<p>I do not know how to manage this, since zorder or opacity are not working for region_plot. Does anybody know how to make this work (by the way I use sagenb.org)? Thanks in advance</p>
https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?comment=20121#post-id-20121By not-so-useful I just mean that it seems to have a similar problem to your original one, but with the opposite region "on top". It was just a comment, not an answer of any kind!Thu, 15 Mar 2012 03:50:33 -0500https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?comment=20121#post-id-20121Comment by kcrisman for <p>Hi,
I want to plot two region_plots into one plot. The idea is if you use different colors for the inequalities you can see how the regions change. A minimal example:</p>
<pre><code>var('x,y')
plot1=region_plot(x<y,(x,0,1),(y,0,1),incol='red')
plot2=region_plot(2*x<y,(x,0,1),(y,0,1),incol='blue')
show(plot2+plot1)
</code></pre>
<p>I do not know how to manage this, since zorder or opacity are not working for region_plot. Does anybody know how to make this work (by the way I use sagenb.org)? Thanks in advance</p>
https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?comment=20124#post-id-20124Just for reference, `plot1+plot2` gives a different (also not so useful) plot.Thu, 15 Mar 2012 03:39:42 -0500https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?comment=20124#post-id-20124Answer by niles for <p>Hi,
I want to plot two region_plots into one plot. The idea is if you use different colors for the inequalities you can see how the regions change. A minimal example:</p>
<pre><code>var('x,y')
plot1=region_plot(x<y,(x,0,1),(y,0,1),incol='red')
plot2=region_plot(2*x<y,(x,0,1),(y,0,1),incol='blue')
show(plot2+plot1)
</code></pre>
<p>I do not know how to manage this, since zorder or opacity are not working for region_plot. Does anybody know how to make this work (by the way I use sagenb.org)? Thanks in advance</p>
https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?answer=13367#post-id-13367The problem is that a `region_plot` is really just a contour plot with exactly two colored regions. The "inside" color is determined by `incolor`, and the "outside" is determined by `outcolor`. Now here's the key: `outcolor` is white by default, so you might *think* that the outside of the region is transparent. But it's not. That explains why you only see one of the two plots -- the top one is completely opaque, thus covering the bottom one. This also explains why you get different results when you sum the two plots in different orders -- this changes which one is on top.
Now here's a fix: just use [`contour_plot`](http://www.sagemath.org/doc/reference/sage/plot/contour_plot.html#sage.plot.contour_plot.contour_plot) directly. To do this, define a function which will separate the regions you're interested in. For example:
def sep(x,y):
if 2*x < y:
return 1
if x < y and y <= 2*x:
return 0
if y <= x:
return -1
Now make the contour plot, choosing contour lines between your separate outputs, and listing the colors you want:
contour_plot(sep, (x,0,2), (y,0,2), plot_points=400, contours=[-.5,.5], cmap=['white','red','blue'])
![image description](/upfiles/13319170612147456.png)
Also note that `contour_plot` will probably work pretty well without you explicitly specifying the contours or the colors, if you don't want to worry about that step.Fri, 16 Mar 2012 06:58:07 -0500https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?answer=13367#post-id-13367Comment by kcrisman for <p>The problem is that a <code>region_plot</code> is really just a contour plot with exactly two colored regions. The "inside" color is determined by <code>incolor</code>, and the "outside" is determined by <code>outcolor</code>. Now here's the key: <code>outcolor</code> is white by default, so you might <em>think</em> that the outside of the region is transparent. But it's not. That explains why you only see one of the two plots -- the top one is completely opaque, thus covering the bottom one. This also explains why you get different results when you sum the two plots in different orders -- this changes which one is on top.</p>
<p>Now here's a fix: just use <a href="http://www.sagemath.org/doc/reference/sage/plot/contour_plot.html#sage.plot.contour_plot.contour_plot"><code>contour_plot</code></a> directly. To do this, define a function which will separate the regions you're interested in. For example:</p>
<pre><code>def sep(x,y):
if 2*x < y:
return 1
if x < y and y <= 2*x:
return 0
if y <= x:
return -1
</code></pre>
<p>Now make the contour plot, choosing contour lines between your separate outputs, and listing the colors you want:</p>
<pre><code>contour_plot(sep, (x,0,2), (y,0,2), plot_points=400, contours=[-.5,.5], cmap=['white','red','blue'])
</code></pre>
<p><img alt="image description" src="/upfiles/13319170612147456.png"/></p>
<p>Also note that <code>contour_plot</code> will probably work pretty well without you explicitly specifying the contours or the colors, if you don't want to worry about that step.</p>
https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?comment=20114#post-id-20114Ah, of course! I don't know why I didn't see that, having worked on this code in the past... Great work.Fri, 16 Mar 2012 15:48:26 -0500https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?comment=20114#post-id-20114Comment by niles for <p>The problem is that a <code>region_plot</code> is really just a contour plot with exactly two colored regions. The "inside" color is determined by <code>incolor</code>, and the "outside" is determined by <code>outcolor</code>. Now here's the key: <code>outcolor</code> is white by default, so you might <em>think</em> that the outside of the region is transparent. But it's not. That explains why you only see one of the two plots -- the top one is completely opaque, thus covering the bottom one. This also explains why you get different results when you sum the two plots in different orders -- this changes which one is on top.</p>
<p>Now here's a fix: just use <a href="http://www.sagemath.org/doc/reference/sage/plot/contour_plot.html#sage.plot.contour_plot.contour_plot"><code>contour_plot</code></a> directly. To do this, define a function which will separate the regions you're interested in. For example:</p>
<pre><code>def sep(x,y):
if 2*x < y:
return 1
if x < y and y <= 2*x:
return 0
if y <= x:
return -1
</code></pre>
<p>Now make the contour plot, choosing contour lines between your separate outputs, and listing the colors you want:</p>
<pre><code>contour_plot(sep, (x,0,2), (y,0,2), plot_points=400, contours=[-.5,.5], cmap=['white','red','blue'])
</code></pre>
<p><img alt="image description" src="/upfiles/13319170612147456.png"/></p>
<p>Also note that <code>contour_plot</code> will probably work pretty well without you explicitly specifying the contours or the colors, if you don't want to worry about that step.</p>
https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?comment=20112#post-id-20112this could be a feature request: allow region_plot to color multiple regions. But I don't think I'm motivated enough to open a ticket for it.Sat, 17 Mar 2012 09:28:27 -0500https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?comment=20112#post-id-20112Comment by kcrisman for <p>The problem is that a <code>region_plot</code> is really just a contour plot with exactly two colored regions. The "inside" color is determined by <code>incolor</code>, and the "outside" is determined by <code>outcolor</code>. Now here's the key: <code>outcolor</code> is white by default, so you might <em>think</em> that the outside of the region is transparent. But it's not. That explains why you only see one of the two plots -- the top one is completely opaque, thus covering the bottom one. This also explains why you get different results when you sum the two plots in different orders -- this changes which one is on top.</p>
<p>Now here's a fix: just use <a href="http://www.sagemath.org/doc/reference/sage/plot/contour_plot.html#sage.plot.contour_plot.contour_plot"><code>contour_plot</code></a> directly. To do this, define a function which will separate the regions you're interested in. For example:</p>
<pre><code>def sep(x,y):
if 2*x < y:
return 1
if x < y and y <= 2*x:
return 0
if y <= x:
return -1
</code></pre>
<p>Now make the contour plot, choosing contour lines between your separate outputs, and listing the colors you want:</p>
<pre><code>contour_plot(sep, (x,0,2), (y,0,2), plot_points=400, contours=[-.5,.5], cmap=['white','red','blue'])
</code></pre>
<p><img alt="image description" src="/upfiles/13319170612147456.png"/></p>
<p>Also note that <code>contour_plot</code> will probably work pretty well without you explicitly specifying the contours or the colors, if you don't want to worry about that step.</p>
https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?comment=20113#post-id-20113So do you think this is worth a trac ticket? I feel like we would really want the original thing to work. But I'm not sure exactly how to do this without removing the 'white' business, which would make the graphics look weird, if I recall correctly.Fri, 16 Mar 2012 15:49:25 -0500https://ask.sagemath.org/question/8792/multiple-region_plots-in-one-plot/?comment=20113#post-id-20113