From the matplotlib documentation I know I can use a set of colors in the colors argument of vlines like colors=['green', 'yellow', 'red'] etc, but then the colors simply rotate i.e: the first line will be green, the second yellow, the third red, 4th green, 5th yellow and so on.
Is there a way to access one line individually and change its color?
CodePudding user response:
You can plot that particular line separately:
ax.vlines([1,2,4,5], 0, 1, colors='red')
ax.vlines(3, 0, 1, colors='blue')
Or you just create a colors list with as many colors as you have lines.
CodePudding user response:
Via get_colors() you get the current colors. If there are less colors, then lines, the colors get repeated. To change the color of a particular line, the full array of colors needs to be created, after which particular colors can be altered.
The following example changes the 11th line:
import matplotlib.pyplot as plt
from matplotlib.colors import to_rgba
import numpy as np
lines = plt.vlines(np.arange(16), np.random.rand(16), np.random.rand(16) 2, colors=['r', 'g', 'b'], lw=3)
colors = lines.get_colors()
num_lines = len(lines.get_paths())
new_colors = np.tile(colors, (num_lines // len(colors) 1, 1))
new_colors[10] = to_rgba('orange')
lines.set_colors(new_colors)
plt.show()

