我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用matplotlib.colors.cnames()。
def test_standard_colors_all(self): import matplotlib.colors as colors # multiple colors like mediumaquamarine for c in colors.cnames: result = plotting._get_standard_colors(num_colors=1, color=c) self.assertEqual(result, [c]) result = plotting._get_standard_colors(num_colors=1, color=[c]) self.assertEqual(result, [c]) result = plotting._get_standard_colors(num_colors=3, color=c) self.assertEqual(result, [c] * 3) result = plotting._get_standard_colors(num_colors=3, color=[c]) self.assertEqual(result, [c] * 3) # single letter colors like k for c in colors.ColorConverter.colors: result = plotting._get_standard_colors(num_colors=1, color=c) self.assertEqual(result, [c]) result = plotting._get_standard_colors(num_colors=1, color=[c]) self.assertEqual(result, [c]) result = plotting._get_standard_colors(num_colors=3, color=c) self.assertEqual(result, [c] * 3) result = plotting._get_standard_colors(num_colors=3, color=[c]) self.assertEqual(result, [c] * 3)
def init_colors(self): 'initialize all_colors' self.all_colors = [] # remove any colors with 'white' or 'yellow in the name skip_colors_substrings = ['white', 'yellow'] skip_colors_exact = ['black', 'red', 'blue'] for col in colors.cnames: skip = False for col_substring in skip_colors_substrings: if col_substring in col: skip = True break if not skip and not col in skip_colors_exact: self.all_colors.append(col) # we'll re-add these later; remove them before shuffling first_colors = ['lime', 'cyan', 'orange', 'magenta', 'green'] for col in first_colors: self.all_colors.remove(col) # deterministic shuffle of all remaining colors random.seed(0) random.shuffle(self.all_colors) # prepend first_colors so they get used first self.all_colors = first_colors + self.all_colors
def build_chart(Size,R,w1,xr,xl,yu,yd,xc,yc,totx,toty,name,prints,doc): # build layout sketch, name = filename to build, prints = binary variable - print all values in the file?, # doc - binary variable - create layout sketch file and text file with model results? Area = [ (xr[i]-xl[i])*(yu[i]-yd[i]) for i in range(len(Size))] if doc: fi = open('txt\\'+name+'.txt','w') #open xls file as given colors = list(six.iteritems(cols.cnames)) m = max(totx,toty)*1.1 #size of square fig1 = plt.figure() ax1 = fig1.add_subplot(111, aspect='equal') ax1.axes.xaxis.set_ticklabels([]) ax1.axes.yaxis.set_ticklabels([]) ax1.add_patch( patches.Rectangle( #create rectangle (0, 0), # (x,y) totx/m, # width toty/m, # height facecolor = 'w' ) ) for i in range(len(xr)): #draw each dep acording to its cordintes ax1.add_patch( patches.Rectangle( (xl[i]/m, yd[i]/m), # (x,y) xr[i]/m-xl[i]/m, # width yu[i]/m-yd[i]/m, # height facecolor = colors[i%len(colors)][1] ) ) if i+1 in [1,19,29]: c='white' else: c='black' plt.text((xl[i]+xc[i])/(2*m), (yc[i]+yd[i])/(2*m), str(i+1), fontsize = 8, color = c) # print dep number if prints: # print all results print i+1,'(',round(xc[i],2),',',round(yc[i],2),') m2:',round(max((xr[i]-xl[i])/(yu[i]-yd[i]),(yu[i]-yd[i])/(xr[i]-xl[i])),2), 'Area:', round(Area[i],2), '(',round((xr[i]-xl[i]),2),'x',round((yu[i]-yd[i]),2),')',round(Area[i]*100/Size[i],2),'% of wanted' if doc: #write all data into txt file fi.write(str(i+1)+' ('+str(round(xc[i],2))+','+str(round(yc[i],2))+') m2:'+str(round(max((xr[i]-xl[i])/(yu[i]-yd[i]),(yu[i]-yd[i])/(xr[i]-xl[i])),2))+ ' Area:'+str(round(Area[i],2))+ ' ('+str(round((xr[i]-xl[i]),2))+'x'+str(round((yu[i]-yd[i]),2))+') '+str(round(Area[i]*100/Size[i],2))+'% of wanted \n') if Area[i]/Size[i]<0.9: print '************** Size Error ***************' plt.show() #show layout sketch if doc: #write all data into txt file fig1.savefig('png\\'+name+'.png', dpi=200, bbox_inches='tight') res = result(Size,R,w1,xr,xl,yu,yd,xc,yc,totx,toty) fi.write( 'Score:'+str(res[0])+ ' Dist:'+str(res[1])+' Area:'+str(res[2])+' ('+str(totx)+'x'+str(toty)+') w1:'+str(w1)+'\n') fi.close