Search code examples
pythonmatplotlibseaborncategoriesanomaly-detection

How to label the Y axis with True, False and color code the Python plot?


I am trying to plot the True False values for data from a pandas series object data3. I want the True and False points to be color coded and the Y axis to indicate True and False.

I am already plotting the pandas series with True and False values which are automatically displayed as 1/0 in the following plot.

   import matplotlib.pyplot as plt
   from matplotlib.pyplot import figure
   figure(num=None, figsize=(20,10), dpi=80, facecolor='w', edgecolor='k')
   plt.scatter(x = data3.index, y= data3, color='g', marker='d')

Plot Results

Including seaborn in the tag to see if there is a better option.


Solution

  • from matplotlib.pyplot import figure
    import matplotlib.pyplot as plt
    import matplotlib.colors as clrs
    figure(num=None, figsize=(20,10), dpi=80, facecolor='w', edgecolor='k')
    cmap = clrs.ListedColormap(['red', 'green'])
    plt.yticks([1.0, 0.0], ["True",
                            "False"])
    plt.scatter(x = data3.index, y= data3, c=(data3 != True).astype(float), marker='d', cmap=cmap)#plt.cm.get_cmap('RdBu'))
    

    This produces the following image

    CategoricalPlot