围绕Seaborn FacetGrid轴绘制框

时间:2019-02-22 21:31:00

标签: 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()

赠予:

enter image description here

我想要这样的东西:

enter image description here

我尝试使用

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.

这可能吗?

2 个答案:

答案 0 :(得分:1)

我不确定默认情况下为什么脊柱不是set_visible,但是我能够根据this的答案创建此解决方案。另外,我仅使用您的代码得到两个子图,而不是问题中的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)

enter image description here

编辑(基于以下评论)

在上述答案中,由于您只想更改ax.spines词典中的刺(值)的属性,因此您也可以直接使用

遍历值(刺)。
for spine in ax.spines.values():

答案 1 :(得分:1)

以已经提供的完整答案为基础,在代码行中还存在一个使用 despine = False 的快捷方式选项来启动构面网格。也就是说,您需要将海洋情节的样式更改为 ticks

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

图表如下:

相关问题