如何绘制渐变颜色线?

此代码绘制scatterplot带有渐变颜色的 a:


import matplotlib.pyplot as plt

import numpy as np


x = np.arange(30)

y = x

t = x


plt.scatter(x, y, c=t)

plt.colorbar()

plt.show()

https://img1.sycdn.imooc.com/652f99ce0001261e05480393.jpg

但是我如何linexy坐标绘制渐变颜色?



噜噜哒
浏览 94回答 1
1回答

红颜莎娜

一个最小的工作示例可以是import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.collections import LineCollectionx    = np.linspace(0,1, 100)y    = np.linspace(0,1, 100)cols = np.linspace(0,1,len(x))points = np.array([x, y]).T.reshape(-1, 1, 2)segments = np.concatenate([points[:-1], points[1:]], axis=1)fig, ax = plt.subplots()lc = LineCollection(segments, cmap='viridis')lc.set_array(cols)lc.set_linewidth(2)line = ax.add_collection(lc)fig.colorbar(line,ax=ax)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python