2017-12-06 13 views
0

スペースを埋め込んだ後、同じ視覚的な長さになるようにmatplotlibのテキストを苦労させています。それはいつも私にとってはうまくいかない。ここでmatplotlibスペースを埋めた後に同じ長さのテキストを視覚的に表示する方法

は、私は、

from matplotlib import pyplot as plt 

fig = plt.figure() 
plt.text(0,1,'how are you', bbox=dict(facecolor='red', alpha=0.5)) 
plt.text(0,0,'how'.ljust(len('how are you'), bbox=dict(facecolor='red', alpha=0.5)) 
plt.show() 

やアイデアを試してみましたが、何ですか?

ありがとうございます!

答えて

1

ljustは、指定した数のスペースをテキストに追加します。しかし、通常のフォントでは、スペースは他のほとんどの文字よりも小さくなります。比較:

enter image description here

下のテキストは、(スペースを含む)の各文字が同じ空間を持つ、いわゆる固定幅フォントで書かれている - それ故に名前。

あなたは意図した方法であなたのプログラムを動作させるために、このようなフォントを使用する必要があります:

from matplotlib import pyplot as plt 
plt.rcParams['font.family'] = 'monospace' 

fig = plt.figure() 
plt.text(0,1,'how are you', bbox=dict(facecolor='red', alpha=0.5)) 
plt.text(0,0,'how'.ljust(len('how are you')), bbox=dict(facecolor='red', alpha=0.5)) 
plt.show() 

enter image description here

これができない場合は、あなたがテキストの後ろに四角形を配置しますあなたはあなたの欲望に幅を設定します、この質問を参照してくださいMatplotlib: how to specify width of x-label bounding box

関連する問題