所以我有3个1D数组,x_vals,y_vals和z_vals。
我想在y_vals上绘制x_vals,z_vals定义该点的颜色。
从我查找的所有东西看起来我需要使用numpy.meshgrid,但是当我尝试这个python时,只是超时。
有什么想法?谢谢!
答案 0 :(得分:0)
您正在寻找LineCollection
命令。试试这个
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
fig,ax = plt.subplots(1)
# some data to plot
x = np.linspace(0,1,200)
y = np.sin(10*x)
z = x**2
# creating the segments
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
# creating LineCollection object
lc = LineCollection(segments, cmap=plt.get_cmap('jet'),norm=plt.Normalize(0, 1))
lc.set_array(z)
lc.set_linewidth(2)
# add LineCollection to plot
ax.add_collection(lc)
# set plotting range
ax.set_xlim((0,1))
ax.set_ylim((-1,1))
plt.show()