Pandas 围绕网格轴绘制框

Pandas 围绕网格轴绘制框,pandas,matplotlib,plot,seaborn,Pandas,Matplotlib,Plot,Seaborn,我无法理解如何在每个镶嵌面网格元素周围绘制黑色边界框 import pandas as pd import seaborn as sb from matplotlib import pyplot as plt df = sb.load_dataset('tips') g = sb.FacetGrid(df, col = "time") g.map(plt.hist, "tip") plt.show() 给出: 我想要这样的东西: 我试过使用 sb.reset_orig() #after t

我无法理解如何在每个镶嵌面网格元素周围绘制黑色边界框

import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset('tips')
g = sb.FacetGrid(df, col = "time")
g.map(plt.hist, "tip")
plt.show()
给出:

我想要这样的东西:

我试过使用

sb.reset_orig() #after the seaborn import, to reset to matplotlib original rc
以及轴本身的各种选项:

axes=g.axes.flatten()
for ax in axes:
    ax. # I can't figure out the right option.

这可能吗?

我不确定为什么默认情况下脊椎不可见,但我能够根据答案创建此解决方案。另外,我使用您的代码只得到两个子图,而不是问题中的4个子图。也许这是一个
seaborn
问题。我通过在4个尖刺上循环,使您的代码变得简洁

import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
sb.set()

df = sb.load_dataset('tips')
g = sb.FacetGrid(df, col = "time")
g.map(plt.hist, "tip")

for ax in g.axes.flatten(): # Loop directly on the flattened axes 
    for _, spine in ax.spines.items():
        spine.set_visible(True) # You have to first turn them on
        spine.set_color('black')
        spine.set_linewidth(4)

编辑(基于以下评论)

在上面的回答中,由于您只想从
ax.spines
字典中更改spines(值)的属性,因此也可以直接使用

for spine in ax.spines.values():

在已经提供的完整答案的基础上,还有一个快捷选项,在代码行中使用skeene=False来启动facet网格。也就是说,您需要将seaborn绘图的样式更改为ticks

图表如下:


例如:
对于轴中的ax:ax.spines['bottom'].set_color('black')ax.spines['top'].set_color('black')ax.spines['right'].set_color('black')ax.spines['left'].set_color('black')
仅将X轴和Y轴设置为黑色,而不是右侧和顶部。嗯……哇@巴辛加,太棒了-巴辛加真的。我以前没有在ax.spines.items()中看到过u的
,spine:
-您能进一步解释一下吗,或者提供一个链接,让我可以了解更多信息?谢谢这里
ax.spines
是一本字典。字典有键和值。要从字典中访问键和值对,可以使用
.items()
返回(键,值)对。现在这些值是spines。由于您只想修改脊椎(值)的属性(属性),因此可以在ax.spines.items()中为脊椎(值)使用
。由于不需要对该键执行任何操作,因此可以使用下划线
\uu
,因为此处的键是多余的。在答案中检查我的编辑
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
sb.set(style='ticks')

df = sb.load_dataset('tips')
g = sb.FacetGrid(df, col = "time", despine=False)
g.map(plt.hist, "tip");