python - 如何绘制渐变色线?
问题描述
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()
但是我如何line
用 ax
和y
坐标绘制渐变颜色?
解决方案
您是否已经看过 https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/multicolored_line.html?
编辑:正如评论中所建议的,一个最小的工作示例可能是
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
x = 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)
推荐阅读
- java - Spring Data @JoinColumn 和实体更新
- qt - 在 mime 数据函数中制作树结构
- jquery - 使用 jQuery 限制复选框(一次一个类别和一个子类别)
- ios - 使用 swift 中的按钮操作控制 Web 视图
- python-3.x - 如何“PUT”json数据包含汉字?
- javascript - 运行位于 fastclick 中的测试会导致 Jest 崩溃
- javascript - 谷歌表格脚本不起作用?(用于更改其他选项卡中值的数组)
- javascript - 通过数字或字符串为 Date() 赋值有什么区别?
- javascript - 如何使用 v-for 循环使用 Vue.js 遍历列
- javascript - 如何获得对象acc的最大值形式。到不同的-不同的键