[英]How to scale Seaborn's y-axis with a bar plot
我正在使用factorplot(kind="bar")
。
如何縮放 y 軸,例如使用對數刻度?
我嘗試修改繪圖軸,但這總是以某種方式弄亂 plot 條,因此請先嘗試您的解決方案以確保它確實有效。
調用factorplot
后可以使用Matplotlib命令。 例如:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")
titanic = sns.load_dataset("titanic")
g = sns.factorplot("class", "survived", "sex",
data=titanic, kind="bar",
size=6, palette="muted", legend=False)
g.fig.get_axes()[0].set_yscale('log')
plt.show()
如果您在使用以前的解決方案設置日志規模時遇到消失的問題,請嘗試將log=True
添加到seaborn函數調用中。 (我對其他答案的評論缺乏聲譽)。
使用sns.factorplot
:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")
titanic = sns.load_dataset("titanic")
g = sns.factorplot(x="class", y="survived", hue="sex", kind='bar',
data=titanic, palette="muted", log=True)
g.ax.set_ylim(0.05, 1)
使用sns.barplot
:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")
titanic = sns.load_dataset("titanic")
g = sns.barplot(x="class", y="survived", hue="sex",
data=titanic, palette="muted", log=True)
g.set_ylim(0.05, 1)
Seaborn 的catplot不再有log
參數。
對於那些尋找更新答案的人,這是我使用過的最快的修復方法:您必須通過訪問軸 object 來使用 matplotlib 的內置支持。
g = sns.catplot(data=df, <YOUR PARAMETERS>)
for ax in g.fig.axes:
ax.set_yscale('log')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.