Search code examples
pythonmatplotlibcolors

How can I change the colors of a chart's data points depending on the value of the Y-axis? (Python: Matplotlib)


Chart

My aim is to show the negative data points (related to the Y-axis) in red, those values that are at zero in gray, and only the positive values in green

This is a simplified dataset of my issue:

Row_Data

Here is my current code:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

data = pd.read_excel('data.xlsx')
plt.plot([data.A, data.B,data.C, data.D], 'o', c= 'g')
plt.xticks([0,1,2,3], ['A', 'B', 'C', 'D'])
plt.yticks(np.arange(-4, 6, step=2))

I hope someone can help me with this.

Thanks a lot!


Solution

  • To assign a color to each entry, you will need to first stack() the dataframe so that each column will have the x values (A,B,C,D) and y would be the actual values. Then assign a color to each row and plot. The updated code is below.

    df=data.stack().reset_index().drop(columns=['level_0']) #Stack data and remove unnecessary column
    df.rename(columns={'level_1':'x', 0:'val'}, inplace=True) #Rename for easy identification
    df['color'] = ["green" if i > 0 else "red" for i in df['val']] #Create color column
    plt.scatter(df.x, y=df.val, c= df.color) #Plot with color = color column
    plt.xticks([0,1,2,3], ['A', 'B', 'C', 'D'])
    plt.yticks(np.arange(-4, 6, step=2))
    

    enter image description here