繁体   English   中英

在Seaborn多面散点图中格式化日期时间

[英]Format datetime in seaborn faceted scatter plot

我正在从“ R Lattice”的角度学习python pandas + matplotlib + seaborn绘图和数据可视化。 我的腿仍在。 这是我无法正常工作的一个基本问题。 例子如下:

# envir (this is running in an iPython notebook)
%pylab inline

# imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# generate some data
nRows = 500
df = pd.DataFrame({'c1' : np.random.choice(['A','B','C','D'], size=nRows),
               'c2' : np.random.choice(['P','Q','R'], size=nRows),
               'i1' : np.random.randint(20,50, nRows),
               'i2' : np.random.randint(0,10, nRows),
               'x1' : 3 * np.random.randn(nRows) + 90,
               'x2' : 2 * np.random.randn(nRows) + 89,
               't1' : pd.date_range('10/3/2014', periods=nRows)})

# plot a lattice like plot 
# 'hue=' is like 'groups=' in R
# 'col=' is like "|" in lattice formula interface

g = sns.FacetGrid(df, col='c1', hue='c2', size=4, col_wrap=2, aspect=2)
g.map(scatter, 't1', 'x1', s=20)
g.add_legend()

在此处输入图片说明

我希望x轴以适当的日期时间格式而不是整数进行绘制。 我可以指定格式(例如YYYY-MM-DD)作为开始。

但是,最好检查一下时间范围并产生适当的标度。 在R Lattice(和其他绘图系统)中,如果x变量是日期时间,则“漂亮”函数将确定范围是否较大并且仅隐含YYYY(例如,用于绘制20年时间趋势),YYYY-MM(对于绘制一些东西)或高频时间序列数据的YYYY-MM-DD HH:MM:SS格式(即每100毫秒采样一次)。 那是自动完成的。 在这种情况下有什么可用的吗?

关于此示例的另一个真正基本的问题(我几乎不好意思问)。 如何获得该地块的标题?

谢谢!
兰德尔

它看起来像seaborn不支持datetime在轴lmplot呢。 但是,它确实支持其他一些情节。 同时,我建议您在上面的链接中添加您的需求,因为目前看来他们没有足够的感知力来解决这个问题。


至于标题,use可以在对象本身上使用set_title() 看起来像这样:

.
.
.
g = sns.FacetGrid(df, col='c1', hue='c2', size=4, col_wrap=2, aspect=2)
g.map(scatter, 't1', 'x1', s=20)
g.add_legend()

然后只需添加:

g.set_title('Check out that beautiful facet plot!')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM