在对数刻度图上不打印x轴数字

时间:2012-09-12 11:02:25

标签: python matplotlib

打开“log”刻度时,x轴上的数字消失。

import numpy as np
import matplotlib.pyplot as plt

plt.plot( range(1,10) , range(1,10) ,  color='#aaaaff')
plt.xscale('log')
plt.xticks(range(1,10) )
plt.show()

如果我注释掉xscale,则会打印x轴上的数字。有人可以阐明为什么这在log比例轴上不起作用以及如何实现这一点?

编辑:

简化代码,以便无需数据文件即可使用。同样的事情:没有数字!!

1 个答案:

答案 0 :(得分:2)

默认情况下,日志轴的刻度位置位于基数10.因此,在上述情况下,唯一可能的xtick位于1。

如果您将xticks更改为

plt.xticks([1,10])

您可以在110找到答案。

你也可以玩ticker,这可以让你做很多事情。如,

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import LogLocator

fig = plt.figure(1, [5,4])
ax = fig.add_subplot(111)

ax.plot( range(1,100) , range(1,100) ,  color='#aaaaff')
ax.set_xscale('log')
# This is default, so play with it
ax.xaxis.set_major_locator(LogLocator(base = 10.0))
plt.show()

enter image description here

如果您使用:

ax.xaxis.set_major_locator(LogLocator(base = 100.0))

然后你得到

enter image description here