2015-10-29 189 views
5

我花了一些时间无果寻找我的问题的答案,所以我认为一个新的问题是为了。考虑这个情节:删除轴刻度

![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') 

更新:结合威尔的实施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) 

回答

5

仅仅通过1e14除以x值:如果你要在标签添加到每个单独剔

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

,你必须提供一个custom formatter,就像汤姆的回答一样。

如果你希望它看起来像像你一样对你y轴的刻度,你可以提供一个函数,用乳胶格式为:

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轴例如,你可能最终需要以某种角度显示它们。

+0

感谢您的提示。之前我曾经简单地尝试过,并认为它不起作用,因为情节消失了,比例因子依然存在。我只是在昨天才开始使用Python,所以我认为这是从那时起许多语法错误之一。但是现在你也提到了它,我再次检查并意识到它第一次出错了,因为我忘记了也要将重调缩放到'put.xlim',如'plt.xlim(right = max(data_a [:,0])/ 1E14)'。 – Casimir

+0

你是否也知道一种方法让每个刻度标签中都显示该因子?这意味着在轴标签的数量级发生变化的情况下,摆动得少得多。 – Casimir

+0

@Cimimir:是的,你需要设置'formatter'。看到我的回答 – tom

2

除了从威尔Vousden了很好的答案,你可以设置你在蜱写什么用:

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

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

3

您还可以使用ticker模块更改刻度格式器。

一个例子是使用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

还看到答案here有很多好点子的方式来解决这个问题。

+0

这很酷,但是有没有办法像'y'标签一样打印'x'标签,即'4 x 10^14'而不是'4e + 14'? – Casimir

+0

是的,我想你已经看到@ WillVousden的答案已经做到了 – tom