[英]How can I add grid lines to a catplot in seaborn?
如何將網格線(垂直和水平)添加到seaborn
catplot? 我發現可以在箱線圖上做到這一點,但我有多個方面,因此需要一個貓圖。 與其他答案相反, catplot 不允許使用ax
參數。
這段代碼是從這里借來的。
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()
有任何想法嗎? 謝謝!
編輯:提供的答案有效,但對於多面圖,只有最后一個 plot 繼承網格。
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 圖上設置網格:
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()
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()
來到這個問題,尋找一種將網格添加到 FacetGrid plot 的方法,而不使用“whitegrid”樣式。 在嘗試了許多解決方案之后,我發現在多面圖中,必須將 {'axes.grid': True} 添加到 set_style function 中:
import seaborn as sns
sns.set_style("ticks",{'axes.grid' : True})
g = sns.FacetGrid(df, col="column_variable",col_wrap=4, height=2.3)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.