简体   繁体   English

在不同颜色的熊猫中绘制平行坐标

[英]Plotting Parallel Coordinates in pandas with different colours

I have a data frame like this: 我有一个像这样的数据框:

    0   1   2   3   4   5   6   7   8   9   Cluster
0   0.018420    0.003357    0.002626    -0.015603   -0.009005   -0.023671   -0.016316   0.066504    -0.039526   0.037820    For
1   0.017684    0.003434    -0.003338   -0.003904   -0.021871   -0.009454   -0.013772   -0.004610   -0.006150   -0.005746   For
2   0.018857    0.003987    0.001749    -0.019840   0.011184    -0.020451   0.082434    -0.008789   0.000449    0.005445    Against
3   0.020454    0.026437    0.036899    0.027168    -0.018483   -0.001076   0.005831    -0.002117   -0.011288   0.007491    For
4   0.018006    0.005365    0.001298    -0.006953   0.017034    0.006931    0.000268    0.001615    0.016707    -0.017798   Against

Df.columns
Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, u'Cluster'], dtype='object')

The last column 'cluster' specifies whether an observation belong to "For" cluster or "Against" cluster. 最后一列“集群”指定观察是属于“对于”集群还是对于“反对”集群。

I want to do a plot such that all observations which are for "For" are same coloured while which are for "Against" are same colour. 我想做一个图,使“ For”的所有观察结果都具有相同的颜色,而“ Against”的所有观察结果均具有相同的颜色。 There are 2740 obs, so some transparency in lines would also be required to give better visualisation. 有2740个Obs,因此还需要使线条具有一定的透明度以提供更好的可视化效果。

I did the following , but it is plotting the same colour red to both the classes even when I am specifying the colour option. 我做了以下操作,但是即使我指定了color选项,它也会在两个类上绘制相同的红色。

parallel_coordinates(Y_embed,'Cluster',color=["r" if c=="For" else "g" for c in Y_embed.Cluster])

Where Y_embed is my data frame. 其中Y_embed是我的数据框。

If I don't mention colour option it does plot two colours by default. 如果我不提及颜色选项,则默认情况下会绘制两种颜色。 But I want to put my own colour option. 但是我想放自己的颜色选项。

Any advice? 有什么建议吗?

The color argument is just a list of colors with one color for each cluster, not one color for each row. color参数只是颜色的列表,每个簇具有一种颜色,而不是每行一种颜色。 You can make a plot with green for one cluster and magenta for the other cluster like this: 您可以这样绘制一个图,其中一个群集为绿色,另一个群集为洋红色,如下所示:

parallel_coordinates(data,'Cluster',color=['g','m'])

To make the lines transparent you can use rgba values for the colors . 要使线条透明,可以为颜色使用rgba值。

parallel_coordinates(data,'Cluster',color=[[1,0,0,0.2],[0,1,0,0.9]])

Here the first set is red and partially transparent and the second is green and mostly opaque. 在这里,第一组是红色且部分透明,而第二组是绿色且大部分不透明。

You can control the transparency of the lines by repeating your calls to parallel_coordinates with a different value in each case, eg 您可以通过在每种情况下以不同的值重复对parallel_coordinates的调用来控制线条的透明度,例如

lowColorList=["k","k","y","y"]
midColorList=["c","b","g"]
topColorList=["r"]

plt.close()
plt.gcf().clear()    
fig, ax = plt.subplots()

parallel_coordinates(lowDf, "Cat",color=lowColorList, alpha=0.1)

parallel_coordinates(midDf, "Cat", color=midColorList, alpha=0.4)

parallel_coordinates(topDf, "Cat", color=topColorList, alpha=0.9)
# remove the pandas legend
plt.gca().legend_.remove()

plt.xlabel("Each Component of X is Shown on its Own Vertical Axis")
plt.ylabel("Values")
plt.title("Finding the Optimal Value of X")
# add new legend
topHandle =    mlines.Line2D([],[], color='red',   ls="-", label="Best")
midHandleOne = mlines.Line2D([],[], color='blue',  ls="-", label="Next Best")
lowHandle =    mlines.Line2D([],[], color='black', ls="-", label="Worst")
plt.legend(handles=[topHandle, midHandleOne,lowHandle],loc=1, prop={'size':10})

在不同alpha处具有多个调用的并行坐标

In the plot, there are actually 8 categories, but to keep the legend manageable I have some of them mapped to the same colors. 在该图中,实际上有8个类别,但是为了使图例易于管理,我将其中一些映射为相同的颜色。

Also, be aware that pandas assigns the colors from the color list in the order in which it encounters the categories, so if you have more than one color in a group of lines, you will need to use pandas.DataFrame.sort_values(...) on your category variable. 另外,请注意,pandas按遇到类别的顺序从颜色列表中分配颜色,因此,如果一组线中有多个颜色,则需要使用pandas.DataFrame.sort_values(。 。)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM