2016-09-14 27 views
8

Bir arsaya metin alanı ekliyorum. Metin benim dikdörtgenin yüksek (y-ekseni) tam olarak 2 tane olmak isteyenMatplotlib font boyutu eksen birimleri cinsinden

import matplotlib.pyplot as plt 
r = plt.Rectangle((2,2), 10, 10, fill = False) 
plt.gca().add_patch(r) 
plt.text(7, 7, 'my rectangle', fontsize = 12, ha='center', va='center') 
plt.axis(xmin = 0, xmax = 14, ymin = 0, ymax = 14) 

: I fontsize parametresini kullanımı harf boyutunu belirlemek. Bunu yapmanın bir yolu var mı?

cevap

1

Bu tam 2 veri birimleri şüpheliyim ama oldukça yakın görünüyor:

import matplotlib.pyplot as plt 
r = plt.Rectangle((2,2), 10, 10, fill = False) 
plt.gca().add_patch(r) 
ymin, ymax = (0, 14) 
plt.axis(xmin = 0, xmax = 14, ymin=ymin, ymax=ymax) 

# Get dimensions of y-axis in pixels 
y1, y2 = plt.gca().get_window_extent().get_points()[:, 1] 

# Get unit scale 
yscale = (y2-y1)/(ymax-ymin) 

# We want 2 of these as fontsize 
fontsize = 2*yscale 
print fontsize, 'pixels' 

txt = plt.text(7, 7, u"\u25AF" + 'my rectangle', fontsize=fontsize, ha='center', va='center') 

plt.savefig('test.png') 

test.png

bu boyutlandırma zaman, bir geri arama eklemeniz gerekir çalışmaya almak için.

İlgili konular