前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >matplotlib 误差棒图

matplotlib 误差棒图

作者头像
用户6021899
发布于 2019-08-14 08:13:22
发布于 2019-08-14 08:13:22
2K00
代码可运行
举报
运行总次数:0
代码可运行

matplotlib中使用errorbar()绘制误差棒图来表示被测量的误差范围。

各示例代码如下(除第2例子外,其它例子来自于matplotlib官方文档):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import numpy as np
import matplotlib.pyplot as plt
# example data
x = np.arange(0.1, 4, 0.5)
y = np.exp(-x)
fig, ax = plt.subplots()
ax.errorbar(x, y, xerr=0.2, yerr=0.4)
plt.show()
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import matplotlib
from  matplotlib import ticker
import matplotlib.pyplot as plt
import numpy as np
matplotlib.rcParams["font.sans-serif"] = ["SimHei"]
matplotlib.rcParams["axes.unicode_minus"] = False
X = np.linspace(2,80,8)
Y= 100*np.sin(X)
xerror = 1* np.random.randn(X.shape[0])+3
yerror = 2*xerror
plt.errorbar(X, Y, fmt= "bo:", xerr = xerror, yerr = yerror)
plt.title("误差图示例",fontsize =18,color ="b")
plt.xlabel("x 轴标签",color ="b")
plt.ylabel("y 轴标签",color ="b")
plt.show()
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.patches import Rectangle
# Number of data points
n = 5
# Dummy data
np.random.seed(10)
x = np.arange(0, n, 1)
y = np.random.rand(n) * 5.
# Dummy errors (above and below)
xerr = np.random.rand(2, n) + 0.1
yerr = np.random.rand(2, n) + 0.2

def make_error_boxes(ax, xdata, ydata, xerror, yerror, facecolor='r',
                     edgecolor='None', alpha=0.5):
    # Create list for all the error patches
    errorboxes = []
    # Loop over data points; create box from errors at each point
    for x, y, xe, ye in zip(xdata, ydata, xerror.T, yerror.T):
        rect = Rectangle((x - xe[0], y - ye[0]), xe.sum(), ye.sum())
        errorboxes.append(rect)
    # Create patch collection with specified colour/alpha
    pc = PatchCollection(errorboxes, facecolor=facecolor, alpha=alpha,
                         edgecolor=edgecolor)
    # Add collection to axes
    ax.add_collection(pc)
    # Plot errorbars
    artists = ax.errorbar(xdata, ydata, xerr=xerror, yerr=yerror,
                          fmt='None', ecolor='k')
    return artists

# Create figure and axes
fig, ax = plt.subplots(1)
# Call function to create error boxes
_ = make_error_boxes(ax, x, y, xerr, yerr)
plt.show()
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import numpy as np
import matplotlib.pyplot as plt
# example data
x = np.array([0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0])
y = np.exp(-x)
xerr = 0.1
yerr = 0.2
# lower & upper limits of the error
lolims = np.array([0, 0, 1, 0, 1, 0, 0, 0, 1, 0], dtype=bool)
uplims = np.array([0, 1, 0, 0, 0, 1, 0, 0, 0, 1], dtype=bool)
ls = 'dotted'
fig, ax = plt.subplots(figsize=(7, 4))
# standard error bars
ax.errorbar(x, y, xerr=xerr, yerr=yerr, linestyle=ls)
# including upper limits
ax.errorbar(x, y + 0.5, xerr=xerr, yerr=yerr, uplims=uplims,
            linestyle=ls)
# including lower limits
ax.errorbar(x, y + 1.0, xerr=xerr, yerr=yerr, lolims=lolims,
            linestyle=ls)
# including upper and lower limits
ax.errorbar(x, y + 1.5, xerr=xerr, yerr=yerr,
            lolims=lolims, uplims=uplims,
            marker='o', markersize=8,
            linestyle=ls)
# Plot a series with lower and upper limits in both x & y
# constant x-error with varying y-error
xerr = 0.2
yerr = np.zeros(x.shape) + 0.2
yerr[[3, 6]] = 0.3
# mock up some limits by modifying previous data
xlolims = lolims
xuplims = uplims
lolims = np.zeros(x.shape)
uplims = np.zeros(x.shape)
lolims[[6]] = True  # only limited at this index
uplims[[3]] = True  # only limited at this index
# do the plotting
ax.errorbar(x, y + 2.1, xerr=xerr, yerr=yerr,
            xlolims=xlolims, xuplims=xuplims,
            uplims=uplims, lolims=lolims,
            marker='o', markersize=8,
            linestyle='none')
# tidy up the figure
ax.set_xlim((0, 5.5))
ax.set_title('Errorbar upper and lower limits')
plt.show()
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-04-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
数据科学 IPython 笔记本 8.6 可视化误差
对于任何科学测量,误差的准确计算几乎与数字本身的准确报告一样重要,甚至更重要。例如,假设我正在使用一些天体物理观测来估计哈勃常数,即宇宙膨胀率的局部测量值。我知道目前的文献显示,它是大约71 (km/s)/Mpc,我用我的方法测得的值为74 (km/s)/Mpc。这些值是否一致? 鉴于此信息,唯一正确的答案是:没有办法知道。
ApacheCN_飞龙
2022/05/07
3020
数据科学 IPython 笔记本 8.6 可视化误差
40000字 Matplotlib 实操干货,真的全!
Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率谱,条形图,错误图,散点图等。
统计学家
2020/09/22
10.4K0
40000字 Matplotlib 实操干货,真的全!
Python数据可视化-第2章-使用matplotlib绘制简单图表
在使用bar()函数绘制图表时,可以通过给该函数的bottom参数传值的方式控制柱形的y值,使后绘制的柱形位于先绘制的柱形的上方。
用户2225445
2025/03/31
1090
Python数据可视化-第2章-使用matplotlib绘制简单图表
Matplotlib绘制的27个常用图(附对应代码实现)
https://nbviewer.jupyter.org/github/matplotlib/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part2-Plotting_Methods_Overview.ipynb
double
2019/11/14
9640
12个最常用的matplotlib图例 !!
折线图(Line Plot):用于显示数据随时间或其他连续变量的变化趋势。在实际项目中,可以用于可视化模型性能随着训练迭代次数的变化。
JOYCE_Leo16
2024/03/19
5270
12个最常用的matplotlib图例 !!
基于Matplotlib的高级数据可视化技术与实践探索
文章链接:https://cloud.tencent.com/developer/article/2466769
一键难忘
2024/11/21
2340
python数据分析之Matplotlib学习笔记
说到绘图,那必须要有一个画板。Figure作为一个“老画板”,在matlab中经常能看到它的出没,在python中,它的具体语法是什么呢?让我们来看一下。
远方的星
2021/07/31
8490
python数据分析之Matplotlib学习笔记
Matplotlib使用笔记
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
村雨遥
2019/09/09
5660
Matplotlib 绘2D图
Matplotlib 是一个非常简单而又完善的开源绘图库。那么它到底有多简单呢? 基本知识 首先官方文档奉上 下面,我们通过 3 行代码绘制一张简单的折线图。 from matplotlib imp
听城
2018/04/27
2.4K0
Matplotlib 绘2D图
【matplotlib】2-使用统计函数绘制简单图形
函数功能: 在x轴上绘制定性数据的分布特征 调用签名: plt.bar(x, y) 参数说明:
程序员小涛
2022/12/13
1.4K0
【matplotlib】2-使用统计函数绘制简单图形
Matplotlib从入门到精通02-层次元素和容器
参考: https://datawhalechina.github.io/fantastic-matplotlib/%E7%AC%AC%E4%B8%80%E5%9B%9E%EF%BC%9AMatplotlib%E5%88%9D%E7%9B%B8%E8%AF%86/index.html
用户2225445
2023/10/16
5010
Matplotlib从入门到精通02-层次元素和容器
python matplotlib各种绘图类型完整总结
plot([x], y, [fmt], [x2], y2, [fmt2], …, **kwargs)
Twcat_tree
2022/12/05
6K0
python matplotlib各种绘图类型完整总结
Python基础(十二) | 还不会python绘图?两万字博文教你Matplotlib库(超详细总结)
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot
timerring
2022/10/08
2.5K0
Python基础(十二) | 还不会python绘图?两万字博文教你Matplotlib库(超详细总结)
Matplotlib 学习笔记
在 matplotlib 中,整个图像为 Figure ,而一个 Figure 中可以有多个 axes。
zucchiniy
2020/05/22
5360
Matplotlib 1.4W+字基础教程来了(收藏吃灰去吧~~)
参考:Rougier N P, Droettboom M, Bourne P E, et al. Ten Simple Rules for Better Figures[J]. PLOS Computational Biology【IF 4.7】, 2014, 10(9).感兴趣戳:https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4161295/pdf/pcbi.1003833.pdf
DataCharm
2021/02/22
1.5K0
Matplotlib 1.4W+字基础教程来了(收藏吃灰去吧~~)
Python Matplotlib库:统计图补充
✅作者简介:人工智能专业本科在读,喜欢计算机与编程,写博客记录自己的学习历程。 🍎个人主页:小嗷犬的博客 🍊个人信条:为天地立心,为生民立命,为往圣继绝学,为万世开太平。 🥭本文内容:Python Matplotlib库:统计图补充 ---- Python Matplotlib库:统计图补充 1.引言 2.直方图 3.箱线图 4.误差条图 5.小提琴图 6.尖峰栅格图 7.二维直方图/散点密度图 8.Hexbin散点图 9.扇形图 ---- 1.引言 上两期我们讲了 Matplotlib 库
小嗷犬
2022/11/15
1.9K0
Python Matplotlib库:统计图补充
含有带有很少误差条的数据全精度数据图。
import numpy as np import matplotlib.pyplot as plt # example data x = np.arange(0.1, 4, 0.1) y1 = np.exp(-1.0 * x) y2 = np.exp(-0.5 * x) # example variable error bar values y1err = 0.1 + 0.1 * np.sqrt(x) y2err = 0.1 + 0.1 * np.sqrt(x/2) # Now switch t
裴来凡
2022/05/28
3640
含有带有很少误差条的数据全精度数据图。
matplotlib安装及使用
matplotlib是基于python语言的开源项目,旨在为python提供一个数据绘图包。我将在这篇文章中介绍matplotlib API的核心对象,并介绍如何使用这些对象来实现绘图。实际上,matplotlib的对象体系严谨而有趣,为使用者提供了巨大的发挥空间。用户在熟悉了核心对象之后,可以轻易的定制图像。matplotlib的对象体系也是计算机图形学的一个优秀范例。即使你不是python程序员,你也可以从文中了解一些通用的图形绘制原则。matplotlib使用numpy进行数组运算,并调用一系列其他的python库来实现硬件交互。matplotlib的核心是一套由对象构成的绘图API。
狼啸风云
2023/10/07
4930
matplotlib安装及使用
数据分析画图:50道练习玩转matplotlib
Matplotlib 是 Python 的绘图库。它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案,也可以和图形工具包一起使用。和Pandas、Numpy并称为数据分析三兄弟。友情提示:代码虽好,自己动手才算学到。
Datawhale
2020/02/21
8660
Matplotlib
Matplotlib 官网 此篇笔记参考来源为《莫烦Python》 安装同之前所述,参考《Python初学基础》 基本使用 2.1 基本用法 import matplotlib.pyplot as plt import numpy as np x = np.linspace(-1, 1, 50) #使用np.linspace定义x:范围是(-1,1);个数是50 y = 2*x + 1 plt.figure() #定义一个图像窗口 plt.plot(x, y) plt.show()
闪电gogogo
2018/05/28
1.2K0
相关推荐
数据科学 IPython 笔记本 8.6 可视化误差
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档