![](/img/trans.png)
[英]Python: seaborn pointplot and boxplot in one plot but shifted on the x-axis
[英]Is it possible to add x-axis labels to *every* plot in the Seaborn Factor Plot (Python)?
我正在使用 Seaborn 制作因子圖。
總共,我有 4 個“子圖”(並使用col_wrap =2
,所以我有 2 行,每行包含 2 個子圖)。 只有網格最底部的 2 個子圖具有 x 軸標簽(我認為這是默認值)。
是否可以配置因子圖,使 4 個圖都具有 x 軸標簽? (我在文檔或 StackOverflow 上找不到這個選項)
更新:
這是代碼(在因子網格上生成 4 個時間序列圖):
數據框 (df) 如下所示:
Company Date Value
ABC 08/21/16 500
ABC 08/22/16 600
ABC 08/23/16 650
DEF 08/21/16 625
DEF 08/22/16 675
DEF 08/23/16 680
GHI 08/21/16 500
GHI 08/22/16 600
GHI 08/23/16 650
JKL 08/21/16 625
JKL 08/22/16 675
JKL 08/23/16 680
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
df = pd.read_csv(the_file_name.csv)
g = sns.factorplot(data=df,
x='Date',
y='Value',
col='Company',
col_wrap=2,
sharey=False)
g.set_xlabels('')
g.set_ylabels('product count')
g.set_xticklabels(rotation=45)
plt.show()
您會注意到 x 軸日期顯示在底部 2 個圖上。 我希望 x 軸日期也顯示在前 2 個圖上。
謝謝!
我不確定在seaborn
執行此操作的seaborn
,但是您可以使用 matplotlib Axes
實例來執行此操作。
例如,在這里我們將使用setp
設置xticklabels
可見在所有軸FacetGrid
( g
)。 請注意,我也在此處設置了旋轉,因為 seaborn 不會旋轉上排的ticklabels
。
最后,我增加了子圖之間的空間,以便使用subplots_adjust
為刻度標簽subplots_adjust
。
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from io import StringIO
the_file_name = StringIO(u"""
Company,Date,Value
ABC,08/21/16,500
ABC,08/22/16,600
ABC,08/23/16,650
DEF,08/21/16,625
DEF,08/22/16,675
DEF,08/23/16,680
GHI,08/21/16,500
GHI,08/22/16,600
GHI,08/23/16,650
JKL,08/21/16,625
JKL,08/22/16,675
JKL,08/23/16,680
""")
df = pd.read_csv(the_file_name)
g = sns.factorplot(data=df,
x='Date',
y='Value',
col='Company',
col_wrap=2,
sharey=False)
g.set_xlabels('')
g.set_ylabels('product count')
for ax in g.axes:
plt.setp(ax.get_xticklabels(), visible=True, rotation=45)
plt.subplots_adjust(hspace=0.3)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.