python - matplotlib:为什么我的多色线图忽略了边界值?
问题描述
我有一个异常阈值,axhline
在我的情节中用 an 注释。我希望添加标记和/或更改高于此阈值的线的颜色。我遵循了以下 matplotlib 教程:
https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/multicolored_line.html
以及这在 SO 上使用了这个问题/答案:
要生成此图:
这看起来很不错,直到您放大数据的子集:
不幸的是,这个解决方案似乎不适用于我的目的。我不确定这是否是我的错误,但显然这些线在阈值以下是红色的。在我看来,另一个问题是代码多么笨拙和冗长:
import matplotlib.dates as mdates
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm
fig, ax = plt.subplots(figsize=(15,4))
inxval = mdates.date2num(dates.to_pydatetime())
points = np.array([inxval, scores]).T.reshape(-1,1,2)
segments = np.concatenate([points[:-1],points[1:]], axis=1)#[-366:]
cmap = ListedColormap(['b', 'r'])
norm = BoundaryNorm([0, thresh, 40], cmap.N)
lc = LineCollection(segments, cmap=cmap, norm=norm)
lc.set_array(scores)
ax.add_collection(lc)
monthFmt = mdates.DateFormatter("%Y")
ax.xaxis.set_major_formatter(monthFmt)
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.autoscale_view()
# ax.axhline(y=thresh, linestyle='--', c='r')
plt.show()
dates
and scores
, and thresh
generation 在这里没有显示,但是可以用随机数重新生成以使此代码运行
问题:
为什么我的图表中的红线有时会低于阈值?有没有办法缩短为此目的所需的代码量?
解决方案
推荐阅读
- discord - 为什么我的会员加入代码无效?
- java - 使用通用 JSON 的属性生成 XML
- visual-studio - 如何防止 React 构建和 npm 将文件添加到项目中?
- vba - 使用 On Error Resume Next 的错误处理未按预期工作
- python - 两种方式/在字典 Python 上反向搜索
- node.js - 什么时候使用自定义连接工厂?
- python-3.x - 替换字符串中每个偶数 x2 次出现的子字符串
- spring-boot - 如何在 Spring Boot OAuth2 中将身份验证转换为 Jwt 对象?
- reactjs - React - 调用 api 并导出值
- postgresql - 更快地找到不同的值