Search code examples
pythonmatplotlibplotvisualizationscatter-plot

How plot different values according to a color scale by considering a condition in matplotlib?


I have plotted points from 4 arrays in the following manner. I have plotted them in the same figure by calling plt.plot twice.

import matplotlib.pyplot as plt
plt.plot(ar1,ar2,'b^',label='classical')
plt.plot(ar3,ar4,'go',label='customized')

Now I want to plot the points with a color scale according to a variable sum1 and sum2. sum1 is a reference for arr1 and arr2. sum2 is a reference for arr3 and arr4. How big is the value of sum1(for arr1 and arr2) or sum2(for arr3 and arr4) is going to decide the color of the points that are plotted with the arrays. Preferrable on a color scale from red to green, the points have to be plotted. Now I have plotted the values only with 2 colors (blue and green) as written in the code above. Now I have to plot them referring to the sum values and on a color scale from red to green. How should I proceed?

I have done the following to plot the numbers but the color of the points are not correct. How can I make the coloring perfect of the plotted points according to the values of the sum1_array_classical for the first plot and sum2_array_customized for the second plot.

arr1=[0.71952534,0.70191413,0.7772932 ]
arr2_classical=[0.6946462,0.6902773,0.67463213]
arr3=[0.55630213,0.4769557,0.4736585 ]
arr4=[0.40091604,0.48901308,0.4931372 ]
sum1_array=[0.38544054,0.35463165,0.3929781 ]
sum2_array=[3.15046262,3.38864656,3.18986858]

plt.scatter(arr1,arr1,c=sum1_array,marker='^',cmap='jet', lw=0,label='classical')

plt.scatter(arr3,arr4,c=sum2_array_customized,marker='*',cmap='jet', lw=0,label='multiobjective')

The colorings are not correct. what is the problem here?


Solution

  • Use the property of norm provided by the plt.scatter and also configure the color bar with the clim function. Please refer to the code below

    normalize = plt.Normalize(vmin=0, vmax=3)
    plt.scatter(ar1,ar2,c=sum1_array,marker='*',cmap='jet',norm=normalize, lw=0,label='classical')
    plt.scatter(ar3,ar4,c=sum2_array,marker='^',cmap='jet', norm=normalize,lw=0,label='customized')
    plt.legend(bbox_to_anchor=(0.9, -0.12),
              fancybox=True, shadow=True, ncol=4)
    plt.clim(0,3)
    plt.colorbar(label="Manhatten distance from the perfect image that is 0")
    plt.xlabel("Array values1 and 3")
    plt.ylabel("Array values 2 and 4")
    plt.tight_layout()
    plt.show()