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?