基于python中使用条件集合的条件的彩色时间序列线图

问题描述

Example Plot 我有一个数据框,其中包含雪水和温度数据的时间序列。我正在寻找一个雪水的时间序列图,该图在雪水线图中显示两种颜色,如果温度 273 deg K,则为“红色”。尝试遵循matplotib文档(https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/multicolored_line.html),但未成功。将不胜感激。谢谢!

我的数据框如下:日期(datetime64 [ns]);雪水(float64)和温度(float64)

from matplotlib.collections import LineCollection

Date                  Snowwater  Temperature
2014-01-01 01:00:00   5           240
2014-01-01 02:00:00   10          270
2014-01-01 03:00:00   11          273
2014-01-01 04:00:00   15          279
2014-01-01 05:00:00   20          300
2014-01-01 06:00:00   25          310

我正在寻找类似上面链接的示例图中所示的输出,但是在y轴上具有雪水值(线颜色为蓝色或红色,取决于温度),在x轴上具有日期时间

解决方法

我手动创建了数据,但LineCollection是一个 这是一个包含多行的对象,第一个参数是行的列表。

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

xs = [0,1,2,3,4,5]
ys = [-2,-1,5,10]
lines = [[(x1,y1),(x2,y2)] for x1,y1,x2,y2 in zip(xs,ys,xs[1:],ys[1:])]

colors = ['r','r','b','b']
lc = LineCollection(lines,colors=colors)

fig,ax = plt.subplots()
ax.add_collection(lc)
ax.autoscale()
plt.show()

enter image description here

,

这可以解决问题,尽管可能有更好的方法:

x64

result plot

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...