gpt4 book ai didi

python - 如何在 Pandas DataFrame 散点图中添加图例?

转载 作者:行者123 更新时间:2023-12-04 10:20:34 26 4
gpt4 key购买 nike

我有一个包含以下列感兴趣的 Pandas DataFrame:

['Relative Width', 'Relative Height', 'Object Name', 'Object ID']

df.plot(c='Object ID') 确定了 15 个对象名称和 15 种颜色。产生下图:

fig

我想显示一个带有 15 个对象名称的图例,如何做到这一点?
import matplotlib.pyplot as plt
from annotation_parsers import parse_voc_folder


def visualize_box_relative_sizes(folder_path, voc_conf, cache_file='data_set_labels.csv'):
frame = parse_voc_folder(folder_path, voc_conf, cache_file)
title = f'Relative width and height for {frame.shape[0]} boxes.'
frame.plot(
kind='scatter',
x='Relative Width',
y='Relative Height',
title=title,
c='Object ID',
colormap='gist_rainbow',
colorbar=False,
)
plt.show()

根据 wwnde 的建议,我将代码更改为以下内容:
def visualize_box_relative_sizes(folder_path, voc_conf, cache_file='data_set_labels.csv'):
frame = parse_voc_folder(folder_path, voc_conf, cache_file)
title = f'Relative width and height for {frame.shape[0]} boxes.'
sns.scatterplot(x=frame["Relative Width"], y=frame["Relative Height"], hue=frame["Object Name"])
plt.title(title)
plt.show()

产生以下结果:

enter image description here

最佳答案

请尝试

fig, ax = plt.subplots()

ax = sns.scatterplot(x="total_bill", y="tip",
hue="size", size="size",
data=tips)
ax.set_title('title')
plt.show()

这应该给你一个默认的彩色图例

关于python - 如何在 Pandas DataFrame 散点图中添加图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60875291/

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