如何在 Python 3 中向 matplotlib 2.0 `ax` 对象添加黑色边框?
How to add black border to matplotlib 2.0 `ax` object In Python 3?
我最近一直在使用 matplotlib
中的样式表。我真的很喜欢 seaborn-white
的简洁外观,我希望能够将边框添加到其他样式,例如 ggplot
或 seaborn-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')
你可能想要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
seaborn-whitegrid和seaborn-white样式的区别是
seaborn-whitegrid
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")
看看this。您要查找的是这两行:
ax.patch.set_edgecolor('black')
ax.patch.set_linewidth('1')
我最近一直在使用 matplotlib
中的样式表。我真的很喜欢 seaborn-white
的简洁外观,我希望能够将边框添加到其他样式,例如 ggplot
或 seaborn-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')
你可能想要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
seaborn-whitegrid和seaborn-white样式的区别是
seaborn-whitegrid
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")
看看this。您要查找的是这两行:
ax.patch.set_edgecolor('black')
ax.patch.set_linewidth('1')