Search code examples
pythonmatplotlibgraphline-plot

Python Matplotlib line plot: change line color in the middle


I am trying to plot a continuous line from a series of coordinates but I would like to change the line color at specific junctures.

Input arrays: layerdict['Xc'] = [50.6, 69.4, 69.4, 50.6, **50.6**, **50.2**, 69.8, 69.8, 50.2, **50.2**, **69.053**, 69.12, 69.12] layerdict['Yc'] = [50.6, 50.6, 69.4, 69.4, **50.6**, **50.2**, 50.2, 69.8, 69.8, **50.2**, **50.88**, 50.996, 51.796]

** is just for visual purposes

I want to change the color of the line that goes from (50.6, 50.6) to (50.2,50.2) and (50.2, 50.6) to (69.053,5088) and so on... What is the best way to do this ? I have a conditional statement that can detect then conditions and insert blank values or other operations

Here is what I have so far.

layerdict = {'Xc': [], 'Yc': [], 'Xt': [], 'Yt': []}

with open(inputfilepath, 'r') as ifile:

for item in ifile:
    gonematch = gonepattern.match(item)
    gtrmatch = gtrpattern.match(item)

    if gonematch:
        tlist = item.split(' ')
        layerdict['Xc'].append(float(tlist[1][1:]))
        layerdict['Yc'].append(float(tlist[2][1:]))
    elif gtrmatch:
        tlist = item.split(' ')
        layerdict['Xt'].append(float(tlist[1][1:]))
        layerdict['Yt'].append(float(tlist[2][1:]))



plt.plot(layerdict['Xc'], layerdict['Yc'], label='linepath', linewidth=3.5)


plt.xlabel('X')
plt.ylabel('Y')
plt.show(block=True)

A sample Input file will look like this (just for reference, where I am extracting the coordinates from)

X10 Y10 A10 B10
X11 Y11 A10
X12.4 Y23.5 A5 ...

Solution

  • I'd use masked arrays from the ma module of numpy. While numpy arrays would already be better than simple lists with regards to indexing and math, masked arrays are automatically plotted only where the mask values are False. However, you still can retrieve the whole unmasked array simply by using the data property, so plotting firstly the whole array and then only a subset is just a matter of two almost identical plot commands:

    import matplotlib.pyplot as plt
    import numpy as np
    import numpy.ma as ma
    
    layerdict = dict()
    layerdict['Xc'] = [50.6, 69.4, 69.4, 50.6, 50.6, 50.2, 69.8, 69.8, 50.2, 50.2, 69.053, 69.12, 69.12]
    layerdict['Yc'] = [50.6, 50.6, 69.4, 69.4, 50.6, 50.2, 50.2, 69.8, 69.8, 50.2, 50.88, 50.996, 51.796]
    
    highlightmask = np.ones(len(layerdict['Xc'])).astype(bool)
    highlightmask[4:6] = highlightmask[9:11] = False
    
    layerdict['Xc'] = ma.array(layerdict['Xc'])
    layerdict['Yc'] = ma.array(layerdict['Yc'], mask=highlightmask)
    
    plt.plot(layerdict['Xc'], layerdict['Yc'].data, label='linepath', linewidth=3.5)
    plt.plot(layerdict['Xc'], layerdict['Yc'], 'r', linewidth=3.5)
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.show()