如何根据数据库中的值绘制具有不同颜色的垂直线?

时间:2019-11-25 20:05:14

标签: python-3.x class plot

我正在尝试绘制代表深度的垂直线。在不同的深度存在不同类型的土壤,我想用不同的颜色表示它们。

我收到了存储在类中的数据。基于摩擦数,我想用三种不同的颜色为垂直线着色。但是,我不知道该如何实现。

数据存储在ds1中(我有许多不同的文件(ds2,ds3,.. ds29),因此使我的工作很方便)。 ds1.depth调用深度数组,ds1.frictionnumber表示摩擦数等。

由于大量数据和土壤变化,我不知道该怎么做。如果下面类似的方法行得通,但我无法解决,那就太好了。

更新:我在这里找到了一些代码-> http://abhay.harpale.net/blog/python/how-to-plot-multicolored-lines-in-matplotlib/

我调整了代码,得到了相当合理的结果,但是仍然出问题了。在我的数据中,我最后得到了4个NaN值(没有测量摩擦)。在下面的图像中,这应该是从水平线向下,但是事实并非如此。我该如何解决这个问题,或者出了什么问题?

更新2:线条的高度也明显地与线宽成比例。现在,我只需要找到另一种绘制较宽线条的方法即可。欢迎任何建议。

def find_contiguous_colors(colors):
    # finds the continuous segments of colors and returns those segments
    segs = []
    curr_seg = []
    prev_color = ''
    for c in colors:
        if c == prev_color or prev_color == '':
            curr_seg.append(c)
        else:
            segs.append(curr_seg)
            curr_seg = []
            curr_seg.append(c)
        prev_color = c
    segs.append(curr_seg) # the final one
    return segs

def plot_multicolored_lines(x,y,colors):
    segments = find_contiguous_colors(colors)
    plt.figure(figsize=(12,8))
    start= 0
    for seg in segments:
        end = start + len(seg)
        l, = plt.gca().plot(x[start:end],y[start:end],lw=50,c=seg[0]) 
        start = end
    plt.axhline(-5.8988)
    plt.axis('equal')

x = np.full(len(ds1.depth), 1)
y = ds1.depth 
# color segments
colors = ['red']*len(ds1.depth)
for i in range(len(ds1.depth)):
    if 0 < ds1.wrijvingsgetal[i] <= 1:
        colors[i] = 'blue'
    elif 1 < ds1.wrijvingsgetal[i] <= 2:
        colors[i] = 'green'
    elif ds1.wrijvingsgetal[i] > 2: 
        colors[i] = 'yellow'
    else:
        colors[i] = 'magenta'

Vertical soil profile

1 个答案:

答案 0 :(得分:0)

这根本没有效率,但是我得到了想要的结果。如果有人知道如何使用赞赏的堆叠条形图来解决此问题(由于某种原因,我无法使其正常工作)。我的修复方法如下:

ds1 = read_gef(filepath)

def find_contiguous_colors(colors):
    # finds the continuous segments of colors and returns those segments
    segs = []
    curr_seg = []
    prev_color = ''
    for c in colors:
        if c == prev_color or prev_color == '':
            curr_seg.append(c)
        else:
            segs.append(curr_seg)
            curr_seg = []
            curr_seg.append(c)
        prev_color = c
    segs.append(curr_seg) 
    return segs

def plot_multicolored_lines(x, y, colors):
    segments = find_contiguous_colors(colors)
    plt.figure(figsize=(12,8))
    start= 0
    for seg in segments:
        end = start + len(seg)
        l, = plt.gca().plot(x[start:end], y[start:end], lw=2, c=seg[0]) 
        start = end
    #plt.axhline(-5.8988, -1, 1)

x = np.full((len(ds1.depth), 2), 0)
y = np.array(ds1.depth) 

# color segments
colors = ['red']*len(ds1.depth)
handle = [0] * 3
for i in range(len(ds1.depth)):
    if 0 < ds1.wrijvingsgetal[i] <= 2:
        colors[i] = 'yellow'
        sandplt = plt.axhline(y[i], color=colors[i])
#    elif 1 < ds1.wrijvingsgetal[i] <= 2:
#        colors[i] = 'green'
#        peatplt = plt.axhline(y[i], color=colors[i]) 
    elif ds1.wrijvingsgetal[i] > 2: 
        colors[i] = 'green'
        clayplt = plt.axhline(y[i], color=colors[i])
    else:
        colors[i] = 'magenta'
        nanplt = plt.axhline(y[i], color=colors[i])


ratioxl = 1/5
plt.xlim(0, ratioxl*(ds1.depth[-1]-ds1.depth[0]))
plt.axis('scaled')
plt.ylabel('Depth w.r.t. groundlevel [m]')
plt.title('Vertical soil profile ' + str(Path(filepath).stem))
plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom=False,      # ticks along the bottom edge are off
    top=False,         # ticks along the top edge are off
    labelbottom=False) # labels along the bottom edge are off
plt.legend(handles = (sandplt, clayplt, nanplt), labels=['Sand', 'Clay', 'Nan'], loc=[1.01, 0.78])
plt.savefig(os.path.join(wd, '..', '03data', '20190711-Wiertsema', 'VN72650-1_GEF', 'afbeeldingen') + '\VSP_' + str(Path(filepath).stem))
plt.show()