matplotlib 的上限/下限 [英] Upper/lower limits with matplotlib

查看:91
本文介绍了matplotlib 的上限/下限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用误差线绘制一些数据点.其中一些数据点只有上限或下限,而不是误差线.

I want to plot some data points with errorbars. Some of these data points have only upper or lower limit, instead of error bars.

因此,我试图使用索引来区分带有误差条的点和具有上限/下限的点.但是,当我尝试这样的事情时:

So I was trying to use indices to differentiate between the points with errorbars, and the points with upper/lower limits. However, when I try something like this:

errorbar(x[i], y[i], yerr = (ymin[i], ymax[i]))

我收到错误:

ValueError: In safezip, len(args[0])=1 but len(args[1])=2

这类似于此处的讨论,但我不使用熊猫,但是,阅读一些关于它的其他词对我来说真的很有用.

This is similar to the discussion here, but I don't use pandas, and however, it would be really useful to me to read some other few words about that.

无论如何,我尝试通过以下方式扭转"错误:

In any case, I tried to "turnaround" the error in the following way:

errorbar(x[i], y[i], yerr = [[ymin[i], ymax[i]]], uplims = True)

但结果图不清楚:似乎上限和误差线绘制在一起,或者上限绘制了两次...

But the resulting plot is not clear: it seems that the upper limit AND the errorbars are plotted together, or the upper limit is plotted twice...

目标是在上下误差线不对称时绘制上限/下限,因此我可以选择上/下限箭头前的条线长度.

The goal is to plot upper/lower limits when the upper and lower error bars are not symmetrical, so I can choose the length of the bar before the arrow for the upper/lower limit.

推荐答案

这实际上是让我对 errorbar 感到烦恼的事情之一:它对输入的形状和维度非常挑剔.

This is actually one of the things that tends to annoy me about errorbar: it's very finicky about the shape and dimensionality of inputs.

我假设您想要误差线",但是希望它们的位置由绝对上限和下限来设置,而不是由对称的误差"值来设置.

What I'm assuming is that you want "error bars", but want their locations to be set by absolute upper and lower bounds, rather than by a symmetric "error" value.

errorbar 的作用是将您的 y 数组和 yerr[0] 压缩在一起(使用 safezip)作为下限(yerr[1] 用于上限).因此,您的y和yerr [0](和[1])应该是大小,形状和维数相同的数组.yerr本身根本不需要是数组.

What errorbar does is zip together (with safezip) your y array and yerr[0] for the lower bound (yerr[1] for upper). So your y and yerr[0] (and [1]) should be arrays with the same size, shape and number of dimensions. yerr itself doesn't need to be an array at all.

如果 x、y、ymin 和 ymax 都是一维数组,那么您拥有的第一个代码工作,这是它们应该的样子.听起来你的y不是.

The first code that you have will work if x, y, ymin, and ymax are all one-dimensional arrays, which is what they should be. It sounds like your y is not.

但是,请务必注意,由于 errorbar yerr 是误差量,而不是绝对限值,因此您需要从实际的下限和上限中添加和减去 y.

However, it's important to note that since errorbar yerr are error amounts, and not absolute limits, you need to add and subtract your y from your actual lower and upper limits.

例如:

x = array([1,2,3,4])
y = array([1,2,3,4])  
ymin = array([0,1,2,3])
ymax = array([3,4,5,6])
ytop = ymax-y
ybot = y-ymin

# This works
errorbar( x, y, yerr=(ybot, ytop) )

如果我误解了什么,请告诉我.如果您能以您正在使用的形式发布一些示例数据,那就太好了.

Let me know if I'm misinterpreting anything. It would be good if you could post some example data in the form that you're using.

这篇关于matplotlib 的上限/下限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆