有没有一种方法可以在Matplotlib中从不接触的两个垂直函数之间进行着色?

2024-06-01 12:21:17 发布

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

我目前正在重建一个项目的温度剖面,该项目在y轴上显示高度,在x轴上显示温度波动,如下所示:

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

在图的中间有一条粗线代表插值/模拟温度分布。插值轮廓的右侧和左侧是误差边界,基本上是实际数据,上面有+/-误差值。我想在这些误差范围之间加上阴影,显示插值的温度分布在这些范围内

然而,问题是它们从未接触过,因此本质上它们具有不同的x值。它们也是垂直运行的,所以plt.fill_betweenaxvspan(只生成一个矩形)都不起作用。我试着颠倒一些论点的顺序,因为我是垂直绘制的,它的工作原理如下:

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)

但事后看来,这可能是我在黑暗中的愚蠢之举。我被难住了


Tags: the项目高度plotbetween温度fill误差
1条回答
网友
1楼 · 发布于 2024-06-01 12:21:17

要通过ImportanceOfBeingErnest扩展comment,下面是一个完整的示例,展示了^{}的用法

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

相关问题 更多 >