我想为数据框中的每个组散布不同颜色的熊猫数据框。当我在一个数据框组中恰好有4行时,以下代码对我来说很好,例外。预定义的颜色未应用于绘图。
请参见以下示例:
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()
在上面的示例中,我对m = 6的组有4个值。如您在绘图输出和打印的面色中所见,组m = 6的颜色与颜色图不匹配。
输出:
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 ]]
例如组m = 6中有5个成员,一切看起来都很好:
我该如何解决?
答案 0 :(得分:3)
请注意,c不应为单个数字RGB或RGBA序列,因为它与要进行颜色映射的值数组无法区分。如果要为所有点指定相同的RGB或RGBA值,请使用具有一行的二维数组。
因此
c = [colorMap[m - 1]]
按预期工作。
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 ]]