I ' mworkingwithaprettysimpleexample.icreatethreescatterplotsonthesamesetofaxes,andeachdatasetiplothasadifferentassociatetededed why is this?
import numpy as np
import matplotlib.pyplot as plt
t=NP.linspace (0,10,100 ) )。
x=NP.random.rand (100,3 ) ) ) ) ) ) )。
y=NP.random.rand (100,3 ) ) ) ) ) ) ) )。
colmaps=['Blues ',' Greys ',' Reds']
forIinrange(3) :
PLT.Scatter(x[:i],y[:i],c=t,cmap=colmaps[i],label=i ) ) ) ) ) ) ) )。
plt.legend (
plt.show () )
thisproducesafigurelikebelow :
iwashopingforthefirstlabeltobeblue,the second to be grey,and the third to be red,so they ' reassociatedwithecolormap,butitlokok
youcansetthelegendcolorsassuch :
import numpy as np
import matplotlib.pyplot as plt
t=NP.linspace (0,10,100 ) )。
x=NP.random.rand (100,3 ) ) ) ) ) ) )。
y=NP.random.rand (100,3 ) ) ) ) ) ) ) )。
colmaps=['Blues ',' Greys ',' Reds']
forIinrange(3) :
PLT.Scatter(x[:i],y[:i],c=t,cmap=colmaps[i],label=i ) ) ) ) ) ) ) )。
plt.legend (
legend=ax.get_legend (
legend.legend handles [0].set _ color (PLT.cm.blues ).8 ) )
legend.legend handles [1].set _ color (PLT.cm.greys ).8 ) )
legend.legend handles [2].set _ color (PLT.cm.reds ).8 ) )
plt.show () )
isetthecolorofeachlegendhandletoaspecificvalueintherespectivecolormap。
ifyoumakethescatterdot ' ssizelargeryoucanseethecolorandassociateindividualdotswiththelegendeasier.ialsosetjustonedotperscatatier rather than the default 3,and set the legend's alpha to 0.5,and alpha of scatter plot to 0.7。
.
forIinrange(3) :
PLT.Scatter(x[:i],y[:i],c=t,cmap=colmaps[i],label=i,s=200,alpha=0.7 ) )。
PLT.legend (标记比例=0.7,scatterpoints=1) )。
legend=ax.get_legend (
legend.legend handles [0].set _ color (PLT.cm.blues ).8 ) )
legend.legend handles [1].set _ color (PLT.cm.greys ).8 ) )
legend.legend handles [2].set _ color (PLT.cm.reds ).8 ) )
legend.get_frame(.set_alpha ) (0.5 ) )。
.