Python Forum

Full Version: Creating Complex Color Spectrum Plot From Data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all

I was hoping I could get some suggestions on how to implement the following Python program.

I work on the highway, one of the issues I have to deal with is fixing surface cracks.

The only time I need to intervene is when there are at least 4 cracks on the same cross section. For example, the attachment P1 shows 5 lines, each line represents a crack. In this case all 5 of these cracks were found between 2m and 6m.

See P1 Attachment

However I only need to intervene when 4 or more of the cracks overlap, as shown below.
From attachment P2 you can see that there are 4 cracks the all overlap between 3.7m and 4.2m – it is this section that I need to fix.

See P2 Attachment

My aim is to create a color spectrum where if there is no cracks then the color of the row /plot would be green, if it has 2 x overlapping cracks then the color would be yellow, 3 over lapping cracks the color would be Red and 4 or more overlapping cracks the color would be light blue. For example:-

See P3 Attachment

This is what I wanted to create in Python.
For a new user I know this is a challenging task but I am up to it.
But I wanted to ask if this was possible? I was considering achieving this in a figure window with a plot?

But is there an alternative way to do this? I have the data that tells me where the cracks start and stop i.e.
Crack Type Start Stop
A 5.128 5.962
A 5.965 7.123

Is there a built in function where if I ordered the data in some way then Python/Matplot lib could some how achieve my objective?

Any help or advice would be greatly appreciated.

Thank you.
By counting +1 for a crack start and -1 for a crack stop, you can easily determine the number of cracks at any position. Here is an example
cracks = [
    (3.69, 4.37),
    (3.37, 4.63),
    (3.74, 5.57),
    (2.66, 4.34),
    (4.11, 5.77)
]

def main():
    L = []
    for start, stop in cracks:
        L.append((start, 1))
        L.append((stop, -1))
    L.sort()
    s = 0
    for i, (x, incr) in enumerate(L):
        s += incr
        L[i] = (x, s)
    
    print(L)
    print()
    u, v = zip(*L)
    print(u)
    print(v)
        

if __name__ == '__main__':
    main()
The output shows clearly that there are at least 4 cracks between 3.74 meters and 4.37
Output:
[(2.66, 1), (3.37, 2), (3.69, 3), (3.74, 4), (4.11, 5), (4.34, 4), (4.37, 3), (4.63, 2), (5.57, 1), (5.77, 0)] (2.66, 3.37, 3.69, 3.74, 4.11, 4.34, 4.37, 4.63, 5.57, 5.77) (1, 2, 3, 4, 5, 4, 3, 2, 1, 0)
If I borrow Gribouillis' cracks[] (no pun intended), and multiply them by 100, to get centimeters,
I can put them in a 600(cm) by 5 matrix. For me it is easier to set the matrix to 0 when no crack,
series of "1" indicate a crack. Numpy tells me the sum of the 5 rows. (This varies between 0 = green and 5 = blue)
The simplest of graphics packages (Turtle) turns this in a spectrum plot. No need for "complex" things :-)
Paul

[Image: cracks.jpg]