我想彩色邊框添加一些次要情節與像素指定一個固定的寬度。我寫了下面的函數來爲軸後面的圖形添加一個Rectangle
補丁。如何添加一個固定寬度的邊框插曲
def add_subplot_border(ax, width=0, color=None):
fig = ax.get_figure()
# Convert bottom-left and top-right to display coordinates
x0, y0 = ax.transAxes.transform((0, 0))
x1, y1 = ax.transAxes.transform((1, 1))
# Adjust margins
x0 -= width
x1 += width
y0 -= width
y1 += width
# Convert back to Axes coordinates
x0, y0 = ax.transAxes.inverted().transform((x0, y0))
x1, y1 = ax.transAxes.inverted().transform((x1, y1))
rect = plt.Rectangle((x0, y0), x1-x0, y1-y0,
color=color,
transform=ax.transAxes,
zorder=-1)
fig.patches.append(rect)
這似乎是一個很好的起點,但如果這個數字被調整的邊界的相對厚度也發生了變化。如何指定一個變換來縮放和轉換補丁,使其顯示爲固定寬度邊框,而不管窗口縮放比例如何?或者,有沒有更好的方法來解決這個問題?
原始圖
縮放圖 - 邊境
如何着色軸本身,而不是墊層有色'Rect'?請參閱[本答案](https://stackoverflow.com/a/12059429/2454357)瞭解如何操作。另請參閱[本答案](https://stackoverflow.com/a/2557264/2454357)瞭解如何設置軸的線寬。 –
@ThomasKühn這可能是一個很好的解決了我的眼前問題,但我希望能延長想法顏色在整個插曲背景 – user3419537
見我的回答對固定寬度的矩形。如果要爲整個子區域背景着色,可以使用'ax.set_facecolor()'。例如,見[這個答案](https://stackoverflow.com/a/23645437/2454357) –