python - 图中曲线之间的阴影区域

标签 python matplotlib

因此,我有几条由相同 x 值但不同 y 值定义的曲线。下图由 21 条不同的曲线组成。其中 10 条是虚线,1 条是实线,最后 10 条是虚线。

但是,如图所示,它在一张图表中相当多。你无法真正看到到处是什么。所以我想要的是在前 10 行和最后 10 行之间有一个阴影区域,我认为这会让眼睛更舒服。

但我不太确定如何开始?

现在我的代码如下:

import os
import numpy as np
import matplotlib.pyplot as plt

structures = ['Rectum']
patients = ["426"]

color_map = 'nipy_spectral'
color = {'PTV':0.16, 'Rectum':0.80, 'Bladder':0.96}

legends = ['PTV', 'Rectum', 'Bladder']

x = np.loadtxt('dose_gy.txt')


for plot, patient in enumerate(patients):
    plot += 1
    PATH_TO_YDATA = 'results/Two_Fields/DVH'
    for f in sorted(os.listdir(PATH_TO_YDATA), key=lambda f: f.split('_')[-2]):
        if f.split('_')[-2] == patient:
            for structure in structures:
                if f.split('_')[2] == structure:
                    y = np.loadtxt(PATH_TO_YDATA + '/' + f)
                    plt.axis([0, 90, 0, 50])
                    if int(f.split('_')[-1][:-4]) < 90:
                        plt.plot(x, y * 100, color=plt.get_cmap(color_map)(color[structure]), linestyle='dotted', alpha=0.8, linewidth=2.0)
                    elif int(f.split('_')[-1][:-4]) > 90:
                        plt.plot(x, y * 100, color=plt.get_cmap(color_map)(color[structure]), linestyle='dashed', alpha=0.8, linewidth=2.0)
                    elif int(f.split('_')[-1][:-4]) == 90:
                        plt.plot(x, y * 100, color=plt.get_cmap(color_map)(color[structure]), linestyle='solid', alpha=1.0, linewidth=3.0, zorder=1000)
    plt.title('Patient ' + str(plot))
    plt.xlabel("Dose [Gy]", fontsize=14)
    plt.ylabel("Volume [%]", fontsize=14)


plt.show()

enter image description here

最佳答案

为了在多条曲线的最小值和最大值之间进行填充,您需要确定表示曲线上每个点的最小值或最大值的数组。如果所有曲线共享相同的 x 值,则可以通过沿组合 y 值的一个轴取最小值来轻松完成此操作。例如

np.min(np.c_[y1, y2, y3, ...], axis=1)

最大值相同。然后 fill_ Between 可以与这些组合数组一起用作输入。

一个完整的例子:

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(4)

# generate some data to plot
x = np.linspace(0.8,10,201)
f = lambda x, p1,p2,p3,p4: p1/x + np.sinc(x*p4)*p3 + p2
Y = np.empty((len(x), 9))
P1 = 1.5+ (np.random.normal(size=9)-0.5)*0.2
P2 = np.linspace(0.9,1.1, 9)
P3 = 1+ (np.random.normal(size=9)-0.5)*0.2
P4 = np.linspace(0.9,1.1, 9)+ (np.random.normal(size=9)-0.5)
for i in range(9):
    Y[:,i] = f(x,P1[i], P2[i], P3[i], P4[i])

# plot    
fig, ax = plt.subplots()

style= [":"]*4 + ["-"] + ["--"]*4
colors = ["crimson"]*4 + ["k"] + ["#9a0bad"]*4
lw = np.ones(9); lw[4] = 2
for i in range(9):
    ax.plot(x,Y[:,i], linestyle=style[i], label="curve "+str(i), lw=lw[i], color=colors[i])

Y1min = np.min(Y[:,:4], axis=1)
Y1max = np.max(Y[:,:4], axis=1)
Y2min = np.min(Y[:,5:], axis=1)
Y2max = np.max(Y[:,5:], axis=1)

ax.fill_between(x, Y1max, Y1min, color="crimson", alpha=0.4)
ax.fill_between(x, Y2max, Y2min, color="#9a0bad", alpha=0.4)

plt.show()

enter image description here

关于python - 图中曲线之间的阴影区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43553737/

相关文章:

Python 文件重命名和分割

python - datetime.datetime.strptime 将字符串转换为日期时间时出现错误 未转换日期 remands : 2

python - 从函数调用的输出中删除 None

python - 发送压缩后的表单数据

python - 如何使用pygame和openGL修改相机的 View

python - 使用 matplotlib 创建的 EPS 无法正确显示文本

python - 调整 Seaborn 热图上的刻度设置

python - 调整 matplotlib 注释框内的填充

python - Matplotlib:如何修复意外的 x 刻度行为

python - 将数据添加到 seaborn jointplot