gpt4 book ai didi

python - 来自 pandas 数据框的不同大小、标记和颜色的散点图

转载 作者:太空狗 更新时间:2023-10-29 22:04:47 26 4
gpt4 key购买 nike

我正在尝试为每个点绘制速度超过米的散点图,其中标记表示不同的类型,大小表示不同的重量,颜色表示一个点超过 10 分钟刻度的时间。但是,到目前为止我只能按大小绘制。

非常感谢任何帮助。

x = {'speed': [10, 15, 20, 18, 19], 'meters' : [122, 150, 190, 230, 300], 'type': ['phone', 'phone', 'gps', 'gps', 'car'], 'weight': [0.2, 0.3, 0.1, 0.85, 0.0], 'old': [1, 2, 4, 5, 8]}

m = pd.DataFrame(x)

plt.scatter(m.meters, m.speed, s = 30* m.weight)

mkr_dict = {'gps': 'x', 'phone': '+', 'car': 'o'}

meters speed type weight old
0 122 10 phone 0.20 1
1 150 15 phone 0.30 2
2 190 20 gps 0.10 4
3 230 18 gps 0.85 5
4 300 19 car 0.00 8

更新的问题:

我正在尝试将 colorbar 添加到基于旧的色标。当我针对整个数据集进行绘图但在尝试为每个子集添加标记后失败时它起作用了。有什么想法吗?

plt.scatter(m.meters, m.speed, s = 30* m.weight, c=m.old)
cbar = plt.colorbar(ticks = [0, 5, 10])
cbar.ax.set_yticklabels(['New','5mins', '10mins'])

TypeError:您必须先为可映射设置 set_array

最佳答案

scatter 一次只能做一种标记,所以你必须分别绘制不同的类型。幸运的是,pandas 让这一切变得简单:

import matplotlib.pyplot as plt
import pandas as pd
x = {'speed': [10, 15, 20, 18, 19],
'meters' : [122, 150, 190, 230, 300],
'type': ['phone', 'phone', 'gps', 'gps', 'car'],
'weight': [0.2, 0.3, 0.1, 0.85, 0.0],
'old': [1, 2, 4, 5, 8]}

m = pd.DataFrame(x)
mkr_dict = {'gps': 'x', 'phone': '+', 'car': 'o'}
for kind in mkr_dict:
d = m[m.type==kind]
plt.scatter(d.meters, d.speed,
s = 100* d.weight,
c = d.old,
marker = mkr_dict[kind])
plt.show()

enter image description here

.... 车在哪儿?好吧,原始测试数据中的权重是 0.0,而我们使用的是标记大小的权重,所以:看不到它。

关于python - 来自 pandas 数据框的不同大小、标记和颜色的散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30313882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com