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.Sun, 06 Feb 2011 13:35:06 -0600get_minmax_data on implicit_plothttp://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/This is a sequel of [my question about plotting level set](http://ask.sagemath.org/question/358/plot-the-level-sets-of-a-function).
In the following, G is a circle :
sage: f(x,y)=x**2+y**2
sage: G=implicit_plot(f==1,(x,-2,2),(y,-3,3))
sage: G.get_minmax_data()
{'xmin': -2.0, 'ymin': -3.0, 'ymax': 3.0, 'xmax': 2.0}
The "correct" get_minmax_data sould be
{'xmin': -1.0, 'ymin': -1.0, 'ymax': 1.0, 'xmax': 1.0}
As far as I understood the code (and the thread "Retrieving xy data from implicit plots" on Sage-support), the following is the relevant part :
xy_data_arrays = numpy.asarray([[[func(x, y) for x in xsrange(*ranges[0],include_endpoint=True)]
for y in xsrange(*ranges[1], include_endpoint=True)]
for func in g],dtype=float)
in ../plot/contour_plot.py
My questions are :
1. can I retrieve that xy_data_array ?
2. If I analyse xy_data_array, I suppose that extracting the point with lowest x-component such that the value is positive will provide me the "correct" xmin of the plot. I'm wrong ?
Fri, 04 Feb 2011 06:21:09 -0600http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/Answer by DSM for <p>This is a sequel of <a href="http://ask.sagemath.org/question/358/plot-the-level-sets-of-a-function">my question about plotting level set</a>.</p>
<p>In the following, G is a circle :</p>
<pre><code>sage: f(x,y)=x**2+y**2
sage: G=implicit_plot(f==1,(x,-2,2),(y,-3,3))
sage: G.get_minmax_data()
{'xmin': -2.0, 'ymin': -3.0, 'ymax': 3.0, 'xmax': 2.0}
</code></pre>
<p>The "correct" get_minmax_data sould be</p>
<pre><code>{'xmin': -1.0, 'ymin': -1.0, 'ymax': 1.0, 'xmax': 1.0}
</code></pre>
<p>As far as I understood the code (and the thread "Retrieving xy data from implicit plots" on Sage-support), the following is the relevant part :</p>
<pre><code>xy_data_arrays = numpy.asarray([[[func(x, y) for x in xsrange(*ranges[0],include_endpoint=True)]
for y in xsrange(*ranges[1], include_endpoint=True)]
for func in g],dtype=float)
</code></pre>
<p>in ../plot/contour_plot.py</p>
<p>My questions are :</p>
<ol>
<li><p>can I retrieve that xy_data_array ?</p></li>
<li><p>If I analyse xy_data_array, I suppose that extracting the point with lowest x-component such that the value is positive will provide me the "correct" xmin of the plot. I'm wrong ?</p></li>
</ol>
http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?answer=12066#post-id-12066Following up on the comment discussion about which points are actually used in a plot, here's a revised version of the original answer I gave before kcrisman pointed out g.xy_data_array and I deleted mine as stupid by comparison. This is pretty ugly:
<pre><code>
import matplotlib
def get_paths_from_plot(p):
# untested!
m = p.matplotlib()
sp = m.get_children()[1]
for c in sp.get_children():
# not sure if I need to test for both or not? don't understand
# matplotlib internals well enough to know if every Line2D
# will be in some LineCollection and so this is pure duplication (probably)
if isinstance(c, matplotlib.lines.Line2D):
yield c.get_path()
elif isinstance(c, matplotlib.collections.LineCollection):
for path in c.get_paths():
yield path
def get_bounds_from_implicit_plot(P):
# untested!
xx = []
yy = []
for path in get_paths_from_plot(P):
xx += list(path.vertices[:,0])
yy += list(path.vertices[:,1])
print 'xx:', sorted(xx)[:10]
print 'yy:', sorted(yy)[:10]
d = {'xmin': min(xx), 'xmax': max(xx),
'ymin': min(yy), 'ymax': max(yy)}
return d
</code></pre>
<pre><code>sage: f(x,y)=x**2+y**2
sage: G=implicit_plot(f==1,(x,-2,2),(y,-3,3))
sage: get_bounds_from_implicit_plot(G)
xx: [-0.99972791583529708, -0.99972791583529708, -0.99972791583529697, -0.99972791583529697, -0.99809541084708742, -0.99809541084708742,
-0.99809541084708719, -0.99809541084708719, -0.9948304008706681, -0.9948304008706681]
yy: [-0.99973154362416372, -0.99973154362416372, -0.99973154362416361, -0.99973154362416361, -0.99901565995525998, -0.99901565995525998,
-0.99901565995525976, -0.99901565995525976, -0.99758389261745239, -0.99758389261745239]
{'xmin': -0.99972791583529708, 'ymin': -0.99973154362416372,
'ymax': 0.99973154362415995, 'xmax': 0.99972791583530107}
</code></pre>
Sun, 06 Feb 2011 02:58:55 -0600http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?answer=12066#post-id-12066Comment by DSM for <p>Following up on the comment discussion about which points are actually used in a plot, here's a revised version of the original answer I gave before kcrisman pointed out g.xy_data_array and I deleted mine as stupid by comparison. This is pretty ugly:</p>
<pre><code>
import matplotlib
def get_paths_from_plot(p):
# untested!
m = p.matplotlib()
sp = m.get_children()[1]
for c in sp.get_children():
# not sure if I need to test for both or not? don't understand
# matplotlib internals well enough to know if every Line2D
# will be in some LineCollection and so this is pure duplication (probably)
if isinstance(c, matplotlib.lines.Line2D):
yield c.get_path()
elif isinstance(c, matplotlib.collections.LineCollection):
for path in c.get_paths():
yield path
def get_bounds_from_implicit_plot(P):
# untested!
xx = []
yy = []
for path in get_paths_from_plot(P):
xx += list(path.vertices[:,0])
yy += list(path.vertices[:,1])
print 'xx:', sorted(xx)[:10]
print 'yy:', sorted(yy)[:10]
d = {'xmin': min(xx), 'xmax': max(xx),
'ymin': min(yy), 'ymax': max(yy)}
return d
</code></pre>
<pre><code>sage: f(x,y)=x**2+y**2
sage: G=implicit_plot(f==1,(x,-2,2),(y,-3,3))
sage: get_bounds_from_implicit_plot(G)
xx: [-0.99972791583529708, -0.99972791583529708, -0.99972791583529697, -0.99972791583529697, -0.99809541084708742, -0.99809541084708742,
-0.99809541084708719, -0.99809541084708719, -0.9948304008706681, -0.9948304008706681]
yy: [-0.99973154362416372, -0.99973154362416372, -0.99973154362416361, -0.99973154362416361, -0.99901565995525998, -0.99901565995525998,
-0.99901565995525976, -0.99901565995525976, -0.99758389261745239, -0.99758389261745239]
{'xmin': -0.99972791583529708, 'ymin': -0.99973154362416372,
'ymax': 0.99973154362415995, 'xmax': 0.99972791583530107}
</code></pre>
http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22189#post-id-22189For what it's worth I expect it to fail. :^) I was lazy and hard-coded the [1], so if the AxesSubplot isn't there it'll break, etc., etc. But the information should be in the Paths, so we can probably get at it if we need to. (There might also be some coordinate transformations we need to do, depending.) Hopefully the matplotlib guys can explain the One True Way(tm) to do the above!Sun, 06 Feb 2011 03:34:35 -0600http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22189#post-id-22189Comment by Laurent Claessens for <p>Following up on the comment discussion about which points are actually used in a plot, here's a revised version of the original answer I gave before kcrisman pointed out g.xy_data_array and I deleted mine as stupid by comparison. This is pretty ugly:</p>
<pre><code>
import matplotlib
def get_paths_from_plot(p):
# untested!
m = p.matplotlib()
sp = m.get_children()[1]
for c in sp.get_children():
# not sure if I need to test for both or not? don't understand
# matplotlib internals well enough to know if every Line2D
# will be in some LineCollection and so this is pure duplication (probably)
if isinstance(c, matplotlib.lines.Line2D):
yield c.get_path()
elif isinstance(c, matplotlib.collections.LineCollection):
for path in c.get_paths():
yield path
def get_bounds_from_implicit_plot(P):
# untested!
xx = []
yy = []
for path in get_paths_from_plot(P):
xx += list(path.vertices[:,0])
yy += list(path.vertices[:,1])
print 'xx:', sorted(xx)[:10]
print 'yy:', sorted(yy)[:10]
d = {'xmin': min(xx), 'xmax': max(xx),
'ymin': min(yy), 'ymax': max(yy)}
return d
</code></pre>
<pre><code>sage: f(x,y)=x**2+y**2
sage: G=implicit_plot(f==1,(x,-2,2),(y,-3,3))
sage: get_bounds_from_implicit_plot(G)
xx: [-0.99972791583529708, -0.99972791583529708, -0.99972791583529697, -0.99972791583529697, -0.99809541084708742, -0.99809541084708742,
-0.99809541084708719, -0.99809541084708719, -0.9948304008706681, -0.9948304008706681]
yy: [-0.99973154362416372, -0.99973154362416372, -0.99973154362416361, -0.99973154362416361, -0.99901565995525998, -0.99901565995525998,
-0.99901565995525976, -0.99901565995525976, -0.99758389261745239, -0.99758389261745239]
{'xmin': -0.99972791583529708, 'ymin': -0.99973154362416372,
'ymax': 0.99973154362415995, 'xmax': 0.99972791583530107}
</code></pre>
http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22190#post-id-22190Thanks a lot ! I'm going to test that on my favourites examples. For informations, I need these xmin,xmax,ymin,ymax in order to automatically determine the bounding box in a pspicture (LaTeX). See [here](http://student.ulb.ac.be/~lclaesse/phystricks-doc.pdf) in the section "Implicit curves"Sun, 06 Feb 2011 03:29:17 -0600http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22190#post-id-22190Answer by DSM for <p>This is a sequel of <a href="http://ask.sagemath.org/question/358/plot-the-level-sets-of-a-function">my question about plotting level set</a>.</p>
<p>In the following, G is a circle :</p>
<pre><code>sage: f(x,y)=x**2+y**2
sage: G=implicit_plot(f==1,(x,-2,2),(y,-3,3))
sage: G.get_minmax_data()
{'xmin': -2.0, 'ymin': -3.0, 'ymax': 3.0, 'xmax': 2.0}
</code></pre>
<p>The "correct" get_minmax_data sould be</p>
<pre><code>{'xmin': -1.0, 'ymin': -1.0, 'ymax': 1.0, 'xmax': 1.0}
</code></pre>
<p>As far as I understood the code (and the thread "Retrieving xy data from implicit plots" on Sage-support), the following is the relevant part :</p>
<pre><code>xy_data_arrays = numpy.asarray([[[func(x, y) for x in xsrange(*ranges[0],include_endpoint=True)]
for y in xsrange(*ranges[1], include_endpoint=True)]
for func in g],dtype=float)
</code></pre>
<p>in ../plot/contour_plot.py</p>
<p>My questions are :</p>
<ol>
<li><p>can I retrieve that xy_data_array ?</p></li>
<li><p>If I analyse xy_data_array, I suppose that extracting the point with lowest x-component such that the value is positive will provide me the "correct" xmin of the plot. I'm wrong ?</p></li>
</ol>
http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?answer=12062#post-id-12062I don't think you can retrieve it directly, although you can reconstruct it yourself, basically by computing it in exactly the way the code does it. (This should be improved, IMHO.)
.. well-- you <i>can</i> get at it, at least in a way, because the information is buried in the plot itself, but I wouldn't recommend it. Here's the sort of thing you'd have to do (note that I haven't bothered to check whether the data coordinates are right or anything of the sort, or to figure out whether every Line2D shows up in a LineCollection or not, so this is just a proof-of-concept rather than something that would actually work):
<pre><code>def get_paths_from_plot(p):
# untested!
m = p.matplotlib()
sp = m.get_children()[1]
for c in sp.get_children():
if isinstance(c, matplotlib.lines.Line2D):
yield c.get_path()
elif isinstance(c, matplotlib.collections.LineCollection):
for p in c.get_paths():
yield p
def get_bounds_from_implicit_plot(P):
# untested!
xx = []
yy = []
for path in get_paths_from_plot(P):
xx += list(path.vertices[:,0])
yy += list(path.vertices[:,1])
d = {'xmin': min(xx), 'xmax': max(xx),
'ymin': min(yy), 'ymax': max(yy)}
return d
sage: f(x,y)=x**2+y**2
sage: G=implicit_plot(f==1,(x,-2,2),(y,-3,3))
sage: get_bounds_from_implicit_plot(G)
{'xmin': -0.99972791583529708, 'ymin': -0.99973154362416372,
'ymax': 0.99973154362415995, 'xmax': 0.99972791583530107}
</code></pre>
You see the problem with this approach: you're not guaranteed to get 1 and -1 out.
There's a fundamental difference between what Sage knows or can compute about a function (since it probably has an expression that we can apply calculus to) and what the matplotlib-based tools know about a function (since it's basically a black box). I'm not sure what the right approach is for the general problem.
Fri, 04 Feb 2011 07:02:43 -0600http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?answer=12062#post-id-12062Comment by kcrisman for <p>I don't think you can retrieve it directly, although you can reconstruct it yourself, basically by computing it in exactly the way the code does it. (This should be improved, IMHO.)</p>
<p>.. well-- you <i>can</i> get at it, at least in a way, because the information is buried in the plot itself, but I wouldn't recommend it. Here's the sort of thing you'd have to do (note that I haven't bothered to check whether the data coordinates are right or anything of the sort, or to figure out whether every Line2D shows up in a LineCollection or not, so this is just a proof-of-concept rather than something that would actually work):</p>
<pre><code>def get_paths_from_plot(p):
# untested!
m = p.matplotlib()
sp = m.get_children()[1]
for c in sp.get_children():
if isinstance(c, matplotlib.lines.Line2D):
yield c.get_path()
elif isinstance(c, matplotlib.collections.LineCollection):
for p in c.get_paths():
yield p
def get_bounds_from_implicit_plot(P):
# untested!
xx = []
yy = []
for path in get_paths_from_plot(P):
xx += list(path.vertices[:,0])
yy += list(path.vertices[:,1])
d = {'xmin': min(xx), 'xmax': max(xx),
'ymin': min(yy), 'ymax': max(yy)}
return d
sage: f(x,y)=x**2+y**2
sage: G=implicit_plot(f==1,(x,-2,2),(y,-3,3))
sage: get_bounds_from_implicit_plot(G)
{'xmin': -0.99972791583529708, 'ymin': -0.99973154362416372,
'ymax': 0.99973154362415995, 'xmax': 0.99972791583530107}
</code></pre>
<p>You see the problem with this approach: you're not guaranteed to get 1 and -1 out. </p>
<p>There's a fundamental difference between what Sage knows or can compute about a function (since it probably has an expression that we can apply calculus to) and what the matplotlib-based tools know about a function (since it's basically a black box). I'm not sure what the right approach is for the general problem.</p>
http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22198#post-id-22198DSM, you and I must have about the same schedule or something - we keep responding at the same time!Fri, 04 Feb 2011 07:16:24 -0600http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22198#post-id-22198Answer by kcrisman for <p>This is a sequel of <a href="http://ask.sagemath.org/question/358/plot-the-level-sets-of-a-function">my question about plotting level set</a>.</p>
<p>In the following, G is a circle :</p>
<pre><code>sage: f(x,y)=x**2+y**2
sage: G=implicit_plot(f==1,(x,-2,2),(y,-3,3))
sage: G.get_minmax_data()
{'xmin': -2.0, 'ymin': -3.0, 'ymax': 3.0, 'xmax': 2.0}
</code></pre>
<p>The "correct" get_minmax_data sould be</p>
<pre><code>{'xmin': -1.0, 'ymin': -1.0, 'ymax': 1.0, 'xmax': 1.0}
</code></pre>
<p>As far as I understood the code (and the thread "Retrieving xy data from implicit plots" on Sage-support), the following is the relevant part :</p>
<pre><code>xy_data_arrays = numpy.asarray([[[func(x, y) for x in xsrange(*ranges[0],include_endpoint=True)]
for y in xsrange(*ranges[1], include_endpoint=True)]
for func in g],dtype=float)
</code></pre>
<p>in ../plot/contour_plot.py</p>
<p>My questions are :</p>
<ol>
<li><p>can I retrieve that xy_data_array ?</p></li>
<li><p>If I analyse xy_data_array, I suppose that extracting the point with lowest x-component such that the value is positive will provide me the "correct" xmin of the plot. I'm wrong ?</p></li>
</ol>
http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?answer=12063#post-id-12063Actually, the correct data is given. You specified `implicit_plot(f==1,(x,-2,2),(y,-3,3))`, so it gave you exactly those bounds. If you had done
sage: G=implicit_plot(f==1,(x,-1,1),(y,-1,1))
sage: G.get_minmax_data()
{'xmin': -1.0, 'ymin': -1.0, 'ymax': 1.0, 'xmax': 1.0}
you'd get what you expect.
What is going on here is that `implicit_plot` just creates a contour plot of the equation with only one contour level.
sage: G=contour_plot(f==1,(x,-1,1),(y,-1,1),contours=[0],fill=False); G
As to your question, this is the attribute (not method)
sage: g = G[0]
sage: g.xy_data_array
Which is large, as it's the values of the function at EVERY data point! Only the 'right' points are connected, using matplotlib's contour functionality. See this as well:
sage: sage.plot.contour_plot.ContourPlot??
class ContourPlot(GraphicPrimitive):
"""
Primitive class for the contour plot graphics type. See
``contour_plot?`` for help actually doing contour plots.
INPUT:
- ``xy_data_array`` - list of lists giving evaluated values of the function on the grid
I hope this helps! By the way, as long as you are in the Sage interpreter or notebook (or a .sage file), you can do `f(x,y)=x^2+y^2`; you only need `**` if you are writing a Python file.Fri, 04 Feb 2011 07:15:33 -0600http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?answer=12063#post-id-12063Comment by DSM for <p>Actually, the correct data is given. You specified <code>implicit_plot(f==1,(x,-2,2),(y,-3,3))</code>, so it gave you exactly those bounds. If you had done</p>
<pre><code>sage: G=implicit_plot(f==1,(x,-1,1),(y,-1,1))
sage: G.get_minmax_data()
{'xmin': -1.0, 'ymin': -1.0, 'ymax': 1.0, 'xmax': 1.0}
</code></pre>
<p>you'd get what you expect.</p>
<p>What is going on here is that <code>implicit_plot</code> just creates a contour plot of the equation with only one contour level. </p>
<pre><code>sage: G=contour_plot(f==1,(x,-1,1),(y,-1,1),contours=[0],fill=False); G
</code></pre>
<p>As to your question, this is the attribute (not method)</p>
<pre><code>sage: g = G[0]
sage: g.xy_data_array
</code></pre>
<p>Which is large, as it's the values of the function at EVERY data point! Only the 'right' points are connected, using matplotlib's contour functionality. See this as well:</p>
<pre><code>sage: sage.plot.contour_plot.ContourPlot??
class ContourPlot(GraphicPrimitive):
"""
Primitive class for the contour plot graphics type. See
``contour_plot?`` for help actually doing contour plots.
INPUT:
- ``xy_data_array`` - list of lists giving evaluated values of the function on the grid
</code></pre>
<p>I hope this helps! By the way, as long as you are in the Sage interpreter or notebook (or a .sage file), you can do <code>f(x,y)=x^2+y^2</code>; you only need <code>**</code> if you are writing a Python file.</p>
http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22197#post-id-22197+1 for a much better solution! I must have tried xy_data_array on G rather than on G[0] and gave up too soon. On the bright side, I got to search deep in matplotlib's data structures. :-pFri, 04 Feb 2011 13:34:08 -0600http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22197#post-id-22197Comment by kcrisman for <p>Actually, the correct data is given. You specified <code>implicit_plot(f==1,(x,-2,2),(y,-3,3))</code>, so it gave you exactly those bounds. If you had done</p>
<pre><code>sage: G=implicit_plot(f==1,(x,-1,1),(y,-1,1))
sage: G.get_minmax_data()
{'xmin': -1.0, 'ymin': -1.0, 'ymax': 1.0, 'xmax': 1.0}
</code></pre>
<p>you'd get what you expect.</p>
<p>What is going on here is that <code>implicit_plot</code> just creates a contour plot of the equation with only one contour level. </p>
<pre><code>sage: G=contour_plot(f==1,(x,-1,1),(y,-1,1),contours=[0],fill=False); G
</code></pre>
<p>As to your question, this is the attribute (not method)</p>
<pre><code>sage: g = G[0]
sage: g.xy_data_array
</code></pre>
<p>Which is large, as it's the values of the function at EVERY data point! Only the 'right' points are connected, using matplotlib's contour functionality. See this as well:</p>
<pre><code>sage: sage.plot.contour_plot.ContourPlot??
class ContourPlot(GraphicPrimitive):
"""
Primitive class for the contour plot graphics type. See
``contour_plot?`` for help actually doing contour plots.
INPUT:
- ``xy_data_array`` - list of lists giving evaluated values of the function on the grid
</code></pre>
<p>I hope this helps! By the way, as long as you are in the Sage interpreter or notebook (or a .sage file), you can do <code>f(x,y)=x^2+y^2</code>; you only need <code>**</code> if you are writing a Python file.</p>
http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22187#post-id-22187That's very helpful information. More than I would ever need from mpl, probably, but many people probably do need automatic detection of this sort of thing. Sun, 06 Feb 2011 13:35:06 -0600http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22187#post-id-22187Comment by DSM for <p>Actually, the correct data is given. You specified <code>implicit_plot(f==1,(x,-2,2),(y,-3,3))</code>, so it gave you exactly those bounds. If you had done</p>
<pre><code>sage: G=implicit_plot(f==1,(x,-1,1),(y,-1,1))
sage: G.get_minmax_data()
{'xmin': -1.0, 'ymin': -1.0, 'ymax': 1.0, 'xmax': 1.0}
</code></pre>
<p>you'd get what you expect.</p>
<p>What is going on here is that <code>implicit_plot</code> just creates a contour plot of the equation with only one contour level. </p>
<pre><code>sage: G=contour_plot(f==1,(x,-1,1),(y,-1,1),contours=[0],fill=False); G
</code></pre>
<p>As to your question, this is the attribute (not method)</p>
<pre><code>sage: g = G[0]
sage: g.xy_data_array
</code></pre>
<p>Which is large, as it's the values of the function at EVERY data point! Only the 'right' points are connected, using matplotlib's contour functionality. See this as well:</p>
<pre><code>sage: sage.plot.contour_plot.ContourPlot??
class ContourPlot(GraphicPrimitive):
"""
Primitive class for the contour plot graphics type. See
``contour_plot?`` for help actually doing contour plots.
INPUT:
- ``xy_data_array`` - list of lists giving evaluated values of the function on the grid
</code></pre>
<p>I hope this helps! By the way, as long as you are in the Sage interpreter or notebook (or a .sage file), you can do <code>f(x,y)=x^2+y^2</code>; you only need <code>**</code> if you are writing a Python file.</p>
http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22191#post-id-22191Oops -- it's a little deeper than I said. See my answer; it's in the AxesSubplot.Sun, 06 Feb 2011 02:51:55 -0600http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22191#post-id-22191Comment by Laurent Claessens for <p>Actually, the correct data is given. You specified <code>implicit_plot(f==1,(x,-2,2),(y,-3,3))</code>, so it gave you exactly those bounds. If you had done</p>
<pre><code>sage: G=implicit_plot(f==1,(x,-1,1),(y,-1,1))
sage: G.get_minmax_data()
{'xmin': -1.0, 'ymin': -1.0, 'ymax': 1.0, 'xmax': 1.0}
</code></pre>
<p>you'd get what you expect.</p>
<p>What is going on here is that <code>implicit_plot</code> just creates a contour plot of the equation with only one contour level. </p>
<pre><code>sage: G=contour_plot(f==1,(x,-1,1),(y,-1,1),contours=[0],fill=False); G
</code></pre>
<p>As to your question, this is the attribute (not method)</p>
<pre><code>sage: g = G[0]
sage: g.xy_data_array
</code></pre>
<p>Which is large, as it's the values of the function at EVERY data point! Only the 'right' points are connected, using matplotlib's contour functionality. See this as well:</p>
<pre><code>sage: sage.plot.contour_plot.ContourPlot??
class ContourPlot(GraphicPrimitive):
"""
Primitive class for the contour plot graphics type. See
``contour_plot?`` for help actually doing contour plots.
INPUT:
- ``xy_data_array`` - list of lists giving evaluated values of the function on the grid
</code></pre>
<p>I hope this helps! By the way, as long as you are in the Sage interpreter or notebook (or a .sage file), you can do <code>f(x,y)=x^2+y^2</code>; you only need <code>**</code> if you are writing a Python file.</p>
http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22192#post-id-22192G.matplotlib().get_children() returns
[<matplotlib.patches.Rectangle object at 0xc144e4c>, <matplotlib.axes.AxesSubplot object at 0xc14472c>]
Is the data in the Rectiangle ? In the Axes ? I sent the question to the matplotlib's support list.
Sun, 06 Feb 2011 01:29:32 -0600http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22192#post-id-22192Comment by Laurent Claessens for <p>Actually, the correct data is given. You specified <code>implicit_plot(f==1,(x,-2,2),(y,-3,3))</code>, so it gave you exactly those bounds. If you had done</p>
<pre><code>sage: G=implicit_plot(f==1,(x,-1,1),(y,-1,1))
sage: G.get_minmax_data()
{'xmin': -1.0, 'ymin': -1.0, 'ymax': 1.0, 'xmax': 1.0}
</code></pre>
<p>you'd get what you expect.</p>
<p>What is going on here is that <code>implicit_plot</code> just creates a contour plot of the equation with only one contour level. </p>
<pre><code>sage: G=contour_plot(f==1,(x,-1,1),(y,-1,1),contours=[0],fill=False); G
</code></pre>
<p>As to your question, this is the attribute (not method)</p>
<pre><code>sage: g = G[0]
sage: g.xy_data_array
</code></pre>
<p>Which is large, as it's the values of the function at EVERY data point! Only the 'right' points are connected, using matplotlib's contour functionality. See this as well:</p>
<pre><code>sage: sage.plot.contour_plot.ContourPlot??
class ContourPlot(GraphicPrimitive):
"""
Primitive class for the contour plot graphics type. See
``contour_plot?`` for help actually doing contour plots.
INPUT:
- ``xy_data_array`` - list of lists giving evaluated values of the function on the grid
</code></pre>
<p>I hope this helps! By the way, as long as you are in the Sage interpreter or notebook (or a .sage file), you can do <code>f(x,y)=x^2+y^2</code>; you only need <code>**</code> if you are writing a Python file.</p>
http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22196#post-id-22196Thank for the answer, kcrisman. I did the mistake DSM said :) Using G[0].xy_data_array and digging in "by hand", I obtain my result: the xmin,xmax,ymin,ymax of points that are actually plotted. Obviously I would be happier using the matplotlib's functionality ... but I got lost in the code of matplotlib/contour.py ... where can I get the list of "right" points ?Fri, 04 Feb 2011 23:06:45 -0600http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22196#post-id-22196Comment by DSM for <p>Actually, the correct data is given. You specified <code>implicit_plot(f==1,(x,-2,2),(y,-3,3))</code>, so it gave you exactly those bounds. If you had done</p>
<pre><code>sage: G=implicit_plot(f==1,(x,-1,1),(y,-1,1))
sage: G.get_minmax_data()
{'xmin': -1.0, 'ymin': -1.0, 'ymax': 1.0, 'xmax': 1.0}
</code></pre>
<p>you'd get what you expect.</p>
<p>What is going on here is that <code>implicit_plot</code> just creates a contour plot of the equation with only one contour level. </p>
<pre><code>sage: G=contour_plot(f==1,(x,-1,1),(y,-1,1),contours=[0],fill=False); G
</code></pre>
<p>As to your question, this is the attribute (not method)</p>
<pre><code>sage: g = G[0]
sage: g.xy_data_array
</code></pre>
<p>Which is large, as it's the values of the function at EVERY data point! Only the 'right' points are connected, using matplotlib's contour functionality. See this as well:</p>
<pre><code>sage: sage.plot.contour_plot.ContourPlot??
class ContourPlot(GraphicPrimitive):
"""
Primitive class for the contour plot graphics type. See
``contour_plot?`` for help actually doing contour plots.
INPUT:
- ``xy_data_array`` - list of lists giving evaluated values of the function on the grid
</code></pre>
<p>I hope this helps! By the way, as long as you are in the Sage interpreter or notebook (or a .sage file), you can do <code>f(x,y)=x^2+y^2</code>; you only need <code>**</code> if you are writing a Python file.</p>
http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22194#post-id-22194The "right" points are buried pretty deeply. G.matplotlib().get_children()[1] gives the lines etc. that make up the image, and Line2D (and LineCollection?) instances there have a method .get_path() (.get_paths()), and those Path objects returned have an attribute ".vertices" which lists the information.Sat, 05 Feb 2011 14:35:33 -0600http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22194#post-id-22194Comment by kcrisman for <p>Actually, the correct data is given. You specified <code>implicit_plot(f==1,(x,-2,2),(y,-3,3))</code>, so it gave you exactly those bounds. If you had done</p>
<pre><code>sage: G=implicit_plot(f==1,(x,-1,1),(y,-1,1))
sage: G.get_minmax_data()
{'xmin': -1.0, 'ymin': -1.0, 'ymax': 1.0, 'xmax': 1.0}
</code></pre>
<p>you'd get what you expect.</p>
<p>What is going on here is that <code>implicit_plot</code> just creates a contour plot of the equation with only one contour level. </p>
<pre><code>sage: G=contour_plot(f==1,(x,-1,1),(y,-1,1),contours=[0],fill=False); G
</code></pre>
<p>As to your question, this is the attribute (not method)</p>
<pre><code>sage: g = G[0]
sage: g.xy_data_array
</code></pre>
<p>Which is large, as it's the values of the function at EVERY data point! Only the 'right' points are connected, using matplotlib's contour functionality. See this as well:</p>
<pre><code>sage: sage.plot.contour_plot.ContourPlot??
class ContourPlot(GraphicPrimitive):
"""
Primitive class for the contour plot graphics type. See
``contour_plot?`` for help actually doing contour plots.
INPUT:
- ``xy_data_array`` - list of lists giving evaluated values of the function on the grid
</code></pre>
<p>I hope this helps! By the way, as long as you are in the Sage interpreter or notebook (or a .sage file), you can do <code>f(x,y)=x^2+y^2</code>; you only need <code>**</code> if you are writing a Python file.</p>
http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22195#post-id-22195Yes, the Graphics objects are lists - so it's annoying when trying to access the stuff, but great when (as usually is the case) you want them to 'just work'. I have no answer for the matplotlib question. Why not ask on their support list? If we can give easy access to it, that would be useful to do.Sat, 05 Feb 2011 14:28:48 -0600http://ask.sagemath.org/question/7920/get_minmax_data-on-implicit_plot/?comment=22195#post-id-22195