0

I've three 1-D arrays,

BT1=np.random.uniform(180,300,size=[1,100]) #x
BT2=np.random.uniform(180,300,size=[1,100]) #y
color_code=np.random.choice(0,1,size-[1,100]) #color code

fig,ax=plt.subplots()
scatter=ax.scatter(BT1,BT2,c=color_code)
ax.legend(handles=scatter.legend_elements()[0], labels=["Clear","Cloudy"], title="Cloud Mask")
ax.set_xlabel("TIR1 sample BT")
ax.set_ylabel("TIR2 sample BT")

Array color_code contains 0,1 which is used for color coding the scatter plot.

I need 0 in array to be assigned label "Clear" and 1 to be assigned label "Cloudy".

Basically, I need "0,Clear" and "1,Cloudy" as the correct label to appear against color Blue and Red respectively.

Problem:- The labels passed to the scatter are ["Clear","Cloudy"]. So matplotlib assigns the first occurring element in color_code the label "Clear" and the 2nd element the label "Cloudy".

So, if color_code=Array(1,0,0,1.....), 1 would be assigned the label Clear even though it's wrong.

I've tried the method below but it didn't work

leg = ax.get_legend()
hl_dict = {handle.get_label(): handle for handle in leg.legendHandles}
hl_dict['0'].set_color('blue')
hl_dict['1'].set_color('red')

How can I add the link the label to the data correctly?

Pixel_Bear
  • 97
  • 1
  • 1
  • 11
  • By far the easiest way, is to use Seaborn's scatter plot, e.g. `sns.scatterplot(x=BT1, y=BT2, hue=pd.Categorical.from_codes(codes=color_code, categories=['Clear', 'Cloudy'], ordered=True))`. Instead of `pd.Categorical.from_codes` you could also directly provide a list of strings. Note that `BT1`, `BT2` and `color_code` should be one-dimensional (so `size=100` instead of `size=[1,00]`. – JohanC Nov 29 '22 at 07:12
  • @JohanC I tried this one, it works. Props to you for the answer. Why exactly ```seaborn.scatterplot``` works while ```plt.scatter``` does not? – Pixel_Bear Nov 29 '22 at 12:37
  • @JohanC I want to set the color ```red``` for ```"Cloudy"``` and ```green``` for ```"Clear"```. The ```palette``` method isn't working together with ```Categorical.from_codes()```. It throws an error ```Categorical.from_codes() got an unexpected keyword argument 'palette'``` – Pixel_Bear Nov 29 '22 at 14:19
  • Well, you need to add the `palette=` parameter to `sns.scatterplot(...., palette={'Clear':'green', 'Cloudy':'red'})`, not to `pd.Categorical.from_codes(....)`. – JohanC Nov 29 '22 at 14:41
  • `sns.scatterplot(...)` is a much more high-level function, build on `plt.scatter(...)`. – JohanC Nov 29 '22 at 14:43
  • @JohanC I seem to be stuck. I put the color code against "0"/"1" and "Clear"/"Cloudy", one at a time, but they didn't work. This is the code I'm using currently ```sns.scatterplot(ax=ax, x=BT1, y=BT2, hue=color_code, palette={"Cloudy":'green', "Clear":'red'}, categories=['Clear', 'Cloudy'], ordered=True)``` . The error is ```The palette dictionary is missing keys: {0, 1}``` – Pixel_Bear Nov 29 '22 at 16:29
  • 1
    Did you try `sns.scatterplot(x=BT1, y=BT2, hue=pd.Categorical.from_codes(codes=color_code, categories=['Clear', 'Cloudy'], ordered=True), palette={"Cloudy":'green', "Clear":'red'})`? – JohanC Nov 29 '22 at 16:43
  • @JohanC Yes, I did. Something weird is happening. It's compiling but now showing anything, even after the command ```plt.show()```. – Pixel_Bear Nov 29 '22 at 16:53
  • Are you running the latest seaborn version? Did you try to restart your environment? – JohanC Nov 29 '22 at 16:55
  • @JohanC Yes, latest version ```0.12.1```. Restarted the environment, did a correction, it worked. I was passing an axis in the ```ax=```(first declare an axis with ```subplots```,then pass here) argument inside the ```scatterplot```. It's working now with proper labels and colors. Thank you for your help. Seems like I can't pass an axis for now but I'll work it out. – Pixel_Bear Nov 29 '22 at 17:02

0 Answers0