【问题标题】:Format datetime in seaborn faceted scatter plot在 seaborn 多面散点图中格式化日期时间
【发布时间】:2014-11-28 01:57:28
【问题描述】:

我正在从“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 毫秒采样一次)。这是自动完成的。这种情况下有类似的东西吗?

关于这个例子的另一个非常基本的问题(我几乎不好意思问)。我怎样才能得到这个情节的标题?

谢谢!
兰德尔

【问题讨论】:

    标签: python date datetime matplotlib


    【解决方案1】:

    它看起来像seaborn does not support datetime on the axes in lmplot。但是,它确实支持其他一些情节。同时,我建议您在上面的链接中添加您对问题的需求,因为目前看来他们认为没有足够的需求来解决它。


    就标题而言,可以在对象本身上使用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!')
    

    【讨论】:

      猜你喜欢
      • 2019-05-31
      • 2021-09-12
      • 2020-10-10
      • 2015-02-12
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      相关资源
      最近更新 更多