Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing colour scheme of python matplotlib python plots [duplicate]

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!

like image 709
user2739143 Avatar asked Nov 16 '13 16:11

user2739143


People also ask

How do you change the color scheme in Python?

Press Ctrl+Alt+S to open the IDE settings and select Editor | Color Scheme | Python. Select any code element you want to customize and clear the corresponding Inherit values from checkbox to change inherited color settings for this element; then specify your color and font settings.

How do you update the same plot in matplotlib?

We can use matplotlib to update a plot on every iteration during the loop. With the help of matplotlib. pyplot. draw() function we can update the plot on the same figure during the loop.

Does PLT Savefig overwrite?

As Date Created doesn't change when matplotlib . savefig overwrites a file the Date column never changes.


2 Answers

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)   
like image 137
CT Zhu Avatar answered Sep 30 '22 20:09

CT Zhu


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.

like image 45
Lukas Graf Avatar answered Sep 30 '22 19:09

Lukas Graf