我花了一些时间无果寻找我的问题的答案,所以我认为一个新的问题是为了。考虑这个情节:删除轴刻度
轴标签用科学记数法。在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')
更新:结合威尔的实施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)
感谢您的提示。之前我曾经简单地尝试过,并认为它不起作用,因为情节消失了,比例因子依然存在。我只是在昨天才开始使用Python,所以我认为这是从那时起许多语法错误之一。但是现在你也提到了它,我再次检查并意识到它第一次出错了,因为我忘记了也要将重调缩放到'put.xlim',如'plt.xlim(right = max(data_a [:,0])/ 1E14)'。 – Casimir
你是否也知道一种方法让每个刻度标签中都显示该因子?这意味着在轴标签的数量级发生变化的情况下,摆动得少得多。 – Casimir
@Cimimir:是的,你需要设置'formatter'。看到我的回答 – tom