更好的刻度和刻度标签与对数刻度 [英] Better ticks and tick labels with log scale

查看:61
本文介绍了更好的刻度和刻度标签与对数刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得更好的对数对数图,除了一个小问题外,我几乎得到了我想要的东西.

I am trying to get better looking log-log plots and I almost got what I want except for a minor problem.

我的示例不符合标准设置的原因是x值限制在不到十年的时间内,我想使用十进制,而不是科学计数法.

The reason my example throws off the standard settings is that the x values are confined within less than one decade and I want to use decimal, not scientific notation.

让我用一个例子来说明:

Allow me to illustrate with an example:

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轴存在两个问题:

  1. 使用科学记数法,在这种情况下会适得其反

  1. The use of scientific notation, which in this case is counterproductive

右下角可怕的偏移"

大量阅读后,我添加了三行代码:

After much reading, I added three lines of code:

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个大刻度.更好,但仍不完美:

My understanding of this is that there are 5 minor ticks and 1 major one. It is much better, but still not perfect:

  1. 我想在 1 到 2 之间添加一些刻度
  2. 将标签的格式设置为1是错误的.应该是1.0"

所以我在格式化语句之前插入了以下行:

So I inserted the following line before the formatter statement:

ax.xaxis.set_major_locator(mpl.ticker.MultipleLocator(0.2))

我终于得到了想要的壁虱:

I finally get the ticks I want:

我现在有8个主要刻度线和2个次要刻度线.现在,除了0.6、0.8和2.0的勾号标签比其他标签更粗体外,这几乎是正确的.这是什么原因,我该如何纠正?

I now have 8 major and 2 minor ticks. Now, this almost looks right except for the fact that the tick labels at 0.6, 0.8 and 2.0 appear bolder than the others. What is the reason for this and how can I correct it?

推荐答案

一些标签出现粗体的原因是它们是主要和次要刻度标签的一部分.如果两个文本完全重叠,由于抗锯齿,它们会显得更粗.
您可能决定只使用次要的ticklabel,并使用 NullLocator 设置主要的ticklabel.

The reason, some of the labels appear bold is that they are part of the major and minor ticklabels. If two texts perfectly overlap, they appear bolder due to the antialiasing.
You may decide to only use minor ticklabels and set the major ones with a NullLocator.

由于您想要的刻度标签的位置确实是特定的,因此没有自动定位器可为您提供现成的标签.对于这种特殊情况,最简单的方法是使用 FixedLocator 并指定要作为列表使用的标签.

Since the locations of the ticklabels you wish to have is really specific there is no automatic locator that would provide them out of the box. For this special case it may be easiest to use a FixedLocator and specify the labels you wish to have as a list.

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

对于更通用的标签,当然可以将定位器子类化,但我们需要知道用于确定刻度标签的逻辑.(由于我没有看到针对问题的理想滴答声的明确定义的逻辑,所以我认为现在提供这种解决方案会浪费很多精力.)

For a more generic labeling, one could of course subclass a locator, but we would then need to know the logic to use to determine the ticklabels. (As I do not see a well defined logic for the desired ticks from the question, I feel it would be wasted effort to provide such a solution for now.)

这篇关于更好的刻度和刻度标签与对数刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆