Assign Colors to Lines

2024/10/13 18:27:06

I am trying to plot a variable number of lines in matplotlib where the X, Y data and colors are stored in numpy arrays, as shown below. Is there a way to pass an array of colors into the plot function, so I don't have to take an extra step to assign a color to each line individually? Should I be translating the RGB color arrays to another color format for this to work, such as HSV or other?

import numpy as np
X = np.arange(1990, 1994)
Y = [[  1.50615936e+08   5.88252480e+07   2.60363587e+08][  1.53193798e+08   5.91663430e+07   2.63123995e+08][  1.55704596e+08   5.94899260e+07   2.65840188e+08][  1.58175186e+08   5.97843680e+07   2.68559452e+08]]
colors = [(0.99609375, 0.3984375, 0.3984375) (0.796875, 0.0, 0.99609375)(0.59765625, 0.99609375, 0.0)]
#current way
ax.plot(X, Y)
[ax.lines[i].set_color(color) for i, color in enumerate(colors)]
#way I feel it can be done, but doesn't work currently
ax.plot(X, Y, color=colors)
plt.show()

Any help is greatly appreciated.

Answer

I think you want to use the Axes method set_color_cycle. As you can imagine, it sets the list of colors that are cycled through when colors are assigned by default, i.e. when no color keyword is provided to the plot call. Here's an extended version of your example:

import matplotlib.pyplot as plt
import numpy as npX = np.arange(1990, 1994)
Y = [[  1.50615936e+08,   5.88252480e+07,   2.60363587e+08],[  1.53193798e+08,   5.91663430e+07,   2.63123995e+08],[  1.55704596e+08,   5.94899260e+07,   2.65840188e+08],[  1.58175186e+08,   5.97843680e+07,   2.68559452e+08]]
colors = [(0.99609375, 0.3984375, 0.3984375), (0.796875, 0.0, 0.99609375),(0.59765625, 0.99609375, 0.0)]fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.set_title('old way')
ax1.plot(X, Y)
[ax1.lines[i].set_color(color) for i, color in enumerate(colors)]ax2 = fig.add_subplot(212)
ax2.set_title('new way')
ax2.set_color_cycle(colors)
ax2.plot(X, Y)fig.savefig('manycolors.py')
plt.show()

This results in two subplots with the identically colored lines:

enter image description here

https://en.xdnf.cn/q/69504.html

Related Q&A

How to display multiple annotations in Seaborn Heatmap cells

I want seaborn heatmap to display multiple values in each cell of the heatmap. Here is a manual example of what I want to see, just to be clear:data = np.array([[0.000000,0.000000],[-0.231049,0.000000]…

ImportError: No module named lxml on Mac

I am having a problem running a Python script and it is showing this message:ImportError: No module named lxmlI suppose I have to install somewhat called lxml but I am really newbie to Python and I don…

Pandas Rolling window Spearman correlation

I want to calculate the Spearman and/or Pearson Correlation between two columns of a DataFrame, using a rolling window.I have tried df[corr] = df[col1].rolling(P).corr(df[col2]) (P is the window size)b…

Python string splitlines() removes certain Unicode control characters

I noticed that Pythons standard string method splitlines() actually removes some crucial Unicode control characters as well. Example>>> s1 = uasdf \n fdsa \x1d asdf >>> s1.splitlines(…

Get only HTML head Element with a Script or Tool

I am trying to get large amount of status information, which are encoded in websites, mainly inside the "< head >< /head >" element. I know I can use wget or curl or python to get…

Is it possible to restore corrupted “interned” bytes-objects

It is well known, that small bytes-objects are automatically "interned" by CPython (similar to the intern-function for strings). Correction: As explained by @abarnert it is more like the inte…

Wildcard namespaces in lxml

How to query using xpath ignoring the xml namespace? I am using python lxml library. I tried the solution from this question but doesnt seem to work.In [151]: e.find("./*[local-name()=Buckets]&qu…

WordNet - What does n and the number represent?

My question is related to WordNet Interface.>>> wn.synsets(cat)[Synset(cat.n.01), Synset(guy.n.01), Synset(cat.n.03),Synset(kat.n.01), Synset(cat-o-nine-tails.n.01), Synset(caterpillar.n.02), …

How to change the values of a column based on two conditions in Python

I have a dataset where I have the time in a game and the time of an event. EVENT GAME0:34 0:43NaN 0:232:34 3:43NaN 4:50I want to replace the NaN in the EVENT column where GAME…

logging module for python reports incorrect timezone under cygwin

I am running python script that uses logging module under cygwin on Windows 7. The date command reports correct time:$ date Tue, Aug 14, 2012 2:47:49 PMHowever, the python script is five hours off:201…