简体   繁体   中英

How to use seaborn scientific notation facetgrid and catplot?

How can I force scientific notation on the axes of a seaborn.FacetGrid or seaborn.catplot again?

import seaborn as sns
sns.set_theme(style="ticks")
exercise = sns.load_dataset("exercise")
g = sns.catplot(x="time", y="pulse", hue="kind", data=exercise)

Thanks!

The key is here:

plt.ticklabel_format(axis="x", style="sci", scilimits=(0,0))

To use the specified format of scientific notation:

plt.ticklabel_format(axis="y", style="sci", scilimits=(0,0))

Complete code:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
a = pd.DataFrame({"x": np.random.randn(500),
                   "y": np.random.randn(500)*200})

import seaborn as sns
sns.set_theme(style="ticks")

g = sns.catplot( data=a)
plt.ticklabel_format(axis="y", style="sci", scilimits=(0,0))
plt.show()

在此处输入图像描述

To use the default scientific notation:

Seaborn will use scientific notation automatically if your number is large (note the value of y ):

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
a = pd.DataFrame({"x": np.random.randn(500),
                   "y": np.random.randn(500)*1000000000})

import seaborn as sns
sns.set_theme(style="ticks")

g = sns.catplot( data=a)
# plt.ticklabel_format(style='plain', axis='y')
plt.show()

To avoid using scientific notation

Just use

plt.ticklabel_format(style='plain', axis='y')

Complete code:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
a = pd.DataFrame({"x": np.random.randn(500),
                   "y": np.random.randn(500)*200})

import seaborn as sns
sns.set_theme(style="ticks")

g = sns.catplot( data=a)
plt.ticklabel_format(style='plain', axis='y')
plt.show()

在此处输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM