Pandas 使用Seaborn FaceGrid格式化日期标签

Pandas 使用Seaborn FaceGrid格式化日期标签,pandas,seaborn,Pandas,Seaborn,我想制作一个刻面网格,变量名作为列,部门作为行,每个小图表都是一个y=value和x=date的散点图 我的数据有点像这样: 作为pd进口熊猫 将numpy作为np导入 导入seaborn作为sns 将matplotlib.pyplot作为plt导入 从日期时间导入日期时间 将matplotlib.dates导入为mdates 随机输入 datelist = pd.date_range(start="march 1 2020", end="may 20 2020", freq="w").toli

我想制作一个刻面网格,变量名作为列,部门作为行,每个小图表都是一个y=value和x=date的散点图

我的数据有点像这样: 作为pd进口熊猫 将numpy作为np导入 导入seaborn作为sns 将matplotlib.pyplot作为plt导入 从日期时间导入日期时间 将matplotlib.dates导入为mdates 随机输入

datelist = pd.date_range(start="march 1 2020", end="may 20 2020", freq="w").tolist()
varlist = ["x", "y", "z", "x", "y", "z", "x", "y", "z", "x", "y", "z"]
deptlist = ["a", "a", "b", "a", "a", "b", "a", "a", "b", "a", "a", "b"]
vallist =  random.sample(range(10, 30), 12)
df = pd.DataFrame({'date': datelist, 'value': vallist, 'variable': varlist, 'department': deptlist})
我想制作一个刻面网格,变量名作为列,部门作为行,每个小图表都是一个y=value和x=date的散点图

这是我到目前为止所拥有的。它几乎可以工作,除了我想看到沿着底部的日期没有挤在一起,所以我想看到“3/14/15/1”而不是完整的日期。但我不知道如何格式化它

plt.style.use('seaborn-darkgrid')
xformatter = mdates.DateFormatter("%m-%d")
g = sns.FacetGrid(df2, row="department", col="variable", sharey='row')
g = g.map(plt.plot, "date", "value", marker='o', markersize=0.7)
datelist = pd.date_range(start="march 1 2020", end="june 1 2020", freq="MS").tolist()
g.set(xticks=datelist)


这非常接近,但请注意底部x轴上的日期。他们都挤在一起。这就是为什么我试图使用一个特殊的日期格式化程序,但无法使其工作。实际上,我希望每个日期都显示为mon dd,并且我可以控制有多少记号出现在那里。

您可以访问FaceGrid的Axes对象,即
g.Axes
(2D数组)。您可以迭代此数组并更改所有轴的属性,但在您的示例中,您有
sharex=True
(默认值),这意味着更改其中一个子地块的xaxis将同时更改所有子地块

g = sns.FacetGrid(df, row="department", col="variable", sharey='row')
g = g.map(plt.plot, "date", "value", marker='o', markersize=0.7)

xformatter = mdates.DateFormatter("%m/%d")
g.axes[0,0].xaxis.set_major_formatter(xformatter)

谢谢!天才:)顺便问一下,我得到了这个我一直忽略的信息,但它意味着什么?“/usr/local/lib/python3.7/site packages/pandas/plotting/_matplotlib/converter.py:103:FutureWarning:对matplotlib绘图方法使用隐式注册的日期时间转换器。转换器是由pandas在导入时注册的。pandas的未来版本将要求您显式注册matplotlib转换器。若要注册转换程序rs:>>>来自pandas.plotting导入寄存器\u matplotlib\u转换器>>>寄存器\u matplotlib\u转换器()警告。警告(msg,FutureWarning)