バーでプロットを作成しており、後でさらに処理するために、プロット上の絶対位置 (ピクセル単位) を見つけようとしています。これは、軸インスタンスに関するmatplotlibの変換情報から把握できるはずです。具体的にはax.transData
、データ座標 (ボックスの位置がわかっている場所) から座標を表示するために使用しています。ここにいくつかのコードがあります:
import matplotlib.pyplot as plt
x = range(10)
y = range(1, 11)
fig = plt.figure()
ax = fig.add_subplot(111)
bars = ax.bar(x, y, width=.5, label="foo")
ax.monkey_rectangles = bars
ax.legend()
def get_useful_info(fig):
for ax in fig.get_axes():
for rect in ax.monkey_rectangles:
corners = rect.get_bbox().corners()[::3]
pos = ax.transData.transform(corners)
left = pos[0,0]
width = pos[1,0] - pos[0,0]
bottom = pos[0,1]
height = pos[1,1] - pos[0,1]
yield left, width, bottom, height
fig.savefig('foo.png')
for l, w, b, h in get_useful_info(fig):
print l, w, b, h
これにより、次のように出力されます。
80.0 24.8 48.0 38.4
129.6 24.8 48.0 76.8
179.2 24.8 48.0 115.2
228.8 24.8 48.0 153.6
278.4 24.8 48.0 192.0
328.0 24.8 48.0 230.4
377.6 24.8 48.0 268.8
427.2 24.8 48.0 307.2
476.8 24.8 48.0 345.6
526.4 24.8 48.0 384.0
したがって、matplotlib は、私のボックスの幅が 24.8 ユニット (ピクセルと仮定) であると考えています。ボックスの幅を実際に測定する場合を除いて、それで問題ありません。幅は 32 ピクセル程度になります。ここでの矛盾は何ですか?