如何在Python 3中向matplotlib 2.0的`ax`对象添加黑色边框?

21

最近我一直在使用 matplotlib 中的样式表。我非常喜欢 seaborn-white 的简洁外观,希望能够将其边框添加到其他样式中,例如 ggplotseaborn-whitegrid

如何在来自 fig,ax = plt.subplots()ax 对象周围添加黑色边框?

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")

输入图片说明

回应下方的答案:

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')

在这里输入图片描述


我觉得我在这里漏掉了什么。如果你喜欢“seaborn-white”风格,那么使用它而不是其他任何风格有什么问题吗? - ImportanceOfBeingErnest
我想知道边框是如何分配的,这样我就可以将其添加到任何绘图中。 - O.rka
我认为我下面的答案回答了这个问题。边框始终存在,只是在“seaborn-white”风格中它们的颜色更深,线条宽度更粗。 - ImportanceOfBeingErnest
你在更新版本的脚本中出现的错误非常明显,“边距必须是0到1之间的数字”。所以不要使用5。 - ImportanceOfBeingErnest
3个回答

24
看一下this。你要找的是这两行:
ax.patch.set_edgecolor('black')  

ax.patch.set_linewidth(1)  

1
与其使用 ax.patch.set_linewidth('1'),我成功的方法是使用 ax.patch.set_linewidth(1)。它不接受字符串作为线宽值。 - Rafael Beirigo

9

您可能需要使用ax.spines.set_color()

以下是一些自定义解决方案的广泛选项:

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

ax.margins(5) 应该改为 ax.margins(0.5)。 - litepresence
背景仍然是黑色:/,一旦我把它改成白色,边框就消失了。 - O.rka
我没有将它们设置为任何特定值;1.0 是白色;0.0 是黑色,其他的都是介于灰度之间。我把这留给了你来调整,以满足你的需求;给你提供自定义的工具。 - litepresence
对于 ax.spines.set_color(),我需要单独设置每个脊柱(就像您的代码块中一样),例如 [a.set_color("w") for a in ax.spines.values()] - yodavid

6
"seaborn-whitegrid"和"seaborn-white"风格之间的区别在于:
"seaborn-whitegrid":显示网格线,可用于更好地观察数据分布。
"seaborn-white":没有网格线,适用于简单的数据可视化。
axes.grid: True
axes.edgecolor: .8
axes.linewidth: 1

seaborn-white

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


我不得不在“无边框”图表中添加 plt.rcParams["axes.grid"] = False,因为我的图表显然选择了网格背景作为默认设置。 - Coding Tumbleweed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接