2015-10-29 302 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