Partial coloring of text #697

Closed
gsever opened this Issue Feb 7, 2012 · 5 comments

Comments

Projects
None yet
4 participants

gsever commented Feb 7, 2012

It would be nice to have partial coloring of the text strings.

Example:

plt.ylabel("Sun is shining.")

Show "Sun" as red, "is" as green and "shining." as blue.

Possible use case: separate coloring can be used as legend for plots.

Member

ivanov commented Feb 7, 2012

here's the interactive version (same one I posted to the list)

import matplotlib.pyplot as plt
from matplotlib import transforms

def rainbow_text(x,y,ls,lc,**kw):
    """
    Take a list of strings ``ls`` and colors ``lc`` and place them next to each
    other, with text ls[i] being shown in color lc[i].

    This example shows how to do both vertical and horizontal text, and will
    pass all keyword arguments to plt.text, so you can set the font size,
    family, etc.
    """
    t = plt.gca().transData
    fig = plt.gcf()
    plt.show()

    #horizontal version
    for s,c in zip(ls,lc):
        text = plt.text(x,y," "+s+" ",color=c, transform=t, **kw)
        text.draw(fig.canvas.get_renderer())
        ex = text.get_window_extent()
        t = transforms.offset_copy(text._transform, x=ex.width, units='dots')

    #vertical version
    for s,c in zip(ls,lc):
        text = plt.text(x,y," "+s+" ",color=c, transform=t,
                rotation=90,va='bottom',ha='center',**kw)
        text.draw(fig.canvas.get_renderer())
        ex = text.get_window_extent()
        t = transforms.offset_copy(text._transform, y=ex.height, units='dots')


plt.figure()
rainbow_text(0.0,0.0,"all unicorns poop rainbows ! ! !".split(), 
        ['red', 'orange', 'brown', 'green', 'blue', 'purple', 'black'],
        size=30)

all unicorns poop rainbows

gsever commented Feb 8, 2012

I liked this solution Paul. It would be nice this is checked into the code as a helper function.

Member

pelson commented Sep 2, 2012

Yeah, nice example @ivanov. Would you mind adding this to the gallery?

Owner

mdboom commented Oct 30, 2013

Yeah, let's get this in the gallery, as it's a MFAQ. (moderately frequently asked question).

Member

ivanov commented Oct 30, 2013

Michael Droettboom, on 2013-10-30 07:43, wrote:

Yeah, let's get this in the gallery, as it's a MFAQ. (moderately frequently asked question).

I actually made a branch for this at SciPy'13 but never sent it it.

PR incoming in ~20 minutes

ivanov added a commit to ivanov/matplotlib that referenced this issue Oct 30, 2013

ivanov added a commit to ivanov/matplotlib that referenced this issue Oct 30, 2013

adding Mike Jacobsen image credit, permission
Date: Tue, 02 Jul 2013 09:12:22 +1000
From: Mike Jacobsen <mikej01@tpg...>
To: Paul Ivanov <pi@berkeley...>
Subject: Re: Pooping unicorn in an open source project

Hi Paul,

Yep you're welcome to use it. And thanks for asking first - I appreciate it.
If you wanted to credit me you could perhaps use my blog address
seemikedraw.com.au

Cheers
Mike.

On 1/7/13 6:29 PM, "Paul Ivanov" <pi@berkeley...> wrote:

> Hi Mike,
>
> I hope this email finds you well.
>
> I was wondering if you would grant matplotlib, an open-source
> project for doing scientific plots, to which I'm a contributor
> to, permission to use your Unicorn Poop cartoon in our examples
> gallery.
>
> We occasionally have the opportunity to spice up our examples,
> and in response to a request for rainbow coloring of text, I
> threw this together:
>
> matplotlib#697
>
> I was recently asked again to include this example in our
> examples gallery, and it was suggested that an actual image of a
> unicorn would be awesome. I did some google image searches, and
> yours was the subtlest, most smug unicorn I could find, so it
> would fit in nicely. We would attribute the image to you in
> whatever manner you see fit.
>
> You can see how we present our plots in our gallery (click any
> example to see the source code for the figure, as well as a
> larger version of it).
>
> http://matplotlib.org/gallery.html

ivanov added a commit to ivanov/matplotlib that referenced this issue Oct 30, 2013

ivanov added a commit to ivanov/matplotlib that referenced this issue Oct 30, 2013

adding Mike Jacobsen image credit, permission
Date: Tue, 02 Jul 2013 09:12:22 +1000
From: Mike Jacobsen <mikej01@tpg...>
To: Paul Ivanov <pi@berkeley...>
Subject: Re: Pooping unicorn in an open source project

Hi Paul,

Yep you're welcome to use it. And thanks for asking first - I appreciate it.
If you wanted to credit me you could perhaps use my blog address
seemikedraw.com.au

Cheers
Mike.

On 1/7/13 6:29 PM, "Paul Ivanov" <pi@berkeley...> wrote:

> Hi Mike,
>
> I hope this email finds you well.
>
> I was wondering if you would grant matplotlib, an open-source
> project for doing scientific plots, to which I'm a contributor
> to, permission to use your Unicorn Poop cartoon in our examples
> gallery.
>
> We occasionally have the opportunity to spice up our examples,
> and in response to a request for rainbow coloring of text, I
> threw this together:
>
> matplotlib#697
>
> I was recently asked again to include this example in our
> examples gallery, and it was suggested that an actual image of a
> unicorn would be awesome. I did some google image searches, and
> yours was the subtlest, most smug unicorn I could find, so it
> would fit in nicely. We would attribute the image to you in
> whatever manner you see fit.
>
> You can see how we present our plots in our gallery (click any
> example to see the source code for the figure, as well as a
> larger version of it).
>
> http://matplotlib.org/gallery.html

ivanov added a commit to ivanov/matplotlib that referenced this issue Oct 30, 2013

ivanov added a commit to ivanov/matplotlib that referenced this issue Oct 30, 2013

adding Mike Jacobsen image credit, permission
Date: Tue, 02 Jul 2013 09:12:22 +1000
From: Mike Jacobsen <mikej01@tpg...>
To: Paul Ivanov <pi@berkeley...>
Subject: Re: Pooping unicorn in an open source project

Hi Paul,

Yep you're welcome to use it. And thanks for asking first - I appreciate it.
If you wanted to credit me you could perhaps use my blog address
seemikedraw.com.au

Cheers
Mike.

On 1/7/13 6:29 PM, "Paul Ivanov" <pi@berkeley...> wrote:

> Hi Mike,
>
> I hope this email finds you well.
>
> I was wondering if you would grant matplotlib, an open-source
> project for doing scientific plots, to which I'm a contributor
> to, permission to use your Unicorn Poop cartoon in our examples
> gallery.
>
> We occasionally have the opportunity to spice up our examples,
> and in response to a request for rainbow coloring of text, I
> threw this together:
>
> matplotlib#697
>
> I was recently asked again to include this example in our
> examples gallery, and it was suggested that an actual image of a
> unicorn would be awesome. I did some google image searches, and
> yours was the subtlest, most smug unicorn I could find, so it
> would fit in nicely. We would attribute the image to you in
> whatever manner you see fit.
>
> You can see how we present our plots in our gallery (click any
> example to see the source code for the figure, as well as a
> larger version of it).
>
> http://matplotlib.org/gallery.html

@ivanov ivanov closed this in 009073b Nov 21, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment