Search code examples
pythonmatplotlibgraphicsscatter

Matplotlib change graph colors


I would like to change colors in the next graph. I need to have yellow as green color, green as red and violet as blue.

enter image description here

I have the next code:

fig = plt.figure()
ax = fig.add_subplot(111)

scatter = ax.scatter( kmeans['Revenue'], kmeans['Frequency'],c=kmeans['Cluster'], s=15)
                
ax.set_xlabel('Recency')
ax.set_ylabel('Frequency')
plt.colorbar(scatter)

How could I change the colors?


Solution

  • If you would read documentation for scatter then you would see option cmap for colormap.

    If you use Google matplotlib scatter colormap then you find Choosing Colormaps in Matplotlib

    It shows predefined colormaps and it seems you need brg.


    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    random = np.random.sample((100,3))
    kmeans = pd.DataFrame(random, columns=['Revenue', 'Frequency', 'Cluster'])
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    scatter = ax.scatter( kmeans['Revenue'], kmeans['Frequency'], c=kmeans['Cluster'], s=15, cmap='brg')
                    
    ax.set_xlabel('Recency')
    ax.set_ylabel('Frequency')
    plt.colorbar(scatter)
    
    plt.show()
    

    enter image description here