2016-09-08 79 views
2

我想在matplotlib圖中的某些文本上添加一個邊框,我可以使用patheffects.withStroke來做這些邊框。但是,對於某些字母和數字,符號的右上方會有一個小小的空白。圍繞文本邊框的空白

有沒有辦法沒有這個差距?

最低工作例如:

import matplotlib.pyplot as plt 
import matplotlib.patheffects as patheffects 

fig, ax = plt.subplots() 
ax.text(
    0.1, 0.5, "test: S6", 
    color='white', 
    fontsize=90, 
    path_effects=[patheffects.withStroke(linewidth=13, foreground='black')]) 
fig.savefig("text_stroke.png") 

這給出了圖像,該圖像示出了在S和6個符號的間隙。 enter image description here

我正在使用matplotlib 1.5.1。

+0

我認爲這是有意的。字母的輪廓被繪製成類似於形狀的刷子,從而以不同角度到達終點,因此錯誤連接。 – Ian

回答

2

該文檔沒有提及它(或者我沒有找到它),但是在代碼中搜索,我們可以看到patheffects.withStroke方法接受了很多關鍵字參數。

您可以通過交互式會話執行該有那些關鍵字參數列表:

>>> from matplotlib.backend_bases import GraphicsContextBase as gcb 
>>> print([attr[4:] for attr in dir(gcb) if attr.startswith("set_")]) 
['alpha', 'antialiased', 'capstyle', 'clip_path', 'clip_rectangle', 'dashes', 'foreground', 'gid', 'graylevel', 'hatch', 'joinstyle', 'linestyle', 'linewidth', 'sketch_params', 'snap', 'url'] 

你正在尋找的參數是capstyle它接受3個可能的值:

  • 「對接」
  • 「round」
  • 「預測」

在你的情況下,「圓」值似乎解決了這個問題。 考慮下面的代碼...

import matplotlib.pyplot as plt 
import matplotlib.patheffects as patheffects 

fig, ax = plt.subplots() 
ax.text(
    0.1, 0.5, "test: S6", 
    color='white', 
    fontsize=90, 
    path_effects=[patheffects.withStroke(linewidth=13, foreground='black', capstyle="round")]) 
fig.savefig("text_stroke.png") 

...它會產生這樣的:

enter image description here


接受的關鍵字參數,實際上所有的set_*(減去 「SET_」 prefixe )GraphicsContextBase類的方法。您可以在課程文檔中找到有關可接受值的詳細信息。

+0

這很好,謝謝!我猜這應該更好地記錄在某處,可能在文檔字符串中。我將在GitHub matplotlib存儲庫上發佈一個問題。 – Magnus