Home > Enterprise >  Scatter plot order by hue
Scatter plot order by hue

Time:04-23

Is there a way to set parameter to order the hue value in a scatter plot? For example I want to only show 3 values and have 3 different colors . In the chart below seaborn automatically makes 6 values and assigned a color. I'm trying to do something like this: if X > 0.06 red, between 0.06 and 0.08 yellow and everything above that red . I read the documentation and still cant figure out how to achieve this

My code:

plt.figure(figsize=(12,8))
sns.scatterplot(x='lng_dms_to_lng_dd',y='lat_dms_to_lat_dd',data=store_avg,hue='MOP_GC_PCT', size="MOP_GC_PCT",sizes=(20, 200))

Scatter plot

CodePudding user response:

You can try creating a new column with a new label for your conditions and plot the graph with hue as the new column.

# create new column and label with defined conditions
store_avg.loc[(store_avg['MOP_GC_PCT'] >= 0.09), 'MOP_GC_PCT_bins'] = 'more than 0.08'
store_avg.loc[(store_avg['MOP_GC_PCT'] >= 0.06) & (store_avg['col1'] <= 0.08), 'MOP_GC_PCT_bins'] = 'between 0.06 and 0.08'
store_avg.loc[(store_avg['MOP_GC_PCT'] <= 0.05), 'MOP_GC_PCT_bins'] = 'less than 0.06'

# your plot code
plt.figure(figsize=(12,8))
sns.scatterplot(x='lng_dms_to_lng_dd',y='lat_dms_to_lat_dd',data=store_avg,hue='MOP_GC_PCT_bins', size="MOP_GC_PCT",sizes=(20, 200))

You can also define your own colour palette.

# define palette
palette = {'more than 0.08':'red',
           'between 0.06 and 0.08':'yellow', 
           'less than 0.06':'red'}

# plot with custom palette
sns.scatterplot(x='lng_dms_to_lng_dd',y='lat_dms_to_lat_dd',data=store_avg,hue='MOP_GC_PCT_bins', size="MOP_GC_PCT",sizes=(20, 200), palette=palette)
  • Related