删除轴刻度

Cas*_*mir 5 python plot matplotlib

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

![在此处输入图像说明

轴标签使用科学记数法.在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')
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

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

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)
Run Code Online (Sandbox Code Playgroud)

Wil*_*den 5

只需将x值除以1e14:

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

如果你想为每个单独的标记添加标签,你必须提供自定义格式化程序,就像汤姆的答案一样.

如果你希望它看起来像你的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)
Run Code Online (Sandbox Code Playgroud)

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