Matplotlib中的渐变颜色图

时间:2018-03-13 15:08:17

标签: python matplotlib tkinter

我正在编写一个python应用程序,它从无线传感器网络收集温度和时间。

简单地说,我的代码片段是这样的:

a.set_facecolor((0.9, 0.9, 1.0))
myFmt = mdates.DateFormatter('%H:%M')
a.xaxis.set_major_formatter(myFmt)
a.xaxis_date()
a.set_title('WSN temperature')
a.set_ylabel('Temp ' + u"\u2103")
a.set_xlabel('Time')
a.grid(True)
a.set_ylim([-20, 40])   #values found in the collectsensor.py
a.yaxis.set_ticks(np.arange(-20, 40, 5))
a.set_xlim([timeIntervalLeft, timeIntervalRight])

cmap = clr.LinearSegmentedColormap.from_list("", ["darkblue", "blue", "violet", "yellow", "orange", "red"])
cmap2 = clr.LinearSegmentedColormap.from_list("", ["red", "orange", "yellow", "violet", "blue", "darkblue"])

a.scatter(timeList, tempList, c=tempList, s=100, cmap=cmap)

y = tempList
s = pd.Series(y, index=timeList)

# convert dates to numbers first
inxval = mdates.date2num(s.index.to_pydatetime())
points = np.array([inxval, s.values]).T.reshape(-1, 1, 2)

segments = np.concatenate([points[:-1], points[1:]], axis=1)

lc = LineCollection(segments, cmap=cmap2, linewidth=3)
lc.set_array(inxval)
a.add_collection(lc)

使用该代码,我获得了类似的内容:

enter image description here

你可以看到散射可以使用cmap,因此点变得越来越热"随着y值的增加。我为剧情功能寻找了类似的东西,但我找不到任何适合我的东西。

感谢ImportanceOfBeingErnest,我注意到之前的问题How to plot multi-color line if x-axis is date time index of pandas

但是这里的颜色与y轴的温度并没有关系,但是它沿着所有的cmap颜色分布。

0 个答案:

没有答案
相关问题