【问题标题】:Plain notation in seaborn plotsseaborn图中的简单符号
【发布时间】:2015-07-16 22:05:13
【问题描述】:

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

    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()

【问题讨论】:

  • 我无法重现此行为。您能否提供一个导致您看到的问题的完整可重现示例?
  • 我用一些代码编辑了帖子,我无法附上图片,但你可以find it here

标签: python matplotlib seaborn


【解决方案1】:

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

不要使用%.0f,它会删除小数点后的所有数字(留下0而不是0.1),而是使用%g,它将舍入到1位有效数字。我已将您的示例扩展为使用子图,以便可以设置格式:

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 你说得对。我已经相应地更新了我的答案,我认为这就是你要找的。​​span>
  • @Louise 没问题!如果您认为这回答了问题,请接受它(绿色复选标记):)
猜你喜欢
  • 2015-09-13
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 2020-09-03
  • 2011-10-11
  • 1970-01-01
  • 2018-05-15
  • 2012-09-02
相关资源
最近更新 更多