删除轴刻度

时间:2015-10-29 14:09:36

标签: python matplotlib plot

我花了一些时间徒劳地寻找我的问题的答案,所以我认为一个新的问题是有序的。考虑这个情节:

![enter image description here

轴标签使用科学记数法。在y轴上,一切都很好。但是,我已经尝试并且未能摆脱Python在右下角添加的缩放因子。我想要完全删除这个因素,只需用轴标题中的单位表示它,或者将它乘以每个刻度标签。一切看起来都比这丑陋的1e14好。

以下是代码:

import numpy as np data_a = np.loadtxt('exercise_2a.txt')

import matplotlib as mpl 
font = {'family' : 'serif',
        'size'   : 12} 
mpl.rc('font', **font)

import matplotlib.pyplot as plt 
fig = plt.figure() 
subplot = fig.add_subplot(1,1,1)

subplot.plot(data_a[:,0], data_a[:,1], label='$T(t)$', linewidth=2)

subplot.set_yscale('log')               
subplot.set_xlabel("$t[10^{14}s]$",fontsize=14) 
subplot.set_ylabel("$T\,[K]$",fontsize=14) 
plt.xlim(right=max(data_a [:,0])) 
plt.legend(loc='upper right')

plt.savefig('T(t).pdf', bbox_inches='tight')

更新:将Will的scientificNotation实现合并到我的脚本中,情节现在看起来像

enter image description here

如果你问我,好多了。对于想要采用其中某些部分的人来说,这是完整的代码:

import numpy as np
data = np.loadtxt('file.txt')

import matplotlib as mpl
font = {'family' : 'serif',
        'size'   : 16}
mpl.rc('font', **font)

import matplotlib.pyplot as plt
fig = plt.figure()
subplot = fig.add_subplot(1,1,1)

subplot.plot(data[:,0], data[:,1], label='$T(t)$', linewidth=2)

subplot.set_yscale('log')
subplot.set_xlabel("$t[s]$",fontsize=20)
subplot.set_ylabel("$T\,[K]$",fontsize=20)
plt.xlim(right=max(data [:,0]))
plt.legend(loc='upper right')

def scientificNotation(value):
    if value == 0:
        return '0'
    else:
        e = np.log10(np.abs(value))
        m = np.sign(value) * 10 ** (e - int(e))
        return r'${:.0f} \cdot 10^{{{:d}}}$'.format(m, int(e))

formatter = mpl.ticker.FuncFormatter(lambda x, p: scientificNotation(x))
plt.gca().xaxis.set_major_formatter(formatter)


plt.savefig('T(t).pdf', bbox_inches='tight', transparent=True)

3 个答案:

答案 0 :(得分:5)

只需将x值除以1e14

subplot.plot(data_a[:,0] / 1e14, data_a[:,1], label='$T(t)$', linewidth=2)

如果你想为每个单独的标记添加标签,你必须提供一个custom formatter,就像汤姆的答案一样。

如果你希望它看起来像你的y轴上的刻度一样好,你可以提供一个函数来用LaTeX格式化它:

def scientificNotation(value):
    if value == 0:
        return '0'
    else:
        e = np.log10(np.abs(value))
        m = np.sign(value) * 10 ** (e - int(e))
        return r'${:.0f} \times 10^{{{:d}}}$'.format(m, int(e))

# x is the tick value; p is the position on the axes.
formatter = mpl.ticker.FuncFormatter(lambda x, p: scientificNotation(x))
plt.gca().xaxis.set_major_formatter(formatter)

当然,这会使你的x轴混乱很多,所以你最终可能需要以某个角度显示它们。例如。

答案 1 :(得分:3)

You can also change the tick formatter with the ticker module.

An example would be to use a FormatStrFormatter:

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

fig,ax = plt.subplots()
ax.semilogy(np.linspace(0,5e14,50),np.logspace(3,7,50),'b-')
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%.0e'))

enter image description here

Also see the answers here with lots of good ideas for ways to solve this.

答案 2 :(得分:2)

除了Will Vousden的好答案之外,您还可以设置您在刻度线中写的内容:

plt.xticks(range(6), range(6))

第一个range(6)是位置,第二个是标签。

相关问题