pythonmatplotlibviridis

Choosing two colors from palette in scatter plot


I am trying to make scatter plot in python using viridis palette with only two values. I really like the purple, but the yellow is bearly visible. is it possible to choose middle value (blue) and purple?

x_test = [1, 2, 3, 4]
y_test = [1, 2, 3, 4]
c_test = [0, 1, 0, 1]

plt.scatter(x = x_test, y = y_test, c = c_test,  alpha=1, cmap='viridis')

sample plot

and it produce two colors - yellow and purple. The first one is not visible.


Solution

  • You can create your custom colormap with only 2 colors:

    Code:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib import colors
    
    
    x_test = [1, 2, 3, 4]
    y_test = [1, 2, 3, 4]
    c_test = [0, 1, 0, 1]
    
    mycmap = colors.ListedColormap(['purple', 'blue'])
    
    plt.scatter(x = x_test, y = y_test, c = c_test,  alpha=1, cmap=mycmap, s=100)
    

    Plot:

    plot