我有3个列表,它们都有相同数量的元素:
time_list ###(float - converted from date object with matplotlib.dates.date2num)
temperature_list ###(float)
heating_list ###(boolean)
我想用Matplotlib绘制温度随时间变化的曲线。我还有一个heating_list列表,它由布尔值True/False组成,具体取决于当时是否发生了加热。例如,如果正在加热,我希望曲线为红色,如果没有加热,则曲线为蓝色。
我确实找到了这个例子:http://wiki.scipy.org/Cookbook/Matplotlib/MulticoloredLine
但是,我想使用heating_list来决定颜色,而不是一些温度值。我还想使用已有的列表,而不是示例中的numpy数组。
编辑:
我都试了些什么。
colors = ListedColormap(["b", "k"])
norm = BoundaryNorm([not heating, heating], colors.N) ### I don't really know how to define this condition
lc = LineCollection(tuple(zip(time_list, temperature_list)), label = "T1", cmap=colors, norm=norm)
###I tried to make my lists the same form as this "linen = (x0, y0), (x1, y1), ... (xm, ym)"
###According to LineCollection found here: http://matplotlib.org/api/collections_api.html
plt.gca().add_collection(lc)
plt.show()
我也试过了:
time_plot = plt.plot(time_list, temperature_list, label = "T1")
time_plot.gca().add_collection(lc)
有一些问题,我的列表的格式是错误的,这就是为什么我试图做所有那些zip/tuple的事情。然后我意识到它无论如何都不能工作,因为我不知道如何定义什么时候应该有哪些颜色。食谱示例简单地定义了值的范围,而我不希望它依赖于温度值,而是依赖于外部属性。
发布于 2013-07-11 07:49:21
我最终通过绘制几个图来做到这一点,以便每个图始终是一种颜色。但是,这会在绘图之间留下很小的间隙,并且您不能轻松地将其作为一个绘图来处理。
我发现,如果你不需要连接这些点,那么使用scatters做这类事情也会容易得多,因为scatter可以有一个色彩映射表作为参数。
https://stackoverflow.com/questions/17543505
复制相似问题