2015-10-29 31 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) 

あなたは、個々の目盛りにラベルを追加したい場合は、あなたをtomの答えのように、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軸を乱雑になりますあなたはそれらをある角度で表示する必要があるかもしれません。

+0

チップをありがとう。プロットが消えてスケールファクタが残っていたので、以前は簡単に試してみましたが、うまくいかないと思っていました。私は昨日Pythonを使い始めました。それ以来、それは多くの文法ミスの1つと考えていました。しかし、あなたがそれを持ってきたので、私はもう一度チェックして、最初に間違っていることに気付きました。 'pl.x.xlim(right = max(data_a) 'のように' put.xlim'に再スケーリングを追加することを忘れてしまったからです。 [:、0])/ 1e14)。 – Casimir

+0

また、すべての目盛りのラベルに因子が表示される方法も知っていますか?それは、軸ラベルの大きさの順序が変化した場合に、周囲をひっくり返すことをはるかに少なくすることを意味します。 – Casimir

+0

@Casimir:うん、あなたは 'formatter'を設定する必要があります。私の答えを参照してください – tom

2

ウィルVousdenから良い答えに加えて、あなたはあなたとあなたのティックで書いたものを設定することができます

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

最初range(6)は、場所で、2番目はラベルです。

3

tickerモジュールを使用してダンプフォーマッタを変更することもできます。また、これを解決する方法については良いアイデアのたくさんの答えをhere参照

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

例はFormatStrFormatterを使用することです。

+0

それは素晴らしいですが、 'x'ラベルを' y'ラベルのように、つまり '4e + 14'ではなく' 4x10^14'として印刷する方法がありますか? – Casimir

+0

うん、私はあなたが@ WillVousdenの答えがすでにそれをしているのを見たと思う – tom

関連する問題