使用matplotlib绘制多个系列时使用颜色列表

时间:2018-03-20 17:42:28

标签: python pandas matplotlib colors

我想将一个pandas DataFrame的多列添加到matplotlib轴,并使用颜色名称列表定义颜色。将列表传递给颜色参数时,我得到一个值错误:无效的RGBA参数。以下MWE重现此错误:

SELECT
    `activity`.`id` AS `activityID`,
    `activity`.`name`,
    ( SELECT COUNT(*) AS `count`
      FROM `quiz` WHERE `quiz`.`activityID` = `activity`.`id` ) +
    ( SELECT COUNT(*) AS `count`
      FROM `comment` WHERE `comment`.`activityID` = `activity`.`id` ) +
    ( SELECT COUNT(*) AS `count`
      FROM `question` WHERE `question`.`activityID` = `activity`.`id` ) AS `interactions`
FROM
    `activity`
WHERE 1
    AND `activity`.`eventID` = 1234
GROUP BY `activity`.`id`
ORDER BY `interactions` DESC

1 个答案:

答案 0 :(得分:3)

matplotlib plot的{​​{1}}参数只接受一种颜色。选项:

  • 一个简单的选择是使用颜色循环器

    color
  • 您也可以在绘图后循环划线,

    ax2.set_prop_cycle('color',colors )
    h = ax2.plot(df.index.values, df[['a','b','c']].values)
    
  • 最后考虑使用pandas plotting wrapper,

    h = ax2.plot(df.index.values, df[['a','b','c']].values)
    for line, color in zip(h,colors):
        line.set_color(color)
    

所有选项都会产生相同的情节。

enter image description here