Change the color of the curve according to its y value in matplotlib

I am trying to reproduce the style of the attached figure using matplotlib objects.

Curves whose color change with height

Basically, I want to change the color of the curve according to its y value using matplotlib.

+4
source share
1 answer

The selected plot does not have the color specified by the vertical axis of the graph (this is what I would like to consider the y-value). Instead, it simply has 8 different plots, each with a different color, without indicating what color means.

Here is an example of what looks like your plot:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

# some fake data:
x = np.linspace(0, 2*np.pi, 1000)
fs = np.arange(1, 5.)
ys = np.sin(x*fs[:, None])

for y, f in zip(ys, fs):
    plt.plot(x, y, lw=3, c=cm.hot(f/5))

colors

, , , Line2D , . - , .

x = np.linspace(0, 2*np.pi, 1000)
y = np.sin(2*x)
plt.scatter(x,y, c=cm.hot(np.abs(y)), edgecolor='none')

:

  • 0 1, y.max() > 1, : c=cm.hot(y/y.max()) , .
  • edgecolor='none', scatter , .
  • , , .

more colors

+7

All Articles