简体   繁体   English

matplotlib分组数据帧的散点图中的错误颜色

[英]matplotlib wrong colors in scatter plot of grouped dataframe

I want to scatter plot a pandas dataframe with a different color for each group in the dataframe. 我想为数据帧中的每个组散布一个具有不同颜色的pandas数据帧。 The below code worked fine for me except when I have exactly 4 rows in a dataframe group. 下面的代码对我来说很好, 除非我在数据帧组中只有4行。 The predefined colors where not applied to the plot. 未应用于绘图的预定义颜色。

Pls see the following example: 请参阅以下示例:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

data = [
[3.28, 1, 0.202],
[3.05, 4, 0.006],
[1.20, 4, 0.234],
[3.44, 4, 0.052],
#[3.47, 4, 0.007],
#[2.79, 4, 0.029],
[3.44, 5, 0.0261],
[3.92, 5, 0.008],
[0.97, 5, 0.077],
#[1.58, 5, 0.043],
[0.03, 6, 0.441],
[0.75, 6, 0.099],
[0.68, 6, 0.093],
[0.68, 6, 0.083],
#[0.68, 6, 0.103], # uncomment this line and it works as expected
#[1.12, 6, 0.057]
]
columns = ['time', 'm', 'diff']
df = pd.DataFrame(data, columns=columns)
columns = ['time', 'm', 'diff']
df = pd.DataFrame(data, columns=columns)

colorMap = plt.cm.hsv(np.linspace(0, 1, 7))
fig, ax = plt.subplots()
print 'colormap'
for m, data in df.groupby('m'):
    print m, colorMap[m - 1]
    ax.scatter('time', 'diff', alpha=0.6, s=8*m**2, data=data,label=m, c= colorMap[m - 1])
vals = ax.get_yticks()
ax.set_yticklabels(['{:3.2f}%'.format(x*100) for x in vals])
ax.legend(title='m')
ax.grid(True)
plt.gcf().subplots_adjust(left=0.15)
handles, labels = ax.get_legend_handles_labels()
print 'facecolors'
for h in handles:
    print h.get_label(), h.get_facecolor()
plt.show()

In the above example I have 4 values for the group m=6. 在上面的例子中,我有4个值为m = 6的组。 As you can see in the plot output and the printed facecolors, the color for group m=6 does not match to the colormap. 正如您在绘图输出和打印的面部颜色中看到的,组m = 6的颜色与颜色图不匹配。

Output: 输出:

colormap
1 [ 1.  0.  0.  1.]
4 [ 0.          1.          0.96470316  1.        ]
5 [ 0.          0.06250197  1.          1.        ]
6 [ 0.93345491  0.          1.          1.        ]
facecolors
1 [[ 1.   0.   0.   0.6]]
4 [[ 0.          1.          0.96470316  0.6       ]]
5 [[ 0.          0.06250197  1.          0.6       ]]
6 [[ 0.12156863  0.46666667  0.70588235  0.6       ]]

在此输入图像描述

Eg with 5 members in group m=6 everything looks fine: 例如,在m = 6组中有5名成员,一切看起来都很好:

在此输入图像描述

How can I fix this? 我怎样才能解决这个问题?

The scatter documentation states scatter文档说明

Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. 请注意,c不应该是单个数字RGB或RGBA序列,因为它与要进行颜色映射的值数组无法区分。 If you want to specify the same RGB or RGBA value for all points, use a 2-D array with a single row. 如果要为所有点指定相同的RGB或RGBA值,请使用具有单行的二维数组。

Hence 于是

c = [colorMap[m - 1]] 

works as expected. 按预期工作。

colormap
1 [ 1.  0.  0.  1.]
4 [ 0.          1.          0.96470316  1.        ]
5 [ 0.          0.06250197  1.          1.        ]
6 [ 0.93345491  0.          1.          1.        ]
facecolors
1 [[ 1.   0.   0.   0.6]]
4 [[ 0.          1.          0.96470316  0.6       ]]
5 [[ 0.          0.06250197  1.          0.6       ]]
6 [[ 0.93345491  0.          1.          0.6       ]]

在此输入图像描述

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

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