python - 有没有办法在 Matplotlib 中从不接触的两个垂直函数之间进行着色?

标签 python matplotlib plot data-science

我目前正在为一个项目重建温度分布图,该项目在 y 轴上显示海拔高度,在 x 轴上显示温度波动,如下所示:

The temperature profile with +/- 2-sigma error bounds

在图的中间有一条粗线,代表插值/模拟的温度曲线。插值曲线的右侧和左侧是误差范围,基本上是带有 +/- 误差值的实际数据。我想在这些误差范围之间画上阴影,以表明插值温度曲线位于这些范围内。

然而,问题在于它们从不接触,因此本质上它们具有不同的 x 值。它们也是垂直运行的,因此 plt.fill_ Between axvspan (仅生成一个矩形)都不起作用。我尝试颠倒一些参数的顺序,因为我正在垂直绘制它,它的工作原理如下:

plt1.plot(data, altitude, 'b') #Make the first plot show the temperature profile
plt1.plot(maxSigma, rawalt, 'r', linewidth = 0.3)
plt1.plot(minSigma, rawalt, 'g', linewidth = 0.3)
plt1.fill_between(rawalt, minSigma, maxSigma)

但事后看来,这对我来说可能是一个愚蠢的举动。我被难住了。

最佳答案

扩展 comment通过 ImportanceOfBeingErnest ,这是一个完整的示例,显示 matplotlib.pyplot.fill_betweenx 的用法,

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(0)
fig = plt.figure(figsize=(6,8))

n = 200
x = [0.0]
xa = []
xe = []
# Generate some data with a random, accumulating jitter
for i in range(n-1):
    x.append((np.random.random()-0.5)+x[i-1])
ma = 10
# Add some variable error on each side of the generated data
# and use a running average to smooth the generated data
for i in range(n-ma):
    xa.append(sum(x[i:i+ma])/float(ma))
    xe.append([xa[i]-2+(np.random.random()-0.5)*0.25,xa[i]+2+(np.random.random()-0.5)*0.25])

y = np.linspace(10,0,n-ma)
xe = np.array(xe)
plt.plot(xa, y, lw=0.75)
plt.fill_betweenx(y, xe[:,0], xe[:,1], alpha=0.4)
plt.show()

enter image description here

关于python - 有没有办法在 Matplotlib 中从不接触的两个垂直函数之间进行着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59688223/

相关文章:

Python、Sklearn : How to reverse train_test_split of Sklearn?

python - 排序数据以在 mplot3d 中绘图

使用 ggplot2 绘制来自 Geotiff 的 R 背景图

python - 在 python 中从 cURL GET 请求保存 .zip 文件

python - 具有对数 Y 轴的小平面网格上的线性回归线

Python headless MatplotLib/Pyplot

python - 在 draw_networkx 和 matplotlib 中设置颜色

r - 使用 plotly 向现有散点图添加点

r - 如何将颜色匹配的图例添加到 R matplot

python - ABC 强制方法装饰器吗?