在 pandas/python 中绘制平行坐标图

3

我正在尝试使用Python中的pandas绘制以下高维数据:

http://i.stack.imgur.com/34nbR.jpg

这是我的代码:

import pandas
from pandas.tools.plotting import parallel_coordinates

data = pandas.read_csv('ParaCoords.csv')
parallel_coordinates(data,'Name')

代码无法绘制数据,并且Traceback错误以此结束:
Keyerror: 'Name'

parallel_coordinates中的第二个参数应该说/做什么? 我如何成功地绘制数据?


我认为第二个参数必须是您要用于绘图的列的名称。这就是为什么在iris.data中他们使用“Name”的原因。 - ysearka
无论我在第二个参数的位置(即“列名”)使用哪个字符串,都会导致函数错误。 - C. Stucki
2个回答

1
第二个参数应该是定义class的列名。 想想['dog', 'dog', 'cat', 'bird', 'cat', 'dog']
在线示例中,他们使用'Name'作为第二个参数,因为那是定义鸢尾花名称的列名。

文档

Signature: parallel_coordinates(*args, **kwargs)
Docstring:
Parallel coordinates plotting.

Parameters
----------
frame: DataFrame
class_column: str
    Column name containing class names
cols: list, optional
    A list of column names to use
ax: matplotlib.axis, optional
    matplotlib axis object
color: list or tuple, optional
    Colors to use for the different classes
use_columns: bool, optional
    If true, columns will be used as xticks
xticks: list or tuple, optional
    A list of values to use for xticks
colormap: str or matplotlib colormap, default None
    Colormap to use for line colors.
axvlines: bool, optional
    If true, vertical lines will be added at each xtick
axvlines_kwds: keywords, optional
    Options to be passed to axvline method for vertical lines
kwds: keywords
    Options to pass to matplotlib plotting method

我明白了!所以,y是我的因变量;而x1、x2、x3和x4是我的自变量。第二个参数应该是“y”;或者它可以是“x1”、“x2”等。 - C. Stucki
你好!在平行坐标图中,有没有自定义图例的选项? - cucurbit
1
@cucurbit 你可以像对待任何 matplotlib 绘图一样对其进行自定义。基本上,将绘图的返回值分配给一个变量。这将是一个 axes 对象。然后从那里操纵。你会想要搜索 matplotlib 图例 - piRSquared

0

你从 UCI 下载 的 iris.data 文件没有表头。为了使 Pandas 示例正常工作,你必须显式地将列名分配为表头:

from pandas.tools.plotting import parallel_coordinates
# The iris.data file from UCI does not have headers,
# so we have to assign the column names explicitly.
data = pd.read_csv("data-iris-for-pandas/iris.data")
data.columns=["x1","x2","x3","x4","Name"]
plt.figure()
parallel_coordinates(data,"Name")

Pandas Parallel Coordinates Example

基本上,pandas文档不完整。有人将列名放入数据框中,但没有告诉我们。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接