在轴值和图例的 seaborn 中自动缩放

Autoscaling in seaborn of axis values and legend

所以,我在学习建立深度学习模型,在可视化部分我绘制了一个散点图,其中 x 轴和 y 轴分别是经度和纬度,色调等于房价。尽管价格值采用浮点格式,但图例以不同的比例值显示。

import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv("../DATA/kc_house_data.csv")
sns.histplot(df['price'])

non_top_1_perct = df.sort_values('price',ascending=False).iloc[216:]
plt.figure(figsize=(12,8))
sns.scatterplot(x= 'long', y= 'lat', data =non_top_1_perct,
           edgecolor = None, alpha = 0.2, palette  ='RdYlGn', hue='price')

在这里,在第一张图片中,可以注意到x轴刻度被格式化为科学记数法。此外,在图例框中,如果我想显示 7,700,000 而不是 1.6,我必须如何重新缩放它们?

数据:kaggle house data

这是一个示例,您可以如何根据现有图例条目的值修改图例。

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'lat': [4, 24, 31, 2, 3],
    'long': [3, 5, 5, 6, 7], 
    'price':[35e6, 54899767, 57890789, 62890798, 70897871]
})
ax = sns.scatterplot(y="lat", x="long", data=df, hue='price')

# modify legend entries
handles = ax.get_legend().legendHandles
ax.legend(handles, [str(round(float(v.get_label())/1e6,1))+'m' for v in handles] , loc='upper left')

# disable scientific notation on y axis
ax.ticklabel_format(style='plain', axis='y')

你必须根据你的任务调整它。