5

I want to set a colour scheme for my python plots, so that they don't repeat the same colour like they are for A and H in the top plot shown below. (sorry if its difficult to see). enter image description here

The code I'm using is simply,

ax1.plot(sections,A,label='A',linewidth=2) ax1.plot(sections,B,label='B',linewidth=2) ax1.plot(sections,C,label='C',linewidth=2) ax1.plot(sections,D,label='D',linewidth=2) ax1.plot(sections,E,label='E',linewidth=2) ax1.plot(sections,F,label='F',linewidth=2) ax1.plot(sections,G,label='G',linewidth=2) ax1.plot(sections,H,label='H',linewidth=2)

What's the best way to set the colour scheme? Is using the colourmap function? Thanks for any help!

user2739143
  • 591
  • 2
  • 8
  • 13

2 Answers2

4

You can use some colormaps to color your lines, example (here I use the 'jet' colormap):

>>> from matplotlib import cm
>>> for i, val in enumerate(cm.jet(linspace(0,1,10))): #different lines
    plt.plot(arange(10), arange(10)+i, color=val, linestyle='-')

enter image description here

To do this in your existing code with minimal changes, just add (And don't forget to change 10 to the number of plots that you have.):

for L, C in zip([item for item in ax.get_children() if isinstance(item, matplotlib.lines.Line2D)], cm.jet(linspace(0,1,10))):
    L.set_color(C)   
CT Zhu
  • 52,648
  • 17
  • 120
  • 133
  • Thanks for the help, but I'm struggling to understand how to use this in the script I have written. Is there a way I could just say something along the lines of ax1.cm.ScalarMappable(cmap='jet') – user2739143 Nov 17 '13 at 12:48
  • I'm getting an error saying "NameError: name 'matplotlib' is not defined" in your code. I have defined matplotlib earlier using import "matplotlib.pyplot as plt" and "from matplotlib import *" is there something wrong with that? – user2739143 Nov 17 '13 at 15:20
  • Yeah, add `import matplotlib`, of course, :-). But since you have `from matplotlib import *` already, you just need `lines.Line2D` instead of `matplotlib.lines.Line2D` – CT Zhu Nov 17 '13 at 15:33
  • What represents the values that cm.jet(x) returns (where x is any number)? – iam_agf Apr 02 '15 at 17:35
  • It returns a list of RGBA color values, in float point numbers from 0 to 1. If `x` is larger than 1, it returns color 'more blue' than `[0, 0, 0.5, 1]`; if `x` is smaller than 0, returns a color 'more red' than `[0.5, 0, 0, 1]`. Not very easy to see, that is the reason you want to provide it with a `x` within 0 and 1. – CT Zhu Apr 03 '15 at 15:41
2

You can change the color map for a specific axes by using the set_color_cycle() method.

This is a simplified version of the color cycle demo in the matplotlib docs:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi)
offsets = np.linspace(0, 2*np.pi, 4, endpoint=False)
yy = np.transpose([np.sin(x + phi) for phi in offsets])

plt.rc('lines', linewidth=4)

ax = plt.gca()

# Set color cycle for this axes 
ax.set_color_cycle(['c', 'm', 'y', 'k'])

ax.plot(yy)

plt.show()

CMYK example

The other option is to change the global default color map. You can do this by either creating a matplotlibrc file and setting axes.color_cyle there, or by chaning the running configuration at runtime:

import matplotlib
matplotlib.rcParams['axes.color_cycle'] = ['r', 'g', 'b']

You can also use HTML hex notation for specifying colors:

ax.set_color_cycle(['#FF0000', '#00FF00', '#0000FF'])

For more information on how to specify colors, see the documentation of the `color``module.

The matplotlib.cm module is also useful, for example for registering your own color maps.

Lukas Graf
  • 30,317
  • 8
  • 77
  • 92