Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set axvlines to use the same colors from the axes.color_cycle in matplotlib?

I have set the following colors in the axes.color_cycle of matplotlibrc file. It works fine when I plot lines and step. But when I use it to plot vertical lines with axvline it does not use the set colors as shown in figure:

enter image description here

It uses matplotlib's default colors. How to make axvline continue the color cycle?

Reproducible code

import numpy as np
import matplotlib.pyplot as plt
import itertools

m = 5
n = 5

x = np.zeros(shape=(m, n))
plt.figure(figsize=(5.15, 5.15))
plt.clf()
plt.subplot(111)
marker = itertools.cycle(('o', 'v', '^', '<', '>', 's', '8', 'p'))
ax = plt.gca()
for i in range(1, n):
    x = np.dot(i, [1, 1.1, 1.2, 1.3])
    y = x ** 2
    color = next(ax._get_lines.color_cycle)
    plt.plot(x, y, linestyle='', markeredgecolor='none', marker=marker.next(), color=color, label = str(i))
    plt.plot(x, y, linestyle='-', color = color)
plt.axvline(2.)
plt.axvline(3.)
plt.axhline(4.)
plt.axhline(6.)
plt.ylabel(r'y', labelpad=6)
plt.xlabel(r'x', labelpad=6)
plt.show()
like image 245
Tom Kurushingal Avatar asked Aug 10 '15 08:08

Tom Kurushingal


1 Answers

You can assign a color to your vertical lines as well:

vline_color = next(ax._get_lines.color_cycle)

plt.axvline(2., color = vline_color)
like image 152
The Dude Avatar answered Oct 13 '22 19:10

The Dude