1

I would like to plot a 3xN matrix to obtain three lines, specifying the colors for each one. I can do this with three separate calls to plot():

fig = plt.figure()
theta = np.arange(0,1,1.0/720) * 2 * np.pi
abc = np.cos(np.vstack([theta, theta+2*np.pi/3, theta+4*np.pi/3])).T
ax = fig.add_subplot(1,1,1)
ax.plot(theta,abc[:,0],color='red')
ax.plot(theta,abc[:,1],color='green')
ax.plot(theta,abc[:,2],color='blue')

enter image description here

Is there a way to do the same thing, specifying colors with one call to plot()?

ax.plot(theta,abc,????)

I've tried

ax.plot(theta,abc,color=['red','green','blue'])

but I get ValueError: could not convert string to float: red in a callback.

2 Answers 2

1

An option is to change the color cycle from which colors are chosen automatically, such that it contains those colors needed for the plot.

ax.set_prop_cycle('color', ["red", "green","blue"]) 
ax.plot(theta,abc)

This can also be done using rcParams, see e.g. here.

Complete example:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
theta = np.arange(0,1,1.0/720) * 2 * np.pi
abc = np.cos(np.vstack([theta, theta+2*np.pi/3, theta+4*np.pi/3])).T
ax = fig.add_subplot(1,1,1)

ax.set_prop_cycle('color', ["red", "green","blue"]) 
ax.plot(theta,abc)

plt.show()
Sign up to request clarification or add additional context in comments.

2 Comments

Where is the index in the color cycle stored? In my real application I'm going to be making several plot() calls to one axis, and I need to control the color to all of the plotted lines.
It doesn't have any index. But you may reset it by calling the line ax.set_prop_cycle('color', ["red", "green","blue"]) again - after that it will restart at "red". Is that what you mean?
1

As far as I know, that's not possible. According to the documentation the color= argument must be a single color (or an array of 3-4 floats). Is there a particular reason you need to do only one call?

If it must be a one-liner, you could do the following:

ax.plot(theta,abc[:,0],color='red',theta,abc[:,1],color='green',theta,abc[:,2],color='blue')

which is one line, but does not really save on typing.

Or:

for i,c in enumerate(['red','green','blue']):
    ax.plot(theta,abc[:,i],color=c)

EDIT

Actually, I thought of one way to do this. If you simply call plot(theta, abc[:,:]), the colors will be cycled through the list of colors in the rcParams axes.prop_cycle.

Therefore, you could (temporarily) change the color cycle to red,green,blue before calling your plot command.

from cycler import cycler

#save the current color cycler
old = matplotlib.rcParams['axes.prop_cycle']

#set the color cycler to r,g,b
matplotlib.rcParams['axes.prop_cycle'] = cycler(color=['r','g','b'])

fig = plt.figure()
theta = np.arange(0,1,1.0/720) * 2 * np.pi
abc = np.cos(np.vstack([theta, theta+2*np.pi/3, theta+4*np.pi/3])).T
ax = fig.add_subplot(1,1,1)
ax.plot(theta,abc)  # <----- single call to `plot` for the 3 lines

#restore original color cycle
matplotlib.rcParams['axes.prop_cycle'] = old

1 Comment

" Is there a particular reason you need to do only one call?" -- for clarity; it's one unified set of data and simpler to read the source code.

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.