Pandas 更改图例的颜色以匹配线图matplotlib

Pandas 更改图例的颜色以匹配线图matplotlib,pandas,matplotlib,colors,legend,line-plot,Pandas,Matplotlib,Colors,Legend,Line Plot,更新、编辑mcve版本的代码问题在第17行 我正在尝试从.csv文件中绘制多个折线图。我已设法将绘图中的默认颜色更改为tableau20颜色方案。生成图例时,图例中的颜色保持默认颜色。如果我尝试向图例代码部分添加颜色命令,则会出现错误。TypeError:init()获得意外的关键字参数“color”。是否有办法将图例颜色与主图表体中的颜色相匹配 下面是代码和几行数据 import pandas as pd from pandas import Series, DataFrame import

更新、编辑mcve版本的代码问题在第17行

我正在尝试从.csv文件中绘制多个折线图。我已设法将绘图中的默认颜色更改为tableau20颜色方案。生成图例时,图例中的颜色保持默认颜色。如果我尝试向图例代码部分添加颜色命令,则会出现错误。TypeError:init()获得意外的关键字参数“color”。是否有办法将图例颜色与主图表体中的颜色相匹配

下面是代码和几行数据

import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as 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)]
for i 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']
for 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 the next line when i try to tell it to use tableau20 by adding color=tableau20[rank]after fontsize
    ax.legend(lines[0:8], labels[0:8], loc='best', fontsize=8, color=tableau20[rank])
plt.show()


我无法运行您的确切代码,因为我无法访问您正在使用的csv,所以我只是猜测您的数据帧是什么样子。但是您应该能够根据需要调整以下代码。我试图使代码尽可能少,因为如果遇到问题,这将更容易进行故障排除

import pandas as pd
import matplotlib.pyplot as 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) for r, g, b in tableau20]

f, ax = plt.subplots()

for rank, column in enumerate(df.columns):        
    ax.plot(df.index, df[column], color=tableau20[rank], label=column)

ax.legend(loc='best')

legend
方法没有颜色参数,这就是为什么出现错误的原因,我不确定为什么标签中没有正确的颜色,但是您不需要明确说明颜色是什么,因为这些颜色包含在Axis对象中。您可能还希望在打印方法中定义标签,以便它们显示在图例中。祝你好运]

这段代码在这个阶段得到了我想要的结果。感谢所有帮助过我的人

import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
df=pd.read_csv('coveragechart.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)]
for i in range(len(tableau20)):    
    r, g, b = tableau20[i]    
    tableau20[i] = (r / 255., g / 255., b / 255.)
f, ax = plt.subplots()
for rank, column in enumerate(df.columns[1:8]):        
    ax.plot(df.index, df[column], color=tableau20[rank], label=column)
    plt.ylabel('Coverage')
    plt.xlabel('Position')
ax.legend(loc='best', fontsize=8)
plt.savefig('cov.png', bbox_inches='tight', dpi=300)
plt.show()

大家好,可能值得编写一个代码示例。理想情况下,人们可以复制您编写的代码,并重现您遇到的错误。您的示例中有很多代码与问题无关。我无法重现您的错误,但是我也无法运行您提供的代码,因为我没有您正在使用的csv。我想,完成了。谢谢,谢谢。把你的几条线和我的混在一起,让它工作起来。
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
df=pd.read_csv('coveragechart.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)]
for i in range(len(tableau20)):    
    r, g, b = tableau20[i]    
    tableau20[i] = (r / 255., g / 255., b / 255.)
f, ax = plt.subplots()
for rank, column in enumerate(df.columns[1:8]):        
    ax.plot(df.index, df[column], color=tableau20[rank], label=column)
    plt.ylabel('Coverage')
    plt.xlabel('Position')
ax.legend(loc='best', fontsize=8)
plt.savefig('cov.png', bbox_inches='tight', dpi=300)
plt.show()