python - 取决于索引号的熊猫数据框的不同颜色
问题描述
我有一个 500 行的 pandas 数据框,其中有 2 列 x 和 y 指的是表中的坐标。但是我希望能够为从 0 到 249 的点分配不同的颜色,然后是 250 到 499,例如一半是红色,一半是蓝色。我该怎么做?
编码:
diagram = pos_table.plot.scatter('x', 'y', c = 'turquoise', s = 4)
结果:
解决方案
选择第一行和最后250
一行DataFrame.iloc
,然后传递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)
推荐阅读
- ios-simulator - 使用模拟器触发重大位置更改事件的具体方法是什么?
- c# - 在 Unity 中读取串行端口“COM3”。连接建立成功,但未调用DataReceiveHandler
- javascript - 在等待之后调用 setState 时,状态立即可用
- pandas - 为什么这个追星族不能总结价格?
- rpm - 如何在规范文件中使用 $RPM_INSTALL_PREFIX?
- python - 如何通过指定初始和最终点通过 3d 矩阵(ndarray 3-dim)提取线轮廓(光线跟踪线)
- python - 从熊猫数据框中提取无向图
- python - 如何每周自动将数据添加到数据库?
- java - 如何使用 Maven 将依赖文件、放入目标中包含到 Java 代码中?
- python - 如何在 Python 中编写具有 sudo 权限的文件?