2017-05-16 8 views
0

xtickメジャーラベルの位置を取得するにはどうすればよいですか? label.get_position()から取得している値は意味をなさない。上記のコードからPython matplotlib、xtickラベルの位置を取得

import numpy as np 
import matplotlib.pyplot as plt 

def f(t): 
    return np.exp(-t) * np.cos(2*np.pi*t) 

t1 = np.arange(0.0, 5.0, 0.1) 
t2 = np.arange(0.0, 5.0, 0.02) 

# fig, ax = plt.figure(1) 
fig, ax = plt.subplots() 
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') 

# plt.show() 
print(fig) 
print(ax.get_position()) 

# ------------------------------------------------ 
# positions of the tick labels, incorrect (0,0) returned 
# ------------------------------------------------ 
print([text.get_position() for text in ax.get_xticklabels()]) 
# correct tick label values 
print(ax.get_xticks()) 

出力は次のとおりです。

Figure(640x480) 
Bbox('array([[ 0.125, 0.1 ],\n  [ 0.9 , 0.9 ]])') 
[(0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0)] <-- incorrect positions 
[ 0. 1. 2. 3. 4. 5.] 

にはどうすればXTICK主要なラベルの位置を得るのですか? label.get_position()から取得している値は意味をなさない。私が知らない変容はありますか? 最終的に、(x、y)イメージピクセル単位のテキストボックスの位置が必要です。

+0

はねえ、私はこの[解答](http://stackoverflow.com/a/42171199/5103802が)あなたの問題を解決することができると思います! –

+0

このコードを実行すると、次のようになります。 ラベル0、データ:Bbox( 'array [[80.、43.55555556]、\ n [80.、43.55555556]])が各行に対して繰り返されます。 – Justin

+0

私はいくつかの 'labels = ['Frogs'、 'Hogs'、 'Bogs'、 'Slogs'] * 12 'と同じコードを試して、x0、y0、x1、y1を出力していました。 –

答えて

2

ピクセル座標が必要な場合は、図座標を必要とし、それらを変換します。

あなたは変換の詳細情報が必要な場合:このmatplotlibの変換チュートリアルをチェック:ref

EDIT:完全を期すために、私はあなたの姿の大きさに影響を与えるであろう、解像度を指定するオプションを追加

import matplotlib as mpl 
import numpy as np 
import matplotlib.pyplot as plt 

def f(t): 
    return np.exp(-t) * np.cos(2*np.pi*t) 

t1 = np.arange(0.0, 5.0, 0.1) 
t2 = np.arange(0.0, 5.0, 0.02) 

# set the dpi you want in your final figure 
dpi = 300 
mpl.rcParams['figure.dpi'] = dpi 

fig, ax = plt.subplots() 
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') 

# saving the figure: don't forget the dpi option! 
fig.savefig('./out.png', format='png', dpi=dpi) 

xtickslocs = ax.get_xticks() 
ymin, _ = ax.get_ylim() 
print('xticks pixel coordinates') 
print(ax.transData.transform([(xtick, ymin) for xtick in xtickslocs])) 
print('label bounding boxes') 
print([l.get_window_extent() for l in ax.get_xticklabels()]) 

xticks pixel coordinates 
array([[ 60. , 40. ], 
     [ 134.4, 40. ], 
     [ 208.8, 40. ], 
     [ 283.2, 40. ], 
     [ 357.6, 40. ], 
     [ 432. , 40. ]]) 
label bounding boxes 
[Bbox([[56.4375, 25.5555555556], [63.5625, 35.5555555556]]), 
Bbox([[130.8375, 25.5555555556], [137.9625, 35.5555555556]]), 
Bbox([[205.2375, 25.5555555556], [212.3625, 35.5555555556]]), 
Bbox([[279.6375, 25.5555555556], [286.7625, 35.5555555556]]), 
Bbox([[354.0375, 25.5555555556], [361.1625, 35.5555555556]]), 
Bbox([[428.4375, 25.5555555556], [435.5625, 35.5555555556]])] 
+0

これは、画像のx、y座標ではなく、グラフのプロットデータ空間に変換するだけです。ここにあるドキュメントを参照してください:https://matplotlib.org/users/transforms_tutorial.html – Justin

+0

有効なポイント@ジャスティン、それに応じて調整します – Daan

+0

上記のコメントの答えと以下のものは、試してみる前にアクセントをつけるax.get_xticklabels 1)plt.gcf()。canvas.draw() 2)fig.savefig( './out.png'、format = 'png'、dpi = dpi) – Justin

0

import numpy as np 
import matplotlib.pyplot as plt 

def f(t): 
    return np.exp(-t) * np.cos(2*np.pi*t) 

t1 = np.arange(0.0, 5.0, 0.1) 
t2 = np.arange(0.0, 5.0, 0.02) 

# fig, ax = plt.figure(1) 
fig, ax = plt.subplots() 
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') 

print(fig) 
print(ax.get_position()) 


plt.gcf().canvas.draw() 
ticks = [tick for tick in plt.gca().get_xticklabels()] 

for i, t in enumerate(ticks): 
    print("Label ", i, ", data: ", t.get_text(), " ; ", t.get_window_extent()) 

print(ax.get_xticks()) 
plt.show() 
(テキストボックスの位置は(x、y))にする必要があるため、この answerをコメント/コードで要求に合わせて調整します。印刷し

>>> 
RESTART: C:/Users/Vinicius/AppData/Local/Programs/Python/Python35-32/stack21.py 
Figure(640x480) 
Bbox(x0=0.125, y0=0.10999999999999999, x1=0.9, y1=0.88) 
Label 0 , data: ; Bbox(x0=102.54545454545455, y0=43.077777777777776, x1=102.54545454545455, y1=43.077777777777776) 
Label 1 , data: 0 ; Bbox(x0=98.17045454545455, y0=29.077777777777776, x1=106.92045454545455, y1=43.077777777777776) 
Label 2 , data: 1 ; Bbox(x0=188.65194870390656, y0=29.077777777777776, x1=197.52694870390656, y1=43.077777777777776) 
Label 3 , data: 2 ; Bbox(x0=279.25844286235855, y0=29.077777777777776, x1=288.00844286235855, y1=43.077777777777776) 
Label 4 , data: 3 ; Bbox(x0=369.80243702081054, y0=29.077777777777776, x1=378.55243702081054, y1=43.077777777777776) 
Label 5 , data: 4 ; Bbox(x0=460.34643117926254, y0=29.077777777777776, x1=469.09643117926254, y1=43.077777777777776) 
Label 6 , data: 5 ; Bbox(x0=550.8904253377145, y0=29.077777777777776, x1=559.6404253377145, y1=43.077777777777776) 
Label 7 , data: ; Bbox(x0=102.54545454545455, y0=43.077777777777776, x1=102.54545454545455, y1=43.077777777777776) 
[-1. 0. 1. 2. 3. 4. 5. 6.] 
関連する問題