qshinoの日記

Powershell関係と徒然なこと

色付きグラフ by matplotlab on python

値による色付きグラフ

python matplotlibでの色付きグラフ。 線分単位で値に応じて色を指定し、一本のグラフを引く。

import matplotlib.pyplot as plt
import math
import matplotlib

N=128
f = 8
omg = [i*f*2*math.pi/N for i in range(N)]
sig = [math.sin(i) for i in omg]

fig,(ax) = plt.subplots(1,1)
colormap = plt.get_cmap('jet')
norm = matplotlib.colors.Normalize(vmin=min(sig), vmax=max(sig))
c = colormap(norm(sig))

for j in range(len(sig)-1):
    ax.plot(omg[j:j+2], sig[j:j+2], color=c[j+1])

plt.show()

参考

http://naga-tsuzuki.sblo.jp/s/article/182253300.html

https://qiita.com/ynakayama/items/8d3b1f7356da5bcbe9bc