【发布时间】:2017-05-01 15:24:33
【问题描述】:
我正在尝试在散点图上的两条线之间进行填充。这是为了显示散点图平均值周围的标准偏差。
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's 和plt.fill_between's:
plt.axhline(yma1, linestyle='--', color='b', alpha=0.7),
plt.axhline(yma2, linestyle='--', color='r', alpha=0.7),
plt.axhline(yma3, linestyle='--', color='c', alpha=0.7),
# To create the 'mean value' lines on the graph
plt.fill_between(x, ysupper1, yslower1, color='b', alpha=0.3),
plt.fill_between(x, ysupper2, yslower2, color='r', alpha=0.3),
plt.fill_between(x, ysupper3, yslower3, color='c', alpha=0.3),
# To fill in between the upper and lower limits of the standard deviation
但是,这样创建的图形始终只在 x 轴的两点之间填充。我尝试手动将plt.fill_between 中的where 参数声明为None,并且正如我所料,没有任何改变。我不知道为什么它只在这些之间填充到沿 x 轴的看似任意点。作为参考,这里是我得到的图表的图像:
编辑:一如既往地找到答案是因为未能准确理解函数的工作原理。
【问题讨论】:
-
认为
fill_between与plot几乎相同,只是其中有两行,x-data 相同。我想你可以从那里独立进步。 -
你的 x 范围是你的 x 轴范围吗?我猜你的 x 只覆盖了图表的填充部分
-
问题中的代码似乎可以正常工作,因此为了获得帮助,您应该(一如既往)提供问题的minimal reproducible example。
-
@heyu91 上帝我讨厌犯愚蠢的错误。我使用
glob.glob来帮助整理我使用过的大量文件。这意味着x中的值不是升序或降序。由于glob.glob的工作方式,它们实际上来自从文件中提取的两个任意值。在我使用np.sort函数对x进行升序排序后,它现在工作正常。我想这意味着更多的代码更改。我会回答我自己的问题。
标签: python matplotlib