Maverick Meerkat
Maverick Meerkat

Reputation: 6404

Change colors for lines, when passing a matrix as output variable in python matplotlib

I have an x vector of size n, and an (n,p) mu matrix. When I place it in pyplot, I get:

plt.plot(x_train_inv, y_train_inv, 'go', alpha=0.5, markerfacecolor='none')
plt.plot(x, mu)

enter image description here

The green line is a not seen very well with the green dots, and I would like to change it's color. Is it possible in this set up? (where the "y" is a matrix) If so - how?

EDIT - a reproducible example:

x  = np.linspace(-1, 1, 100)
y = np.c_[x**2, np.cos(x), np.exp(x)]
plt.plot(x, y)

enter image description here

I wish to pass something to the last line to specify the color per column in y, e.g.: plt.plot(x, y, color)

Upvotes: 2

Views: 449

Answers (2)

n1colas.m
n1colas.m

Reputation: 3989

You can use set_color.

import matplotlib.pyplot as plt
import numpy as np

COLORS = ['#ff5555', '#00ffaa', '#9900ff']

x  = np.linspace(-1, 1, 100)
y = np.c_[x**2, np.cos(x), np.exp(x)]

line = plt.plot(x, y)
for idx, color in enumerate(COLORS):
    line[idx].set_color(color)
plt.show()

matrix_lines

Upvotes: 2

Iaggo Capitanio
Iaggo Capitanio

Reputation: 138

Yes, it's possible:

import matplotlib.pyplot as plt 
import numpy

x  = numpy.linspace(-1, 1, 100)
y = x**2
mu = numpy.random.normal(0,1, 200)
mu = mu.reshape((len(x), 2))
# plt.plot(x_train_inv, y_train_inv, 'go', alpha=0.5, markerfacecolor='none')
plt.plot(x, mu, 'go')
plt.plot(x, y, "r--")

Upvotes: 0

Related Questions