我正在努力寻找更好看的日志日志,我几乎得到了我想要的东西,除了一个小问题。
我的例子抛出标准设置的原因是x值被限制在不到十年内,我想使用小数,而不是科学记数法。
请允许我举例说明:
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib as mpl
import numpy as np
x = np.array([0.6,0.83,1.1,1.8,2])
y = np.array([1e-5,1e-4,1e-3,1e-2,0.1])
fig1,ax = plt.subplots()
ax.plot(x,y)
ax.set_xscale('log')
ax.set_yscale('log')
产生:
x轴存在两个问题:
使用科学记数法,在这种情况下会适得其反
可怕的"偏移"在右下角
经过多次阅读,我添加了三行代码:
ax.xaxis.set_major_formatter(mpl.ticker.ScalarFormatter())
ax.xaxis.set_minor_formatter(mpl.ticker.ScalarFormatter())
ax.ticklabel_format(style='plain',axis='x',useOffset=False)
这会产生:
我对此的理解是有5个小刻度和1个主要刻度。它好多了,但仍然不完美:
答案 0 :(得分:2)
原因是,一些标签看起来很粗体,它们是主要和次要标签的一部分。如果两个文本完全重叠,由于抗锯齿,它们看起来更大胆
您可以决定仅使用次要的刻度标签,并使用NullLocator
设置主要标签。
由于您希望获得的ticklabels的位置非常具体,因此没有自动定位器能够提供开箱即用的功能。对于这种特殊情况,最简单的方法是使用FixedLocator
并指定您希望作为列表的标签。
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
x = np.array([0.6,0.83,1.1,1.8,2])
y = np.array([1e-5,1e-4,1e-3,1e-2,0.1])
fig1,ax = plt.subplots(dpi=72, figsize=(6,4))
ax.plot(x,y)
ax.set_xscale('log')
ax.set_yscale('log')
locs = np.append( np.arange(0.1,1,0.1),np.arange(1,10,0.2))
ax.xaxis.set_minor_locator(ticker.FixedLocator(locs))
ax.xaxis.set_major_locator(ticker.NullLocator())
ax.xaxis.set_minor_formatter(ticker.ScalarFormatter())
plt.show()
对于更通用的标记,当然可以对定位符进行子类化,但是我们需要知道用于确定ticklabels的逻辑。 (由于我没有看到问题中所需滴答的明确定义的逻辑,我觉得现在提供这样的解决方案将是浪费精力。)