Ticket #14580: trac_14580-legend_color.patch

File trac_14580-legend_color.patch, 8.1 KB (added by ppurka, 6 years ago)

Apply to devel/sage

  • sage/plot/graphics.py

    # HG changeset patch
    # User Punarbasu Purkayastha <ppurka@gmail.com>
    # Date 1369136031 -28800
    # Node ID a2714dfdc18da0e4eafd0b843c65de04a1660252
    # Parent  59361f10b1b2dbd408504bbe0d8d1203ed52f87d
    add option to add individual color to legend texts
    
    diff --git a/sage/plot/graphics.py b/sage/plot/graphics.py
    a b  
    161161        self._bbox_extra_artists = []
    162162        self._extra_kwds = {}
    163163        self._fontsize = 10
     164        self._legend_colors = []
    164165        self._legend_opts = {}
    165166        self._objects = []
    166167        self._show_axes = True
     
    10371038        g._show_legend = self._show_legend or other._show_legend
    10381039        g._extra_kwds.update(self._extra_kwds)
    10391040        g._extra_kwds.update(other._extra_kwds)
     1041        g._legend_colors = self._legend_colors + other._legend_colors
    10401042        g._legend_opts.update(self._legend_opts)
    10411043        g._legend_opts.update(other._legend_opts)
    10421044        if self.aspect_ratio()=='automatic':
     
    22772279                # color
    22782280                lframe = leg.get_frame()
    22792281                lframe.set_facecolor(color)
    2280        
     2282                from sage.plot.colors import to_mpl_color
     2283                for txt,color in zip(leg.get_texts(), self._legend_colors):
     2284                    if color is not None:
     2285                        txt.set_color(to_mpl_color(color))
    22812286           
    22822287        subplot.set_xlim([xmin, xmax])
    22832288        subplot.set_ylim([ymin, ymax])
  • sage/plot/line.py

    diff --git a/sage/plot/line.py b/sage/plot/line.py
    a b  
    6060            sage: list(sorted(Line([-1,2], [17,4], {})._allowed_options().iteritems()))
    6161            [('alpha', 'How transparent the line is.'),
    6262             ('hue', 'The color given as a hue.'),
     63             ('legend_color', 'The color of the legend text.'),
    6364             ('legend_label', 'The label for this item in the legend.'),
    6465             ('linestyle',
    6566              "The style of the line, which is one of '--' (dashed), '-.' (dash dot), '-' (solid), 'steps', ':' (dotted)."),
     
    7273             ('thickness', 'How thick the line is.'),
    7374             ('zorder', 'The layer level in which to draw')]
    7475        """
    75         return {'alpha':'How transparent the line is.',
     76        return {'alpha':'How transparent the line is.',
     77                'legend_color':'The color of the legend text.',
    7678                'legend_label':'The label for this item in the legend.',
    77                'thickness':'How thick the line is.',
     79                'thickness':'How thick the line is.',
    7880                'rgbcolor':'The color as an RGB tuple.',
    7981                'hue':'The color given as a hue.',
    8082                'linestyle':"The style of the line, which is one of '--' (dashed), '-.' (dash dot), '-' (solid), 'steps', ':' (dotted).",
     
    8587                'markerfacecolor':'the color of the marker face',
    8688                'zorder':'The layer level in which to draw'
    8789                }
    88                
     90
    8991    def _plot3d_options(self, options=None):
    9092        """
    9193        Translate 2D plot options into 3D plot options.
     
    240242        """
    241243        import matplotlib.lines as lines
    242244        options = dict(self.options())
    243         del options['alpha']
    244         del options['thickness']
    245         del options['rgbcolor']
    246         del options['legend_label']
    247         if 'linestyle' in options:
    248             del options['linestyle']
     245        for o in ('alpha', 'legend_color', 'legend_label', 'linestyle',
     246                  'rgbcolor', 'thickness'):
     247            if o in options:
     248                del options[o]
    249249        p = lines.Line2D(self.xdata, self.ydata, **options)
    250250        options = self.options()
    251251        a = float(options['alpha'])
     
    283283        from sage.plot.plot3d.shapes2 import line3d
    284284        return line3d(points, **kwds)
    285285
    286    
     286
    287287@rename_keyword(color='rgbcolor')
    288 @options(alpha=1, rgbcolor=(0,0,1), thickness=1, legend_label=None, aspect_ratio ='automatic')
     288@options(alpha=1, rgbcolor=(0,0,1), thickness=1, legend_label=None,
     289         legend_color=None, aspect_ratio ='automatic')
    289290def line2d(points, **options):
    290291    r"""
    291292    Create the line through the given list of points.
     
    304305
    305306    - ``hue`` -- The color given as a hue
    306307
     308    - ``legend_color`` -- The color of the text in the legend
     309
    307310    - ``legend_label`` -- the label for this item in the legend
    308311
     312
    309313    Any MATPLOTLIB line option may also be passed in.  E.g.,
    310314
    311315    - ``linestyle`` - (default: "-") The style of the line, which is one of
     
    357361
    358362        sage: line([(0,0),(1,1)], legend_label='line')
    359363
     364    Lines with different colors in the legend text::
     365
     366        sage: p1 = line([(0,0),(1,1)], legend_label='line')
     367        sage: p2 = line([(1,1),(2,4)], legend_label='squared', legend_color='red')
     368        sage: p1 + p2
     369
    360370    Extra options will get passed on to show(), as long as they are valid::
    361371
    362372        sage: line([(0,1), (3,4)], figsize=[10, 2])
     
    458468    g.add_primitive(Line(xdata, ydata, options))
    459469    if options['legend_label']:
    460470        g.legend(True)
     471        g._legend_colors = [options['legend_color']]
    461472    return g
  • sage/plot/plot.py

    diff --git a/sage/plot/plot.py b/sage/plot/plot.py
    a b  
    647647    - ``detect_poles`` - (Default: False) If set to True poles are detected.
    648648      If set to "show" vertical asymptotes are drawn.
    649649
     650    - ``legend_color`` - the color of the text for this item in the legend
     651
    650652    - ``legend_label`` - the label for this item in the legend
    651653
    652654    .. note::
     
    839841        sage: plot(sin, legend_label='$sin$')
    840842        sage: plot(sin, legend_label='$\sin$')
    841843
     844    It is possible to use a different color for the text of each label::
     845
     846        sage: p1 = plot(sin, legend_label='sin', legend_color='red')
     847        sage: p2 = plot(cos, legend_label='cos', legend_color='green')
     848        sage: p1 + p2
     849
    842850    Note that the independent variable may be omitted if there is no
    843851    ambiguity::
    844852
  • sage/plot/point.py

    diff --git a/sage/plot/point.py b/sage/plot/point.py
    a b  
    9090            'How big the point is (i.e., area in points^2=(1/72 inch)^2).'
    9191        """
    9292        return {'alpha':'How transparent the point is.',
     93                'legend_color':'The color of the legend text',
    9394                'legend_label':'The label for this item in the legend.',
    9495                'size': 'How big the point is (i.e., area in points^2=(1/72 inch)^2).',
    9596                'faceted': 'If True color the edge of the point.',
     
    312313        return point3d(points, **kwds)
    313314
    314315@rename_keyword(color='rgbcolor', pointsize='size')
    315 @options(alpha=1, size=10, faceted=False, rgbcolor=(0,0,1), legend_label=None, aspect_ratio='automatic')
     316@options(alpha=1, size=10, faceted=False, rgbcolor=(0,0,1),
     317         legend_color=None, legend_label=None, aspect_ratio='automatic')
    316318def point2d(points, **options):
    317319    r"""
    318320    A point of size ``size`` defined by point = `(x,y)`.
     
    382384    g.add_primitive(Point(xdata, ydata, options))
    383385    if options['legend_label']:
    384386        g.legend(True)
     387        g._legend_colors = [options['legend_color']]
    385388    return g
    386389
    387390points = point
  • sage/plot/primitive.py

    diff --git a/sage/plot/primitive.py b/sage/plot/primitive.py
    a b  
    106106        if 'alpha' in options:
    107107            options_3d['opacity'] = options['alpha']
    108108            del options['alpha']
    109         if 'legend_label' in options:
    110             del options['legend_label'] # no equivalent in 3d for now           
    111         if 'zorder' in options:
    112             del options['zorder']
     109
     110        for o in ('legend_color', 'legend_label', 'zorder'):
     111            if o in options:
     112                del options[o]
     113
    113114        if len(options) != 0:
    114             raise NotImplementedError, "Unknown plot3d equivalent for %s" % ", ".join(options.keys())
     115            raise NotImplementedError("Unknown plot3d equivalent for {}".format(
     116                                      ", ".join(options.keys())))
    115117        return options_3d
    116    
     118
    117119    def set_zorder(self, zorder):
    118120        """
    119121        Set the layer in which to draw the object.