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

Question:

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?

Asked By: Jeet

||

Answers:

So I am not sure I fully grasped your question but here is an attempt – hope you find it useful!

import numpy as np
import matplotlib.pyplot as plt

if __name__ == "__main__":

    # Make some fake data
    # Fixing random state for reproducibility
    np.random.seed(19680801)
    # Random data arrays
    ar1 = np.random.randn(100)
    ar2 = np.random.randn(100)
    ar3 = np.random.randn(100)
    ar4 = np.random.randn(100)
    t = np.arange(100)

    # Variables Sum1 and Sum2
    sum1 = np.sum(np.sum(ar1) + np.sum(ar2))
    sum2 = np.sum(np.sum(ar3) + np.sum(ar4))

    # Colormap
    cmap = plt.get_cmap('RdYlGn')
    min_cmap = sum1  # Bottom of colorbar is variable sum1
    max_cmap = sum2  # Top of colorbar is variable sum2

    # Start figure
    fig, ax = plt.subplots()

    fig.suptitle("A Title")

    # Set ar1/ar2 and ar3/ar4 in same axis with same colormap   
    ax.scatter(ar1, ar2, c=t, marker='v', vmin=min_cmap, vmax=max_cmap, cmap=cmap, label='classical')
    ax.scatter(ar3, ar4, c=t, marker='o', vmin=min_cmap, vmax=max_cmap, cmap=cmap, label='customized')
    ax.set_ylabel("Y label")
    ax.set_xlabel("X label")
    ax.legend()

    fig.colorbar(mappable=plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(min_cmap, max_cmap)), ax=ax, 
                 orientation='vertical', format='%.1f', label="Sum1 (ar1 + ar2) to Sum2 (ar3 + ar4)")

    plt.tight_layout()
    plt.show()

Example scatter plot

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()

Answered By: Jeet