2010-03-31 8 views
14

所以,似乎一個人不能做以下(它會引發一個錯誤,因爲axes沒有set_linewidth方法):設置`axes.linewidth`不改變`rcParams`全球字典

axes_style = {'linewidth':5} 
axes_rect = [0.1, 0.1, 0.9, 0.9] 

axes(axes_rect, **axes_style) 

和必須使用下面的老把戲,而不是:

rcParams['axes.linewidth'] = 5 # set the value globally 

... # some code 

rcdefaults() # restore [global] defaults 

有一個簡單/乾淨的方式(可能是一個可以設置x - 和y - 獨立軸參數等)?

P.S.如果不是,爲什麼?

回答

8

是的,有一個簡單而乾淨的方法來做到這一點。

呼叫「axhline」和「axvline」從軸實例似乎是在MPL文檔認可的技術。

在任何情況下,它都很簡單,並且可以對軸的外觀進行細化控制。例如,此代碼將創建一個繪圖併爲x軸的綠色着色,並將x軸的線寬從默認值「1」增加到「4」的值; y軸爲紅色,y軸的線寬從「1」增加至「8」。

from matplotlib import pyplot as PLT 
fig = PLT.figure() 
ax1 = fig.add_subplot(111) 

ax1.axhline(linewidth=4, color="g")  # inc. width of x-axis and color it green 
ax1.axvline(linewidth=4, color="r")  # inc. width of y-axis and color it red 

PLT.show() 

的axhline/axvline函數接受額外的論據應該讓你做你想做的美觀,尤其是任何〜matplotlib.lines.Line2D屬性是有效kwargs(例如,「阿爾法」任何非常,'linestyle',capstyle,joinstyle)。

+0

非常感謝,我給它幾個小時仔細看看。 – mlvljr 2010-04-01 06:49:29

+0

再次感謝,接受。 – mlvljr 2010-04-01 11:00:31

+1

投訴該網站!總是很高興見到俄羅斯的同行代碼。 – doug 2010-04-01 13:27:32

39

以上答案無效,正如評論中所述。我建議使用刺。

import matplotlib.pyplot as plt 

fig = plt.figure() 
ax = fig.add_subplot(111) 

# you can change each line separately, like: 
#ax.spines['right'].set_linewidth(0.5) 
# to change all, just write: 

for axis in ['top','bottom','left','right']: 
    ax.spines[axis].set_linewidth(0.5) 

plt.show() 
# see more about spines at: 
#http://matplotlib.org/api/spines_api.html 
#http://matplotlib.org/examples/pylab_examples/multiple_yaxis_with_spines.html 
+1

這是正確的答案! – 2016-01-11 11:15:21

6
plt.setp(ax.spines.values(), linewidth=5)