2017-04-06 132 views
1

最近我一直在使用matplotlib樣式表。我真的很喜歡seaborn-white看起來很乾淨,我希望能夠將邊框添加到其他樣式,如ggplotseaborn-whitegrid如何將黑色邊框添加到matplotlib 2.0`ax`對象在Python 3中?

如何在我的ax對象周圍添加黑色邊框fig, ax = plt.subplots()

import pandas as pd 
import numpy as np 
from collections import * 

Se_data = pd.Series(Counter(np.random.randint(0,10,100))) 
with plt.style.context("seaborn-whitegrid"): 
    fig, ax = plt.subplots() 
    Se_data.plot(kind="barh", ax=ax, title="No Border") 
with plt.style.context("seaborn-white"): 
    fig, ax = plt.subplots() 
    Se_data.plot(kind="barh", ax=ax, title="With Border") 

enter image description here

針對下面的答案:

Se_data = pd.Series(Counter(np.random.randint(0,10,100))) 
with plt.style.context("seaborn-whitegrid"): 
    fig, ax = plt.subplots() 
    Se_data.plot(kind="barh", ax=ax, title="No Border") 
    ax.spines['bottom'].set_color('0.5') 
    ax.spines['top'].set_color(None) 
    ax.spines['right'].set_color('0.5') 
    ax.spines['left'].set_color(None) 
    ax.patch.set_facecolor('0.1') 
    plt.grid(b=True, which='major', color='0.2', linestyle='-') 
    plt.grid(b=True, which='minor', color='0.2', linestyle='-') 
    ax.tick_params(axis='x', colors='0.7', which='both') 
    ax.tick_params(axis='y', colors='0.7', which='both') 
    ax.yaxis.label.set_color('0.9') 
    ax.xaxis.label.set_color('0.9') 
    ax.margins(5) 
    fig.patch.set_facecolor('0.15') 

enter image description here

+0

我覺得我失去了一些東西。如果你喜歡「seaborn-white」風格,那麼使用它代替任何其他風格有什麼問題? – ImportanceOfBeingErnest

+0

我想知道邊界是如何分配的,所以我可以將它添加到任何陰謀 –

+0

我想我的答案下面的答案。邊界總是在那裏,只是它們的顏色更暗,而且「seaborn-white」風格的線條更粗。 – ImportanceOfBeingErnest

回答

1

的seaborn-whitegrid和seaborn白色風格之間的差異是

seaborn-whitegrid

axes.grid: True 
axes.edgecolor: .8 
axes.linewidth: 1 

seaborn-white

012因此
axes.grid: False 
axes.edgecolor: .15 
axes.linewidth: 1.25 

下面將提供相同的情節:

import matplotlib.pyplot as plt 
import pandas as pd 
import numpy as np 
from collections import * 

Se_data = pd.Series(Counter(np.random.randint(0,10,100))) 
with plt.style.context("seaborn-whitegrid"): 
    plt.rcParams["axes.edgecolor"] = "0.15" 
    plt.rcParams["axes.linewidth"] = 1.25 
    fig, ax = plt.subplots() 
    Se_data.plot(kind="barh", ax=ax, title="No Border") 
with plt.style.context("seaborn-white"): 
    plt.rcParams["axes.grid"] = True 
    fig, ax = plt.subplots() 
    Se_data.plot(kind="barh", ax=ax, title="With Border") 

enter image description here

2

你可能想ax.splines.set_color()

這些會給你一個廣泛OPTIO的定製的解決方案NS:

ax.spines['bottom'].set_color('0.5') 
ax.spines['top'].set_color(None) 
ax.spines['right'].set_color('0.5') 
ax.spines['left'].set_color(None) 
ax.patch.set_facecolor('0.1') 
plt.grid(b=True, which='major', color='0.2', linestyle='-') 
plt.grid(b=True, which='minor', color='0.2', linestyle='-') 
ax.tick_params(axis='x', colors='0.7', which='both') 
ax.tick_params(axis='y', colors='0.7', which='both') 
ax.yaxis.label.set_color('0.9') 
ax.xaxis.label.set_color('0.9') 
ax.margins(0.5) 
fig.patch.set_facecolor('0.15') 

文檔:http://matplotlib.org/api/spines_api.html

+0

我無法得到這與我的例子 –

+0

ax.margins(5)應該是ax.margins(0.5) – litepresence

+0

背景仍然是黑色的:/和邊界一旦離開我將其更改爲白色。 –