[英]How to add a legend in a pandas DataFrame scatter plot?
我有一個包含以下列感興趣的 Pandas DataFrame:
['Relative Width', 'Relative Height', 'Object Name', 'Object ID']
使用df.plot(c='Object ID')
確定的 15 個對象名稱有 15 種顏色,生成下圖:
我想顯示一個帶有 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()
產生以下結果:
請嘗試
fig, ax = plt.subplots()
ax = sns.scatterplot(x="total_bill", y="tip",
hue="size", size="size",
data=tips)
ax.set_title('title')
plt.show()
這應該給你一個默認的彩色圖例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.