With the following code, I can use seaborn’s scatterplot to plot data with certain colors assigned to data values.
How can I set the amount of colors that get used in this example? (e.g. if I want to have only two colors used or more than the 6 shown in the example)
import seaborn as sns import matplotlib.pyplot as plt tips = sns.load_dataset('tips') print("tips.columns=", tips.columns) # tips.columns= Index(['total_bill', 'tip', 'sex', 'smoker', 'day', 'time', 'size'], dtype='object') sns.scatterplot(data=tips, x="total_bill", y="tip", hue="total_bill", )
Advertisement
Answer
Seaborn’s scatterplot
has a legend=
keyword, which can be 'auto'
, 'brief'
, 'full'
or False
. If you want something else, you’d need to create a custom legend.
If you want to control the exact number of levels and colors used, you can create an additional column with these hue levels. That way, you fully control the levels and their naming. Pandas pd.cut()
can be helpful. It accepts either a number of bins, or a list of boundaries. You can set the names at the same time (the default labels would indicate the boundaries as '({start},{end}]'
).
Here is an example:
import matplotlib.pyplot as plt import seaborn as sns import pandas as pd tips = sns.load_dataset('tips') fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(14, 4)) tips['bill level'] = pd.cut(tips["total_bill"], [0, 30, 1000], labels=["≤ 30", "> 30"]) sns.scatterplot(data=tips, x="total_bill", y="tip", hue="bill level", palette=['turquoise', 'crimson'], ax=ax1) ax1.set_title('2 levels, 2 colors') tips['bill level'] = pd.cut(tips["total_bill"], bins=7, labels=[f'level {i}' for i in range(1, 8)]) sns.scatterplot(data=tips, x="total_bill", y="tip", hue="bill level", palette='Set2', ax=ax2) ax2.set_title('7 levels, 7 colors') plt.tight_layout() plt.show()