【发布时间】:2018-10-18 20:28:42
【问题描述】:
我正在尝试从 pandas 数据框创建简单的散点图,将 column1 与 column2 进行对比,并使用 column3(布尔值)作为颜色和大小。然而,尽管颜色和大小数组按预期匹配,但它们适用于图中的不同点。
>>> colors = ['white' if not val else 'red' for val in df.column3.values]
>>> size = [5 if not val else 30 for val in df.column3.values]
>>> plt.scatter(df['column1'].values, df['column2'].values, c=colors, s=size)
>>> print zip(colors, size)
[('white', 5),
('white', 5),
('red', 30),
('red', 30),
('white', 5),
...]
为什么会这样?
E:澄清一下,color 和 size 数组中的每个元素都是正确配对的:
>>> for pair in zip(colors, size):
... if (pair[0] == 'white' and pair[1] != 5) or (pair[0] == 'red' and pair[1] != 30):
... print pair
[]
【问题讨论】:
-
你的意思是在第二行写“
in df.column**3**.values”吗? -
@DizietAsahi 是的,我做到了 - 谢谢!
-
如果您有大量元素,那不验证配对是否正确。你不能理智地目视检查。您需要使用断言进行过滤。类似
assert len(zip(colors, size)) == len([x for x in zip(colors, size) if (x[0] == 'white' and x[1] == 30) or (x[0] == 'red' and x[1] == 5)]) -
诚然,它们应该是相同的,因为它们都是以相同的方式生成的......
标签: python matplotlib seaborn