Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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:


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
        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])
    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
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}

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:


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 Line2D
        # will be in some LineCollection
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 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])
    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
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}

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:


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 p path in c.get_paths():
                yield p
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
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}