Python seaborn图中的素表示法

Python seaborn图中的素表示法,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我开始使用seaborn,我只使用pyplot.plot函数,每当我使用对数刻度时,轴刻度显示为10的幂。在使用seaborn之前,请使用line ax.xaxis.set_major_formatter(FormatStrFormatter('%.0f')) 正在解决问题,但现在seaborn没有解决。有没有办法在我的坐标轴上有简单的符号 下面是一个代码示例。我希望x轴的形式是:0.1,1,10,100。。而不是现在 import matplotlib.pyplot as plt i

我开始使用seaborn,我只使用pyplot.plot函数,每当我使用对数刻度时,轴刻度显示为10的幂。在使用seaborn之前,请使用line

    ax.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))
正在解决问题,但现在seaborn没有解决。有没有办法在我的坐标轴上有简单的符号

下面是一个代码示例。我希望x轴的形式是:0.1,1,10,100。。而不是现在

import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('ticks',  {'font.family': 'sans-serif'})
sns.set_context("paper", font_scale=1.3)
plt.rc('text', usetex=True)


x_lim=[0.1,100]
y_lim=[1.2001, 2.2001]
f = plt.figure()
plt.xscale('log')
plt.ylim(y_lim)
plt.xlim(x_lim)
plt.hlines(1.5, *x_lim, color='grey')
plt.show()

关键是格式化程序行的位置。它必须在ax.setxscale('log')之后

不要使用
%.0f
删除小数点后的所有数字(保留
0
而不是
0.1
),而是使用
%g
,它将四舍五入到一个有效数字。我扩展了您的示例,使用子图以便设置格式:

import matplotlib.pyplot as plt
import matplotlib.ticker as tck
import seaborn as sns

sns.set_style('ticks',  {'font.family': 'sans-serif'})
sns.set_context("paper", font_scale=1.3)
plt.rc('text', usetex=True)


x_lim=[0.1,100]
y_lim=[1.2001, 2.2001]

f = plt.figure()
ax = f.add_subplot(plt.subplot(1, 1, 1))
ax.set_xscale('log')
ax.set_ylim(y_lim)
ax.set_xlim(x_lim)
ax.hlines(1.5, *x_lim, color='grey')
ax.xaxis.set_major_formatter(tck.FormatStrFormatter('%g'))

plt.show()
这将生成图像:


我无法重现这种行为。你能提供一个完整的可复制的例子来引起你所看到的问题吗?我用一些代码编辑了这篇文章,我无法附加图片,但你可以。嗨,这实际上不起作用,因为你在开始时得到0,它应该是0.1,但如果你将它更改为FormatStrFormatter('.1f'),那么你得到0.1,但后来你也有了1.0、10.0和100.0…@Luise你说得很对。我已经相应地更新了我的答案,我想这就是你要找的。@Louise没问题!如果您认为这回答了问题,请接受它(绿色复选标记):)