使用 Seaborn FacetGrid 格式化日期标签 [英] Formatting Date labels using Seaborn FacetGrid

查看:52
本文介绍了使用 Seaborn FacetGrid 格式化日期标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to make a facet grid with variable names as the columns, and departments as the rows, and each small chart is a scatter chart of y=value and x=date

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

My data is sort of like this: import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt from datetime import datetime import matplotlib.dates as mdates import random

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的散点图

I want to make a facet grid with variable names as the columns, and departments as the rows, and each small chart is a scatter chart of y=value and x=date

这是我目前所拥有的.它几乎可以工作,除了我想看到底部没有压在一起的日期,所以我想看到3/1 4/1 5/1"而不是完整的日期.但我不知道如何格式化它.

Here is what I have so far. It almost works, except I want to see dates along the bottom that are not squished together, so I would like to see "3/1 4/1 5/1" instead of full dates. But I can't figure out how to format it.

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 并且我可以控制有多少刻度线出现在那里.

This is pretty close, but notice the dates along the bottom x axes. They are all scrunched together. That's why I tried to use a special date formatter but couldn't get it to work. Really what I would like is that each date shows up as mon-dd and that I can control how many tick marks appear there.

推荐答案

您可以以 g.axes(二维数组)的形式访问 FacetGrid 的 Axes 对象.您可以遍历此数组并更改所有轴的属性,但在您的情况下,您有 sharex=True(默认值),这意味着更改子图之一的 xaxis 将更改所有子图的同时.

You can access the Axes object of the FacetGrid as g.axes (a 2D array). You could iterate over this array and change the properties of all the axes, but in your case you have sharex=True (the default), that means that changing the xaxis of one of the subplots will change all of the subplots at the same time.

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)

这篇关于使用 Seaborn FacetGrid 格式化日期标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆