Pandas Dataframe:按列名绘制颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47104862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:43:38  来源:igfitidea点击:

Pandas Dataframe: plot colors by column name

pythonpandasmatplotlib

提问by Joooeey

I'm plotting a Pandas DataFrame with a few lines, each in a specific color (specified by rgb value). I'm looking for a way to make my code more readable by assigning the plot line colors directly to DataFrame column names instead of listing them in sequence.

我正在用几行绘制 Pandas DataFrame,每行都有特定的颜色(由 rgb 值指定)。我正在寻找一种方法,通过将绘图线颜色直接分配给 DataFrame 列名称而不是按顺序列出它们,从而使我的代码更具可读性。

I know I can do this:

我知道我可以这样做:

import pandas as pd

df = pd.DataFrame(columns=['red zero line', 'blue one line'], data=[[0, 1], [0, 1]])
df.plot(colors = ['#BB0000', '#0000BB']) # red amd blue

but with a lot more than two lines, I'd really like to be able to specify the colors by column header, to make the code easy to maintain. Such as this:

但是有很多多于两行,我真的很希望能够按列标题指定颜色,以使代码易于维护。比如这个:

df.plot(colors = {'red zero line': '#FF0000', 'blue one line': '#0000FF'})

The colors keyword can't actually be a dictionary though. (Technically it's type-converted to list, which yields a list of the column labels.)

但是,colors 关键字实际上不能是字典。(从技术上讲,它被类型转换为列表,从而产生一个列标签列表。)

I understand that pd.DataFrame.plotinherits from matplotlib.pyplot.plotbut I can't find the documentation for the colorskeyword. Neither of the documentations for the two methods lists such a keyword.

我知道pd.DataFrame.plot继承自matplotlib.pyplot.plot但我找不到colors关键字的文档。这两种方法的文档都没有列出这样的关键字。

回答by James

If you create a dictionary mapping the column names to colors, you can build the color list on the fly using a list comprehension where you just getthe color from the column name. This also allows you to specify a default color in case you missed a column.

如果您创建一个将列名映射到颜色的字典,您可以使用列表理解来动态构建颜色列表,其中您只需要get列名中的颜色。这也允许您指定默认颜色,以防您错过一列。

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame([[0, 1, 2], [0, 1, 2]], 
                  columns=['red zero line', 'blue one line', 'extra'])

color_dict = {'red zero line': '#FF0000', 'blue one line': '#0000FF'}

# use get to specify dark gray as the default color.
df.plot(color=[color_dict.get(x, '#333333') for x in df.columns])
plt.show()

enter image description here

在此处输入图片说明

回答by andrew_reece

You can specify the order of the columns before plotting with df[cols]:

您可以在绘图之前指定列的顺序df[cols]

import pandas as pd

cols = ['red zero line', 'blue one line', 'green two line']
colors = ['#BB0000', '#0000BB', 'green']
df = pd.DataFrame(columns=cols, data=[[0, 1, 2], [0, 1, 2], [0, 1, 3]])

df[cols].plot(colors = colors)

example plot

示例图

If you want to be sure columns and colors are strictly paired, you can always just zipahead of time:

如果您想确保列和颜色严格配对,您可以随时zip提前:

columns_and_colors = zip(cols, colors)
df[cols].plot(colors = [cc[1] for cc in columns_and_colors])