如何在Python 3中向matplotlib 2.0`ax`对象添加黑色边框?
最近我一直在如何在Python 3中向matplotlib 2.0`ax`对象添加黑色边框?,python,matplotlib,plot,border,aesthetics,Python,Matplotlib,Plot,Border,Aesthetics,最近我一直在matplotlib中使用样式表。我非常喜欢seaborn white的外观,我希望能够将边框添加到其他样式中,如ggplot或seaborn whitegrid 如何在fig,ax=plt.subplot()中的ax对象周围添加黑色边框? import pandas as pd import numpy as np from collections import * Se_data = pd.Series(Counter(np.random.randint(0,10,100))
matplotlib
中使用样式表。我非常喜欢seaborn white
的外观,我希望能够将边框添加到其他样式中,如ggplot
或seaborn whitegrid
如何在fig,ax=plt.subplot()
中的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\u 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')
有关更多详细信息,请参见: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")
看一看。您要查找的是以下两行:
ax.patch.set_edgecolor('black')
ax.patch.set_linewidth('1')
我想我遗漏了什么。如果你喜欢“seaborn white”样式,用它来代替任何其他样式有什么不对?我想知道边界是如何分配的,这样我就可以把它添加到任何绘图中。我想下面的答案就是答案。边框总是在那里,只是它们的颜色更暗,在
seaborn white
样式中线宽更粗。脚本更新版本中的错误是不言自明的,“边距必须在0到1之间的[a]范围内”。所以不要使用5.ax.margins(5)应该是ax.margins(0.5)背景仍然是黑色的:/当我将其更改为白色时,边框就会消失。我没有将它们设置为任何特定的值;1.0为白色;0.0是黑色的,其他的都是介于两者之间的灰度。我把它留给你去摆弄,以满足你的需要;为您提供自定义工具。我必须在“无边框”图表中添加plt.rcParams[“axes.grid”]=False
,因为我的图表显然选择了网格背景作为默认值。