Python Matplotlib: plot with two-dimensional arguments: how to specify parameters?

I draw a few curves as follows:

import numpy as np import matplotlib.pyplot as plt plt.plot(x, y) 

where x and y are two-dimensional (for example, N x 2 for this example).

Now I would like to set the color of each of these curves independently. I tried things like:

 plot(x, y, color= colorArray) 

using, for example, colorArray= ['red', 'black'] , but to no avail. The same goes for other options (linestyle, marker, etc.).

I know that this can be done using a for loop. However, since this plot command accepts multidimensional x / y, I thought it should be possible to also specify plotting options this way.

Is it possible? What is the right way to do this? (all that I found when the search used the loop effectively)

+4
source share
3 answers

You can use ax.set_color_cycle :

 import numpy as np import matplotlib.pyplot as plt np.random.seed(2013) N = 10 x, y = np.random.random((2,N,2)) x.cumsum(axis=0, out=x) y.cumsum(axis=0, out=y) fig, ax = plt.subplots() colors = ['red', 'black'] ax.set_color_cycle(colors) ax.plot(x,y) plt.show() 

gives enter image description here

+5
source

I would build it the way you do, and then the for loop changes colors to suit your colorArray :

 plt.plot(x,y) for i, line in enumerate(plt.gca().lines): line.set_color( colorArray[i] ) 
+1
source

I usually pass one-dimensional arrays with this, for example:

 plot(x[0], y[0], 'red', x[1], y[1], 'black') 
+1
source

Source: https://habr.com/ru/post/1486841/


All Articles