Change color of legend to match line plot matplotlib pandas -


update, edited code mcve version problem in line 17.

i trying plot multiple line chart .csv file. have managed change default colors in plot tableau20 color scheme. when generate legend colors in legend remain default colors. if try add color command legend code section errors. typeerror: init() got unexpected keyword argument 'color'. there way match legend colors colors in main chart body?

here code , few lines of data.

import pandas pd pandas import series, dataframe import matplotlib.pyplot plt df=pd.read_csv('cch30.csv') tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),                  (44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150)] in range(len(tableau20)):         r, g, b = tableau20[i]         tableau20[i] = (r / 255., g / 255., b / 255.) ax = df.plot(kind='line', x=df.columns[0],y=df.columns[1:8]) species= ['a. bellonorium', 'a. fuscolingua', 'a. mucronata', 'a. depressa', 'a. novazelandia', 'a. spp', 'a. australis'] rank, column in enumerate(species):             plt.plot(df.position.values,                 df[column.replace("\n", " ")].values,                 lw=1, color=tableau20[rank])     lines, labels = ax.get_legend_handles_labels()     #problem in next line when try tell use tableau20 adding color=tableau20[rank]after fontsize     ax.legend(lines[0:8], labels[0:8], loc='best', fontsize=8, color=tableau20[rank]) plt.show() 

position,a. bellonorium,a. fuscolingua,a. mucronata,a. depressa,a. novazelandia,a. spp,a. australis 1,17,9,33,22,15,20,78 2,17,9,33,21,14,22,77 3,17,9,34,20,14,23,78 4,17,9,35,21,12,23,79 5,17,9,34,22,12,24,75 6,17,9,34,22,13,24,75 7,17,9,34,22,13,24,74 8,17,9,34,22,15,24,76 9,16,9,36,20,14,24,76 10,16,9,36,20,15,26,75 11,16,9,37,20,15,27,74 12,16,9,36,21,15,26,74 13,16,9,34,21,15,27,75 14,16,9,34,23,15,27,75 15,16,9,34,24,16,26,75 16,16,9,34,24,16,28,76 17,16,9,33,24,16,28,77 18,15,9,34,24,14,28,77 19,15,9,32,25,14,28,77 20,15,9,32,25,13,28,77 21,15,9,31,25,13,29,79 22,15,9,31,25,13,29,79 23,15,9,32,25,13,29,78 24,15,9,31,25,12,29,79 25,15,9,30,25,12,29,78 26,15,9,30,25,12,28,79 27,15,9,29,24,13,30,80 28,15,9,30,24,13,30,80 29,14,9,29,23,11,30,77 

i not able run exact code because not have access csv using i'm guessing dataframe looks like. should able adapt following code need. tried make code minimum possible easier troubleshoot if run issues

import pandas pd import matplotlib.pyplot plt  df = pd.dataframe([[1, 1, 1], [.5, 1, 2]], columns=['a. bellonorium', 'a. fuscolingua', 'a. mucronata'])  tableau20 = [(31, 119, 180), (255, 152, 150), (255, 127, 14)] tableau20 = [(r/255, g/255, b/255) r, g, b in tableau20]  f, ax = plt.subplots()  rank, column in enumerate(df.columns):             ax.plot(df.index, df[column], color=tableau20[rank], label=column)  ax.legend(loc='best') 

the legend method doesn't have color parameter why getting error, i'm not sure why weren't getting correct colors in labels, don't need explicitly state colors are, contained in axes object. may want define labels in plotting method show in legend. luck![enter image description here]1


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -