如何将网格线添加到 seaborn 中的 catplot?
How can I add grid lines to a catplot in seaborn?
如何向 seaborn
catplot 添加网格线(垂直和水平)?我发现可以在箱线图上执行此操作,但我有多个方面,因此需要一个 catplot。与其他答案相反,catplot 不允许 ax
参数。
此代码借用自here。
import seaborn as sns
sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
g = sns.catplot(x="time", y="pulse", hue="kind", data=exercise)
plt.show()
有什么想法吗?谢谢!
编辑:提供的答案有效,但对于多面图,只有最后一个图继承了网格。
import seaborn as sns
sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
g = sns.catplot(x="time", y="pulse", hue="kind", col="diet", data=exercise)
plt.grid()
plt.show()
有人可以向我解释为什么以及如何解决它吗?
您可以通过两种方式在 seaborn 地块上设置网格:
1。 plt.grid()
方法:
您需要在matplotlib.pyplot
中使用grid
方法。你可以这样做:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
g = sns.catplot(x="time", y="pulse", hue="kind", data=exercise)
plt.grid() #just add this
plt.show()
生成此图表的结果:
2。 sns.set_style()
方法
您还可以使用 sns.set_style
,这将在任何给定 FacetGrid
中的所有子图中启用网格。你可以这样做:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
sns.set_style("darkgrid")
g = sns.catplot(x="time", y="pulse", hue="kind", col="diet", data=exercise)
plt.show()
哪个returns这张图:
遇到这个问题是为了寻找一种在不使用“whitegrid”样式的情况下将网格添加到 FacetGrid 图中的方法。在尝试了许多解决方案之后,我发现在多面图中必须将 {'axes.grid' : True} 添加到 set_style 函数中:
import seaborn as sns
sns.set_style("ticks",{'axes.grid' : True})
g = sns.FacetGrid(df, col="column_variable",col_wrap=4, height=2.3)
如何向 seaborn
catplot 添加网格线(垂直和水平)?我发现可以在箱线图上执行此操作,但我有多个方面,因此需要一个 catplot。与其他答案相反,catplot 不允许 ax
参数。
此代码借用自here。
import seaborn as sns
sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
g = sns.catplot(x="time", y="pulse", hue="kind", data=exercise)
plt.show()
有什么想法吗?谢谢!
编辑:提供的答案有效,但对于多面图,只有最后一个图继承了网格。
import seaborn as sns
sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
g = sns.catplot(x="time", y="pulse", hue="kind", col="diet", data=exercise)
plt.grid()
plt.show()
有人可以向我解释为什么以及如何解决它吗?
您可以通过两种方式在 seaborn 地块上设置网格:
1。 plt.grid()
方法:
您需要在matplotlib.pyplot
中使用grid
方法。你可以这样做:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
g = sns.catplot(x="time", y="pulse", hue="kind", data=exercise)
plt.grid() #just add this
plt.show()
生成此图表的结果:
2。 sns.set_style()
方法
您还可以使用 sns.set_style
,这将在任何给定 FacetGrid
中的所有子图中启用网格。你可以这样做:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
sns.set_style("darkgrid")
g = sns.catplot(x="time", y="pulse", hue="kind", col="diet", data=exercise)
plt.show()
哪个returns这张图:
遇到这个问题是为了寻找一种在不使用“whitegrid”样式的情况下将网格添加到 FacetGrid 图中的方法。在尝试了许多解决方案之后,我发现在多面图中必须将 {'axes.grid' : True} 添加到 set_style 函数中:
import seaborn as sns
sns.set_style("ticks",{'axes.grid' : True})
g = sns.FacetGrid(df, col="column_variable",col_wrap=4, height=2.3)