13

I plot a simple line plot with matplotlib/pyplot. In the same plot, there are up to 20 lines. Matplotlib automatically chooses a line color. I need to draw the first of the lines in a thicker red.

So i tried:

if (i==0):                       # first line
    plt.plot(x, y, 'r', lw=2.5)
else:                            # other lines
    plt.plot(x, y)

So far very simple. First line is correct. But one of the other lines is red too. How can avoid this?

2 Answers 2

31

The default jet color map goes from blue to red. Every builtin colormap also has a sister color map with the colors going in reverse order. The reversed color map has the same name with the suffix _r. Thus, jet_r goes from red to blue.

So you can give each line a unique color from the jet_r colormap by defining

color = cmap(float(i)/N)

where cmap is the jet_r colormap. The colormap cmap is a callable. When passed a float, it returns a RGB tuple. Thus, if float(i)/N is a distinct float between 0 and 1 for each line, then each line will automatically get a distinct color.


For example,

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 5, 100)
cmap = plt.get_cmap('jet_r')
N = 20
for i, n in enumerate(np.linspace(2.0, 0, N)):
    y = np.sin(x)*x**n
    color = cmap(float(i)/N)
    lw = 2.5 if i==0 else 1
    # zorder controls the order in which the lines are drawn. 
    # The line with the highest zorder lies on top.
    plt.plot(x, y, c=color, lw=lw, zorder=-i)
plt.show()

yields

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

One up for that eye pleasing plot
What is N in this line? color = cmap(float(i)/N)
N is the number of lines. It's 20 in the plot above.
2

There are lots of ways you could potentially do this. Probably the simplest and most obvious would be to specify separate line colors for all 20 lines:

line_colors = ['r', 'g', 'b', 'c', 'm', 'y', 'k', ...]

for ii in range(20):
    plt.plot(x, y, c=line_colors[ii])

Of course it would a bit of a pain to have to type out 20 different color strings!

You could also generate the colors by drawing an array of RGBA values from a colormap as in unutbu's answer, but most of the built-in colormaps contain red, so you'd have to either pick a colormap that doesn't contain red, design your own, or pick a range of float values between 0 and 1 that did not map to the red region of the colormap.

If you don't mind some colors being repeated, one potentially nicer option would be to use itertools.cycle to create a generator object that yields colors in a repeating cycle:

from itertools import cycle

color_cycle = cycle(['g', 'b', 'c', 'm', 'y', 'k'])

for ii in range(20):
    if ii == 0:
        plt.plot(x, y, c='r')
    else:
        plt.plot(x, y, c=cycle.next())

In fact, this is exactly how the default matplotlib color cycle works. Each axis object contains an itertools.cycle which sequentially changes the default color for each line that is drawn on that axis:

ax = plt.axes()

ax_color_cycle = ax._get_lines.color_cycle

print(ax_color_cycle)
# <itertools.cycle object at 0x7fdb91efae60>

print(ax_color_cycle.next())
# b

print(ax_color_cycle.next())
# g

print(ax_color_cycle.next())
# r

# etc.

5 Comments

So maybe a chance to start the itertools.cycle with the color after red!? Do you know how many colors are in the set, before the cycle starts again?
You mean the default color cycle? You can test this yourself by doing [ax_color_cycle.next() for ii in xrange(10)] - you'll see that it gives you ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'b', 'g', 'r'], i.e. it repeats every 8 lines.
ok, thx. this is in my case unfavorable. I need more then a colorbar.
I'm not sure what you mean by a 'colorbar', but you can create your own color cycle as long or as short as you like by varying the number of strings in the list you pass to cycle. As well as strings you could also give it RGBA tuples or hex values (see here for other ways to specify colors in matplotlib).
I think _get_lines.color_cycle is no longer there matplotlib.org/api/prev_api_changes/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.