Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid line color repetition in matplotlib.pyplot?

I am comparing some algorithmic results using matplotlib.pyplot, however it is very difficult to understand what is going on since several lines have the same exact color. Is there a way to avoid this? I don't think that pyplot has only seven colors, has it?

like image 296
tunnuz Avatar asked May 17 '11 07:05

tunnuz


People also ask

How do I stop matplotlib overlapping?

Use legend() method to avoid overlapping of labels and autopct. To display the figure, use show() method.

Is PLT show () blocking?

show() and plt. draw() are unnecessary and / or blocking in one way or the other.

How do I change the line color in python matplotlib?

The usual way to set the line color in matplotlib is to specify it in the plot command. This can either be done by a string after the data, e.g. "r-" for a red line, or by explicitely stating the color argument.

What does %Matplotlib inline mean?

%matplotlib inline turns on “inline plotting”, where plot graphics will appear in your notebook. This has important implications for interactivity: for inline plotting, commands in cells below the cell that outputs a plot will not affect the plot.


1 Answers

For Python 3, from the solutions above you can use:

colormap = plt.cm.nipy_spectral
colors = [colormap(i) for i in np.linspace(0, 1,number_of_plots)]
ax.set_prop_cycle('color', colors)

or:

import seaborn as sns

    colors = sns.color_palette("hls", number_of_plots)
    ax.set_prop_cycle('color', colors)
like image 176
Ulysses Avatar answered Sep 21 '22 10:09

Ulysses