2014-07-14 28 views
0

Matplotlib的text()函數應用於軸沒有linewidth屬性。如何調整matplotlib中的文本?

下面的例子中,我的圖將成功添加破折號,但它們超過5個發掘者厚度,而情節本身使用優雅的lw=2

for i in range(len(var)): 
    (x2,y2) = DATA[i] 
    ax1.text(x2, y2 , str(y2), fontdict=None, withdash=True, 
      dashdirection=1, 
      dashlength=10, 
      dashpad=5, 
      rotation=0, 
      dashrotation=90, 
      dashpush=10 
      ) 

我在SO這裏發現了這一點的代碼來設置一個圖中所有線的線寬,但它沒有成功定位到破折號。

for ln in ax.lines: 
    ln.set_linewidth(20) 

關於如何只能定位短劃線元素的任何建議?

回答

1

我無法重現您的問題。

但是,我仍然可以知道一個修補程序。虛線性能都比較容易改變:

import matplotlib.pyplot as plt 

# create a plot with some text with a dash 
fig = plt.figure() 
ax = fig.add_subplot(111) 
t = ax.text(.5,.5, "dashtest", withdash=True, dashlength=50) 

# change some dash line properties 
dash = t.dashline 
dash.set_color('r') 
dash.set_dashes((5,5)) 
dash.set_linewidth(5) 

這給了你:

enter image description here

酒店t.dashline是一個普通的Line2D實例,因此可以應用於適用於Line2D實例的任何格式。

+0

Ty。我想我會做第二篇文章,詢問關於matplotlib如何在類中封裝對象的問題。如果我無法在網站上找出這個... – xtian