Opened 6 years ago

Last modified 6 years ago

#14580 closed enhancement

Set individual legend font color in Sage — at Version 9

Reported by: ppurka Owned by: jason, was
Priority: major Milestone: sage-5.12
Component: graphics Keywords:
Cc: sd45, sd48 Merged in:
Authors: Punarbasu Purkayastha, Karl-Dieter Crisman Reviewers: Karl-Dieter Crisman
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by kcrisman)

Currently, it is not possible to change the legend font color in Sage at all. This link shows how to do it in matplotlib - it is not straightforward in matplotlib either.

I think the way to introduce it is

  1. Allow a legend_color parameter to each plot.
  2. Keep track of the individual legend colors via a key in the Graphics._legend_opts dictionary.
  3. The key will contain a list of colors, defaulting to black or None (I prefer None, so that we use matplotlib's default).
  4. After all the plots have been rendered and the legend as been set, we can follow the method in the link above to extract all the texts and set the colors individually, skipping the ones which are None (if the default is None).

Apply trac_14580-legend_color.patch and trac_14580-more-colors.patch to devel/sage.

Change History (10)

comment:1 Changed 6 years ago by ppurka

  • Description modified (diff)
  • Status changed from new to needs_review

I added a new attribute self._legend_colors = [] to Graphics. This will hold the colors or None in case no colors are provided. Patch needs review.

Edit: It is now possible to get a graph like this:

sage: p1 = plot(x, legend_label='this is x', legend_color='red', color='red')
sage: p2 = plot(x**2, legend_label='this is x^2', legend_color='green', color='green')
sage: (p1+p2).save('/tmp/a.png')
Last edited 6 years ago by ppurka (previous) (diff)

comment:2 Changed 6 years ago by ppurka

  • Status changed from needs_review to needs_work
  • Work issues set to fix doctests

comment:3 Changed 6 years ago by ppurka

  • Status changed from needs_work to needs_review
  • Work issues fix doctests deleted

comment:4 Changed 6 years ago by kcrisman

  • Authors set to Punarbasu Purkayastha

comment:5 Changed 6 years ago by kcrisman

I think this is very intriguing. I spent a lot of time thinking about why the existing legend suboptions decorator might not work, and how this code magically allows examples to work in different orders, and I think this is really correct. So this is my only question:

  • Tons of other graphics primitives have legends (arrow, disk, etc.), which do not use point or line. Presumably we should support those too?
Last edited 6 years ago by kcrisman (previous) (diff)

comment:6 Changed 6 years ago by kcrisman

$ grep -r p.set_label sage/plot/
sage/plot/arrow.py:        p.set_label(options['legend_label'])
sage/plot/arrow.py:        p.set_label(options['legend_label'])
sage/plot/circle.py:        p.set_label(options['legend_label'])
sage/plot/disk.py:        p.set_label(options['legend_label'])
sage/plot/ellipse.py:        p.set_label(options['legend_label'])
sage/plot/line.py:        p.set_label(options['legend_label'])
sage/plot/polygon.py:        p.set_label(options['legend_label'])

comment:7 Changed 6 years ago by kcrisman

  • Cc sd45 added
  • Reviewers set to Karl-Dieter Crisman

comment:8 Changed 6 years ago by kcrisman

There is also not a doctest for the point version yet. I'll try to write a second patch now, which would definitely need review.

Changed 6 years ago by kcrisman

comment:9 Changed 6 years ago by kcrisman

  • Authors changed from Punarbasu Purkayastha to Punarbasu Purkayastha, Karl-Dieter Crisman
  • Cc sd48 added
  • Description modified (diff)

Patchbot, apply trac_14580-legend_color.patch and trac_14580-more-colors.patch

Note: See TracTickets for help on using tickets.