Search code examples
pythonmatplotlibfigurecolormapcolor-coding

Creating a colour coded figure which represents how a variable changes along the x-direction in Python. (similar to a colour map)


I have to create a colour coded figure of how the thickness of a beam varies along the beam. For example a figure like this:

[Beam schematic with coloured blocks representing the different sections of the beam][1] [1]: https://i.sstatic.net/3GZCp.png The coloured blocks in the figure have to be colour coded like in a contour plot; so for example, a darker shade would indicate a thicker part of a beam and vice versa. The key to the colour code should also be provided with the resulting figure. The 'colour blocks' essentially represent sections of the beam, which is stored in an array. The corresponding thicknesses of the sections are also stored in an array.

import numpy as np

beamsectionsx = np.array([ 0.   ,  2.65 ,  6.25 , 10.45 , 14.05 , 17.25 , 20.05 , 23.05 ,
       26.05 , 29.05 , 32.12 , 34.72 , 37.32 , 40.92 , 44.72 , 48.52 ,
       52.32 , 55.42 , 58.512, 61.612, 63.702, 63.825, 64.02, 66.62 ]) # points of the sections along the x-axis

thicknessy = np.array([0.4, 0.099, 0.099,0.099,0.1,0.099, 0.079,0.079, 0.079, 0.081, 0.084, 0.088, 0.091, 0.092, 0.095, 0.094, 0.093, 0.09, 0.082, 0.068, 0.068, 0.075, 0.095]) 
# thicknesses of the respective sections. For eg: thickness between 0m and 2.65m (from beamsectionsx), would correspond to 0.4 and so on. 



I tried creating colourbars to represent this, but instead of representing the span of the beam in the x-direction (0-66.62m), the colourmap represents 0-24 instead - this corresponds to the length of the array respectively.

Any input on creating the figure in a similar manner to the example figure shown above would be helpful.

Thank you.


Solution

  • I was able to create the plot with a colorbar as follows:

    import numpy as np
    from matplotlib import pyplot as plt
    from matplotlib.colors import Normalize
    
    beamsectionsx = np.array([ 0.   ,  2.65 ,  6.25 , 10.45 , 14.05 , 17.25 , 20.05 , 23.05 ,
           26.05 , 29.05 , 32.12 , 34.72 , 37.32 , 40.92 , 44.72 , 48.52 ,
           52.32 , 55.42 , 58.512, 61.612, 63.702, 63.825, 64.02, 66.62 ]) # points of the sections along the x-axis
    
    thicknessy = np.array([0.4, 0.099, 0.099,0.099,0.1,0.099, 0.079,0.079, 0.079, 0.081, 0.084, 0.088, 0.091, 0.092, 0.095, 0.094, 0.093, 0.09, 0.082, 0.068, 0.068, 0.075, 0.095])
    # thicknesses of the respective sections. For eg: thickness between 0m and 2.65m (from beamsectionsx), would correspond to 0.4 and so on.
    
    # Colormap of choice
    cmap = plt.cm.plasma
    
    # Plot bars
    fig, ax = plt.subplots(1, 1)
    for x0, x1, y in zip(beamsectionsx[:-1], beamsectionsx[1:], thicknessy):
        ax.fill_between([x0, x1], [y, y], color=cmap(y / max(thicknessy)))
    
    # Set colorbar with label
    cbar = plt.colorbar(mappable=plt.cm.ScalarMappable(norm=Normalize(0, max(thicknessy)), cmap=cmap))
    cbar.set_label('Beam thickness')
    
    fig.savefig('result.png')
    

    Output plot