#45 Control color of each marker | seaborn






The graph #41 shows how to custom the features of markers and the #43 shows how to map a categorical value to a color.

But sometimes, you need to have a more precise control on the color. Here we show how to build a new column with the requested color, and how to use this column in the seaborn code.




import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import seaborn as sns

# Create data frame. The last column is the needed color
df = pd.DataFrame(np.random.random((100,2)), columns=["x","y"])

# Add a column: the color depends of x and y values, but you can use whatever function.
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']})

  • Sponsors

  • Leave a Reply

    Your email address will not be published.