曲线中曲线之间的阴影区域 [英] Shaded area between curves in plot

查看:89
本文介绍了曲线中曲线之间的阴影区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有几条曲线由相同的 x 值定义,但不同的 y 值.下图包含21条不同的曲线.其中10条是虚线,1条是实线,最后10条是虚线.

So I have several curves defined by the same x-values, but different y-values. The image below consists of 21 different curves. 10 of them is dotted lines, 1 is solid, and the last 10 is dashed.

但是,从图中可以看出,它在一张图中相当多.您真的看不到到处都是什么.所以我想要的是在前 10 行和最后 10 行之间有一个阴影区域,我认为这会让眼睛更容易看到.

However, as can be seen in the image, it's rather much in one graph. You can't really see what is what everywhere. So what I would like is having a shaded area between the first 10 lines as well as the last 10, which I think would make it a lot easier on the eyes.

但我不太确定如何开始?

But I'm not quite sure how to begin?

现在我的代码如下:

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()

推荐答案

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

In order to fill between the minimum and maximum of several curves you need to determine the array that represents the minimal or maximal value for each point along the curve. If all curves share the same x values, this is pretty easily done by taking the minimum along one axis of the combined y values. E.g.

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

最大相同.然后可以将 fill_between 与这些组合数组一起用作输入.

Same for maximum. Then fill_between can be used with those combined arrays as input.

完整示例:

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()

这篇关于曲线中曲线之间的阴影区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆