[英]Scientific notation in seaborn plot and pivot table
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.我正在尝试使用 seaborn plot 到 plot 我的二手车数据集,我在 x 轴上得到科学记数法而不是实际数字。 It affects the pivot data too.
它也会影响 pivot 数据。 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)
.对于
scatterplot
,您可以使用ax.xaxis.get_major_formatter().set_scientific(False)
删除科学记数法和....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]
):对于颜色条,您需要访问它的
ax
,例如使用最后创建的颜色条( 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}'))
. PS:如果你想要千位分隔符,你可以使用
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).如另一个答案中所述,如果您还想更改注释的格式,则可以使用
sns.heatmap(...,fmt='.0f')
(或fmt=',.0f'
获得数千分隔器)。
Seaborn's heatmap
has a fmt
parameter that formats the annotations. Seaborn 的
heatmap
有一个fmt
参数,用于格式化注释。
You can get rid of the scientific notation in the heatmap specifying fmt='.2f'
or fmt='.0f'
您可以在指定
fmt='.2f'
或fmt='.0f'
的热图中摆脱科学记数法
sns.heatmap(df_hm, cmap='coolwarm', linewidths=.5, annot=True, ax=ax, fmt='.2f');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.