Reputation: 97
So, I was learning to build deep learning models, and during the visualization part I plotted a scatterplot where x and y axis were longitude and latitude, respectively, and the hue was equal to house prices. Although the price values were in float format, the legend is showing in different scaled value.
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')
Here, in the first picture, one can notice that x-axis scale is in formatted into scientific notation. Also, in the legend box, if I want to show 7,700,000 instead of 1.6, how I have to rescale both of them?
Data: kaggle house data
Upvotes: 0
Views: 801
Reputation: 6347
Here is an example how you can modify your legend based on the values of existing legend entries.
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')
You have to adapt this to your task.
Upvotes: 1