Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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!

share|improve this question

marked as duplicate by tcaswell, oefe, zero323, devnull, Oz123 yesterday

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

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.

share|improve this answer

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)   
share|improve this answer
 
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 yesterday
 
Sure, see edit. –  CT Zhu yesterday
 
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 yesterday
 
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 yesterday
 
Thanks for the help, its working now! Cheers! –  user2739143 13 hours ago

Not the answer you're looking for? Browse other questions tagged or ask your own question.