是否可以在Seaborn因子图(Python)中的*每个*图上添加x轴标签? [英] 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)如下:
公司日期值美国广播公司08/21/16 500美国广播公司08/22/16 600美国广播公司08/23/16 650DEF 08/21/16 625DEF 08/22/16 675防守08/23/16 680GHI 08/21/16 500GHI 08/22/16 600GHI 08/23/16 650JKL 08/21/16 625JKL 08/22/16 675JKL 08/23/16 680导入matplotlib.pyplot作为plt将熊猫作为pd导入将 seaborn 作为 sns 导入df = pd.read_csv(the_file_name.csv)g = sns.factorplot(data=df,x='日期',y='值',col='公司',col_wrap = 2,sharey =假)g.set_xlabels('')g.set_ylabels('产品数量')g.set_xticklabels(rotation=45)plt.show()
您会注意到 x 轴日期显示在底部的 2 个图中.我希望 x 轴日期也显示在前 2 个图上.
谢谢!
我不确定在 seaborn
中执行此操作的方法,但是您可以使用matplotlib Axes
实例来执行此操作.
例如,这里我们将使用
I'm using Seaborn to make a Factor Plot.
In total, I have 4 'sub plots' (and use col_wrap =2
, so I have 2 rows, each containing 2 sub plots). Only the 2 sub plots at the very bottom of the grid have x-axis labels (which I believe is the default).
Is it possible to configure the Factor Plot such that each of the 4 plots has x-axis labels? (I couldn't find this option in the Documentation or on StackOverflow)
UPDATE:
Here is the code (which generates 4 time series plots on a Factor Grid):
The dataframe (df) looks as follows:
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()
You'll notice that the x-axis dates are shown on the bottom 2 plots. I'd like for the x-axis dates to be shown on the top 2 plots as well.
Thanks!
I'm not sure of a way to do this in seaborn
, but you can play with the matplotlib Axes
instances to do this.
For example, here we'll use setp
to set the xticklabels
to visible for all axes in the FacetGrid
(g
). Note that I also set the rotation here too, since seaborn would not rotate the ticklabels
on the upper row.
Finally, I have increased the space between subplots to allow room for the tick labels using 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()
这篇关于是否可以在Seaborn因子图(Python)中的*每个*图上添加x轴标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!