I m trying to use seaborn plot to plot my usedcar data set and I m getting scientific notation in the x axis instead of the actual number. It affects the pivot data too. Please suggest a way to remove the scientific notation.
Code:
plt.figure(figsize=(15,7))
sns.scatterplot(x='Price_in_Lac',y='Kilometers_Driven',hue='Location',data=df)
---------------------------------------------------------------
df_hm =df.pivot_table(index = 'Brand',columns ='Location',values ="Price_in_Lac",aggfunc=np.median)
f, ax = plt.subplots(figsize=(15,10))
sns.heatmap(df_hm, cmap='coolwarm',linewidths=.5, annot=True, ax=ax);
For the scatterplot
you can use ax.xaxis.get_major_formatter().set_scientific(False)
to remove the scientific notation and ....set_useOffset(False)
.
Here is an example using the penguins dataset with lengths converted to nanometers to get larger numbers:
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter
import seaborn as sns
penguins = sns.load_dataset("penguins")
penguins["flipper_length_nm"] = penguins["flipper_length_mm"] * 1000000
penguins["bill_length_nm"] = penguins["bill_length_mm"] * 1000000
plt.figure(figsize=(15, 7))
ax = sns.scatterplot(x='flipper_length_nm', y='bill_length_nm', hue='species', data=penguins)
ax.xaxis.get_major_formatter().set_scientific(False)
ax.xaxis.get_major_formatter().set_useOffset(False)
ax.yaxis.get_major_formatter().set_scientific(False)
ax.yaxis.get_major_formatter().set_useOffset(False)
At the left with the original formatting, at the right with the scientific notation removed:
For the colorbar, you need to get access to its ax
, for example using the last created one ( fig.axes[-1]
):
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter
import seaborn as sns
import numpy as np
penguins = sns.load_dataset("penguins")
penguins["flipper_length_nm"] = penguins["flipper_length_mm"] * 1000000
penguins["bill_length_nm"] = penguins["bill_length_mm"] * 1000000
df_hm = penguins.pivot_table(index='sex', columns='species', values="flipper_length_nm", aggfunc=np.median)
fig, ax = plt.subplots(figsize=(10, 5))
sns.heatmap(df_hm, cmap='coolwarm', linewidths=.5, annot=True, ax=ax)
cbar_ax = fig.axes[-1]
cbar_ax.yaxis.get_major_formatter().set_scientific(False)
cbar_ax.yaxis.get_major_formatter().set_useOffset(False)
plt.tight_layout()
plt.show()
PS: If you want a thousands separator, you can use ax.yaxis.set_major_formatter(StrMethodFormatter('{x:,.0f}'))
.
As mentioned in the other answer, if you also want to change the format of the annotations, you can use sns.heatmap(...,fmt='.0f')
(or fmt=',.0f'
to get a thousands separator).
Seaborn's heatmap
has a fmt
parameter that formats the annotations.
See the doc here .
You can get rid of the scientific notation in the heatmap specifying fmt='.2f'
or fmt='.0f'
sns.heatmap(df_hm, cmap='coolwarm', linewidths=.5, annot=True, ax=ax, fmt='.2f');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.