# How do I bleed colors from blue to green in matplotlib?

4

I'm doing a program to graph 22 different lines but I can not make the color shift be gradual from blue to green so that you can see better the difference in growth between each of the lines instead of a color random Please help

``````import matplotlib.pyplot as plt
import numpy as np
t1 = np.arange(0.0, 1.0, 0.02)
for n in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]:
plt.plot(t1, t1*n)
plt.show()
``````

asked by Roberto VM 13.09.2018 в 02:18
source

1

One option is create your own color map . This is especially useful when we want more complex color maps, similar to those that bring Matplotilib by default , likewise, the approach which we will see below can be used with any color map predefined by Matplotlib.

``````from matplotlib.colors import LinearSegmentedColormap, Normalize
import matplotlib.pyplot as plt
import numpy as np

# Diccionario para definir cada canal y como cambian en el rango 0-1
## se puede agregar el canal alpha si se desea.
cdict = {'red': ((0.0, 0.0, 0.0),
(1.0, 0.0, 0.0)),

'green': ((0.0, 0.0, 0.0),
(1.0, 1.0, 1.0)),

'blue':  ((0.0, 1.0, 1.0),
(1.0, 0.0, 0.0))
}

# Creamos nuestro mapa de colores personalizado
blue_green = LinearSegmentedColormap('BlueGreen', cdict, N=100)

# Datos
t1 = np.arange(0.0, 1.0, 0.02)
vals = np.arange(1, 21)

# La instancia de color.Normalize es un callable que
## permite normalizar cualquier valor entre  min y max
norm = Normalize(vals.min(), vals.max())  # Normalize(min(vals), max(vals)) en lista

for n in vals:
plt.plot(t1, t1*n, color=blue_green(norm(n)))

plt.show()
``````

The map in this case is quite simple where the amount of blue and green increase and descend linearly all the time:

Any color map created via `LinearSegmentedColormap` allows you to obtain a specific color by weighing it with a value between 0 and 1. The function of `color.Normalize` is to normalize our values to this range.

To use a map of the predefined ones in Matplotlib we just have to load it using its name, for example:

``````cmap=plt.cm.get_cmap('RdBu')
norm = Normalize(vals.min(), vals.max())

for n in vals:
plt.plot(t1, t1*n, color=cmap(norm(n)))
``````

The map can be reused in subsequent graphics if we need it.

The output for this example is:

answered by 13.09.2018 / 05:08
source
1

With very few changes you can solve it, let's see:

``````import matplotlib.pyplot as plt
import numpy as np
t1 = np.arange(0.0, 1.0, 0.02)

lista = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
lista_normalizada = [ (n-min(lista))/(max(lista)-min(lista)) for n in lista]

for i,n in enumerate(lista):
print(i)
As you can see, the main change is the use of the parameter `color` in `plt.plot()` , which we indicate as a list with the values red, green and blue. To achieve the gradient, the green and blue components must be modified. Since these values are adjusted from 0 to 1, we need a normalized list ( `lista_normalizada` ) that we calculate so that the original values correspond to values from 0 to 1. Then we simply decrease the blue component and increase the green.