只填充两个x值之间的填充,不填充整个散点图,matplotlib

2024-10-01 05:00:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在散点图的两行之间填充。这是为了显示散点图平均值的标准偏差。在

import matplotlib.pyplot as plt
import numpy as np

y1 # is the first set of y-axis data
y2 # is the second set of y-axis data
y3 # is the third set of y-axis data
x # is the x-axis data set

ym1 = np.mean(y1)
ym2 = np.mean(y2)
ym3 = np.mean(y3)
# To set up the means of the three data series

sigma1 = np.std(y1)
sigma2 = np.std(y2)
sigma3 = np.std(y3)
# To set up the standard deviations

ysupper1 = ym1 + sigma1
ysupper2 = ym2 + sigma2
ysupper3 = ym3 + sigma3
yslower1 = ym1 - sigma1
yslower2 = ym2 - sigma2
yslower3 = ym3 - sigma3
# To set up the higher and lower limits to fill between

yma1 = np.array([ym1])
yma2 = np.array([ym2])
yma3 = np.array([ym3])
# To set up values to put into a plt.axhline

在这些设置完成后,我创建了一个y1、y2和y3与x的关系图,这里没有出现任何问题。然后设置plt.axhline和{}:

^{pr2}$

但是,这样创建的图形始终只在沿x轴的两点之间填充。我尝试过手动地将plt.fill_between中的where参数声明为None,正如我所预期的那样,没有任何变化。我不明白为什么它只在这些点之间填充到沿着x轴的任意点。以下是我得到的图表图像,供参考:

Image of graph that I am getting

编辑:答案一如既往地被找到,因为我们无法准确理解一个函数是如何工作的。在


Tags: ofthetodataisnppltset
1条回答
网友
1楼 · 发布于 2024-10-01 05:00:04

好的,我发现的问题是由于我没有提到的代码的一部分,因为我认为这是不必要的。我使用glob.glob函数帮助对我访问的大量文件进行排序,以提取所需的数据。当使用np.fill_between函数时,这对于正常绘图来说很好,x数组只使用数组中的第一个和最后一个值来设置范围。这就是为什么我得到一个奇怪的填充范围。当我使用np.sortx按升序排序,然后在np.fill_between中使用这个新数组,它就起作用了。抱歉弄得乱七八糟。在

相关问题 更多 >