The previous posts control marker features and map a categorical value to a color show how to control the color of all markers or the markers of specific categories in the data. However, it is also possible to control each marker's color in the plot. You will see how to have a more precise control on the color in this example. Dataset is created with random points. In order to control colors, a new column is build with the desired color for each marker (data point).

# libraries
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import seaborn as sns
 
# Create data frame with randomly selected x and y positions
df = pd.DataFrame(np.random.random((100,2)), columns=["x","y"])
 
# Add a column: the color depends on x and y values, but you can use any function you want
value=(df['x']>0.2) & (df['y']>0.4)
df['color']= np.where( value==True , "#9b59b6", "#3498db")
 
# plot
sns.regplot(data=df, x="x", y="y", fit_reg=False, scatter_kws={'facecolors':df['color']})

plt.show()

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!