热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何使用阈值线创建matplotlib条形图?

如何解决《如何使用阈值线创建matplotlib条形图?》经验,为你挑选了2个好方法。

我想知道如何使用阈值线创建matplotlib条形图,阈值线以上条形部分应为红色,阈值线以下部分应为绿色.请给我一个简单的例子,我在网上找不到任何东西.



1> Carsten..:

使其成为堆叠条形图,如本示例所示,但将数据划分为高于阈值的部分和下面的部分.例:

import numpy as np
import matplotlib.pyplot as plt

# some example data
threshold = 43.0
values = np.array([30., 87.3, 99.9, 3.33, 50.0])
x = range(len(values))

# split it up
above_threshold = np.maximum(values - threshold, 0)
below_threshold = np.minimum(values, threshold)

# and plot it
fig, ax = plt.subplots()
ax.bar(x, below_threshold, 0.35, color="g")
ax.bar(x, above_threshold, 0.35, color="r",
        bottom=below_threshold)

# horizontal line indicating the threshold
ax.plot([0., 4.5], [threshold, threshold], "k--")

fig.savefig("look-ma_a-threshold-plot.png")

显示代码结果的示例图



2> Abu Shoeb..:

您可以axhline像这样简单地使用。请参阅此文档

# For your case
plt.axhline(y=threshold,linek')

# Another example - You can also define xmin and xmax
plt.axhline(y=5, xmin=0.5, xmax=3.5)


推荐阅读
author-avatar
火山哥哥5237
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有