使用y值进行颜色的python折线图

2024-10-01 11:28:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个编程的初学者。。。我正在尝试从2列xy数据绘制曲线,用整条线而不是散点。我想根据y的值给y着色。 我可以使它适用于散点图,但不适用于线形图。在

我的代码:

import numpy as np
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D
import matplotlib

read data ... (data are xy 2 columns so one can simply use 2 lists, say a and b)
# a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
# b = [11,12,3,34,55,16,17,18,59,50,51,42,13,14,35,16,17]


fig = plt.figure()
ax = fig.add_subplot(111)

bnorm = []
for i in b:
    i = i/float(np.max(b))   ### normalizing the data
    bnorm.append(i)

plt.scatter(a, b, c = plt.cm.jet(bnorm))
plt.show()

有了分散的效果。。。 我怎样才能把它变成一个有颜色的线条图?像这样: enter image description here


Tags: 数据importdatamatplotlibas编程npfig