How to choose a new color for each plotted line inside a shape in matplotlib?

I would not want to specify a color for each row built:

for i in range(20): ax1.plot(x, y) 

If you look at the image for this, matplotlib will try to select colors for each row that are different from each other, but ultimately it will reuse the colors. I just want him not to repeat the already used AND / OR colors, give him a list of colors to be used.

+76
matplotlib
Feb 11 '11 at 16:05
source share
6 answers

In matplotlib versions 1.0+ you can use axes.color_cycle (see example ), and in earlier versions of Axes.set_default_color_cycle (see href = "http://nullege.com/codes/search/matplotlib.axes.set_default_color_cycle"> example).

+49
Feb 11 '11 at 16:21
source share

I usually use the 3rd of these 3s, also I did not check the 1st and 2nd versions.

 from matplotlib.pyplot import cm import numpy as np #variable n should be number of curves to plot (I skipped this earlier thinking that it is obvious when looking at picture - sorry my bad mistake xD): n=len(array_of_curves_to_plot) #version 1: color=cm.rainbow(np.linspace(0,1,n)) for i,c in zip(range(n),color): ax1.plot(x, y,c=c) #or version 2: - faster and better: color=iter(cm.rainbow(np.linspace(0,1,n))) c=next(color) plt.plot(x,y,c=c) #or version 3: color=iter(cm.rainbow(np.linspace(0,1,n))) for i in range(n): c=next(color) ax1.plot(x, y,c=c) 

example 3: example plot with iter, next color

+84
Sep 08 '14 at 18:03
source share

prop_cycle

color_cycle deprecated in 1.5 in favor of this generalization: http://matplotlib.org/users/whats_new.html#added-axes-prop-cycle-key-to-rcparams

 # cycler is a separate package extracted matplotlib. from cycler import cycler import matplotlib.pyplot as plt plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b']))) plt.plot([1, 2]) plt.plot([2, 3]) plt.plot([3, 4]) plt.plot([4, 5]) plt.plot([5, 6]) plt.show() 

GUCuG.png

Also shown in the example (currently with the wrong name): http://matplotlib.org/1.5.1/examples/color/color_cycle_demo.html, mentioned at: https://stackoverflow.com/a/3/2/2/2/6/2/2/2/2/2/2/2/2/2/2/2/2/2/2/2/3/2/2/2/2/3/2/3/2/3/2/345612/2000/6/240650/240620/

Checked in matplotlib 1.5.1.

+10
Sep 16 '16 at 0:18
source share

I don’t know if you can automatically change the color, but you can use your loop to generate different colors:

 for i in range(20): ax1.plot(x, y, color = (0, i / 20.0, 0, 1) 

In this case, the colors will range from black to 100% green, but you can customize it if you want.

See matplotlib plot () docs and find the color keyword argument.

If you want to serve a list of colors, just make sure you have a list large enough, then use the loop index to select the color

 colors = ['r', 'b', ...., 'w'] for i in range(20): ax1.plot(x, y, color = colors[i]) 
+7
Feb 11 '11 at 16:15
source share

You can also change the default color in your matplotlibrc file. If you do not know where this file is located, follow these steps in python:

 import matplotlib matplotlib.matplotlib_fname() 

This will show you the path to the currently used matplotlibrc file. In this file you will find among many other settings, as well as one for axes.color.cycle . Just enter your desired color sequence and you will find it in every plot you make. Note that you can also use all valid html color names in matplotlib.

+2
Apr 12 '14 at 15:07
source share

You can use the predefined "quality color map" as follows:

 from matplotlib.cm import get_cmap name = "Accent" cmap = get_cmap(name) # type: matplotlib.colors.ListedColormap colors = cmap.colors # type: list axes.set_prop_cycle(color=colors) 

Tested on matplotlib 3.0.3. See https://github.com/matplotlib/matplotlib/issues/10840 to discuss why axes.set_prop_cycle(color=cmap) cannot be called.

A list of predefined quality color maps is available at https://matplotlib.org/gallery/color/colormap_reference.html :

List of qualitative colormaps

0
Apr 12 '19 at 13:08
source share



All Articles