pandas - Change 1 point color in scatter plot regardless of color palette in seaborn matplotlib -
i have pandas data frame df
name value id 0.2 x b 0.4 x c 0.5 x d 0.8 x ... z 0.3 x
i color points 'name' column specifying hue='name' specify color 1 point: b.
how specify color 1 point only, , have "hue" command take care of rest (where each point a-z has unique color)?
right command plot, hue name.
plot = sns.stripplot(x="id", y="value", hue="name", data=df, jitter=true, c=df['name'], s=7, linewidth=1)
you can replace 1 color in palette converting list of colors , replace 1 of colors other color of liking.
import pandas pd import numpy np;np.random.seed(42) import matplotlib.pyplot plt import seaborn sns letters = list(map(chr, range(ord('a'), ord('z')+1))) df = pd.dataframe({"name" : letters, "value": np.sort(np.random.rand(len(letters)))[::-1], "id" : ["x"]*len(letters)}) special_letter = "b" special_color = "indigo" levels = df["name"].unique() colors = sns.color_palette("hls", len(levels)) inx = list(levels).index(special_letter) colors[inx] = special_color ax = sns.stripplot(x="id", y="value", hue="name", data=df, jitter=true, s=7, palette=colors) ax.legend(ncol=3, bbox_to_anchor=(1.05,1), loc=2) ax.figure.subplots_adjust(right=0.6) plt.show()
instead of providing palette directly, 1 may (thanks @mwaskom pointing out) use dictionary of (hue name, color) pairs:
levels = df["name"].unique() colors = sns.color_palette("hls", len(levels)) colors = dict(zip(levels, colors)) colors[special_letter] = special_color
Comments
Post a Comment