使用 python 和 matplotlib,在不给出预期输出的两行之间填充 [英] Using python and matplotlib, fill between two lines not giving expected output

查看:56
本文介绍了使用 python 和 matplotlib,在不给出预期输出的两行之间填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制带有相关误差的线性线.我计算了斜率(a)和截距(b)的值.此外,我计算了与这些值相关的误差.所以我画了下面典型公式给出的线.

I am trying to plot a linear line with associated error. I calculated values for slope (a) and intercepts (b). In addition, I calculated the error associated with these values. So I drew the line given by the typical formula below.

y=ax+b

不过,除了线,我还想画相关的错误.我想出了绘制与这些公式相关的线条并将线条之间的空间涂成灰色的想法.

However, in addition to the line, I also want to draw the associated error. I came up with the idea to draw the lines associated with these formulas and color the space between the lines gray.

y=(a+a_sd)x+(b+b_sd)

y=(a+a_sd)x+(b+b_sd)

y=(a-a_sd)x+(b-b_sd)

y=(a-a_sd)x+(b-b_sd)

使用以下代码,我可以为线条之间的部分表面着色,但不能为整个跨度着色(请参见随附的输出).
我认为这可能是由于距离"与距离"有关.未排序,fill_between 分别使用距离[0] 和距离[-1] 作为跨度的开始和结束.
与往常一样,任何帮助将不胜感激!

Uisng the following piece of code, I am able to color part of the surface between the lines, but not the whole span (see included output).
I think this may be due to the fact that "distance" is not sorted, and fill_between is using distance[0] and distance[-1] as begin and end for the span, respectively.
As always, any help would be highly appreciated!

import matplotlib.pyplot as plt
        
distance=[0.35645334340084989, 0.55406894241607718, 0.10201413273193734, 0.13401365724625941, 0.71918808865838735, 0.14151335417722818]
time=[2.4004984846346171, 2.4909766335028447, 1.9852064018125195, 1.9083156734132103, 2.6380396934372863, 1.9114505780323543]
time_SD=[0.062393810960652669, 0.056945715242838917, 0.073960838867327183, 0.084111239062664475, 0.026912957190265499, 0.08595664694840538]
distance_SD=[0.035160608598240162, 0.032976715460514235, 0.02782911002465227, 0.035465701695038584, 0.043009444687382707, 0.038387585107200854]
a=1.17887019041
b=1.83339229489
a_sd=0.159771527859
b_sd=0.0762509747218
        
plt.errorbar(distance,time,yerr=time_SD, xerr=distance_SD, linestyle="None") 
abline_values = [(a)*i + (b) for i in distance]
abline_values_plus = [(a+a_sd)*i + (b+b_sd) for i in distance]
abline_values_minus = [(a-a_sd)*i + (b-b_sd) for i in distance]
plt.plot(distance, abline_values,"r")
plt.fill_between(distance,abline_values_minus,abline_values_plus,facecolor='lightgrey', interpolate=True, edgecolors="None")
leg = plt.legend(loc="lower right", frameon=False, handlelength=0, handletextpad=0)
for item in leg.legendHandles:
    item.set_visible(False)
plt.show()

推荐答案

为了使用 pyplot.fill_between() 绘制水平坐标的列表应进行排序.可以使用x值的未排序列表,但可能导致不良结果.

In order to use pyplot.fill_between() the list to plot the horizontal coordinate should be sorted. Using an unsorted list of x values is possible, but can lead to undesired results.

排序列表可以使用 sorted(list).

import matplotlib.pyplot as plt

distance=[0.35645334340084989, 0.55406894241607718, 0.10201413273193734, 0.13401365724625941, 0.71918808865838735, 0.14151335417722818]
time=[2.4004984846346171, 2.4909766335028447, 1.9852064018125195, 1.9083156734132103, 2.6380396934372863, 1.9114505780323543]
time_SD=[0.062393810960652669, 0.056945715242838917, 0.073960838867327183, 0.084111239062664475, 0.026912957190265499, 0.08595664694840538]
distance_SD=[0.035160608598240162, 0.032976715460514235, 0.02782911002465227, 0.035465701695038584, 0.043009444687382707, 0.038387585107200854]
a=1.17887019041
b=1.83339229489
a_sd=0.159771527859
b_sd=0.0762509747218

distance_sorted = sorted(distance)

plt.errorbar(distance,time,yerr=time_SD, xerr=distance_SD, linestyle="None") 
abline_values = [(a)*i + (b) for i in distance_sorted]
abline_values_plus = [(a+a_sd)*i + (b+b_sd) for i in distance_sorted]
abline_values_minus = [(a-a_sd)*i + (b-b_sd) for i in distance_sorted]
plt.plot(distance_sorted, abline_values,"r")

plt.fill_between(distance_sorted,abline_values_minus,abline_values_plus, facecolor='lightgrey', edgecolors="None")

plt.show()

<小时>该文档没有提到对x值进行排序的要求.原因可能是fill_between实际上甚至可以用于未排序的列表,但这并不是人们期望的那样.也许下面的动画可以更直观地理解这个问题:


The documentation does not mention the requirement of x values being sorted. The reason is probably that fill_between actually works even with unsorted lists, just not the way one might expect. Maybe the following animation gives a more intuitive understanding on the issue:

这篇关于使用 python 和 matplotlib,在不给出预期输出的两行之间填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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