我有一个 500 行的 pandas 数据框,其中有 2 列 x 和 y 引用表中的坐标。不过,我希望能够为 0 到 249 之间的点分配不同的颜色,然后为 250 到 499 之间的点分配不同的颜色,即一半是红色,一半是蓝色。我该怎么做?
代码:
diagram = pos_table.plot.scatter('x', 'y', c = 'turquoise', s = 4)
结果:
最佳答案
按 DataFrame.iloc
选择第一个和最后一个 250
行然后将 ax
传递给第二个 plot
:
length = len(pos_table)
half = length //
ax = pos_table.iloc[:250].plot.scatter('x', 'y', c = 'red', s = 4)
pos_table.iloc[250:].plot.scatter('x', 'y', c = 'blue', s = 4, ax=ax)
或动态计数值:
length = len(pos_table)
half = length // 2
ax = pos_table.iloc[:half].plot.scatter('x', 'y', c = 'red', s = 4)
pos_table.iloc[half:].plot.scatter('x', 'y', c = 'blue', s = 4, ax=ax)
示例:(更改了s
以便更好地查看)
pos_table = pd.DataFrame({'x':[2,3,4,6,2,4,6,8,5,7],
'y':[4,3,1,4,6,8,5,3,5,4]})
length = len(pos_table)
half = length // 2
ax = pos_table.iloc[:half].plot.scatter('x', 'y', c = 'red', s = 90)
pos_table.iloc[half:].plot.scatter('x', 'y', c = 'blue', s = 90, ax=ax)
关于python - pandas Dataframe 的不同颜色取决于索引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59696883/