简体   繁体   English

如何正确地将Seaborn / matplotlib中的x轴与时间轴隔开?

[英]How to properly space the x-axis in seaborn/matplotlib with a time axis?

I have a pandas dataframe with the time-axis set as the index. 我有一个以时间轴为索引的熊猫数据框。 When plotting the following code 绘制以下代码时

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns; sns.set();
ids = [1,2,3,4]
dates = pd.date_range('20150704', '20160331')
vals = np.random.randn(len(ids)*len(dates))
allids = np.tile(ids, len(dates))
alldates = np.tile(dates, len(ids))
df = (pd.DataFrame(np.vstack((allids, vals)).T, index=alldates)
    .reset_index().rename(columns={'index':'datetime',0:'unique_id',1:'height'}))

time_group = '1w'
threshold = 0.50

fig, axes = plt.subplots(nrows=4, ncols=1, figsize=(12, 16), sharex=True)

for (i, temp), ax in zip(df.groupby('unique_id'), axes.ravel()):
    (temp.set_index('datetime').height
         .groupby(pd.TimeGrouper(time_group))
         .mean()
         .plot(ax=ax))

I get a plot with 4 plots. 我得到一个有4个地块的地块。 The spacing of the vertical lines on the x-axis looks like: x轴上垂直线的间距如下所示:

垂直间距错误

Notice how, for example, November is wider than December (so it isn't merely the number of days in each month). 请注意,例如11月比12月宽(因此,不仅仅是每个月的天数)。 Apparently this is due to the number of data points in each month, not the number of days in each month. 显然,这是由于每个月的数据点数,而不是每个月的天数。 Any suggestions to make the vertical lines spaced with the start/end of the months properly in time? 有什么建议可以使垂直线与月份的开始/结束时间正确地间隔开? Is this a bug? 这是错误吗?

The 1W will group based on Sundays in each month and there are some months that we have more Sundays compare to others... That's why some months are wider. 1W将根据每月的星期日进行分组,在某些月份中,我们的星期日比其他月份要多。这就是为什么有些月份更宽的原因。 Below change should help... 进行以下更改应有帮助...

    %matplotlib inline
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    import matplotlib.dates as mdates
    import seaborn as sns; sns.set();
    ids = [1,2,3,4]
    dates = pd.date_range('20150704', '20160331')
    vals = np.random.randn(len(ids)*len(dates))
    allids = np.tile(ids, len(dates))
    alldates = np.tile(dates, len(ids))
    df = (pd.DataFrame(np.vstack((allids, vals)).T, index=alldates)
        .reset_index().rename(columns={'index':'datetime',0:'unique_id',1:'height'}))
    time_group = '1w'
    threshold = 0.50

    fig, axes = plt.subplots(nrows=4, ncols=1, figsize=(12, 16), sharex=True)


    for (i, temp), ax in zip(df.groupby('unique_id'), axes.ravel()):
        b=(temp.set_index('datetime').height
             .groupby(pd.TimeGrouper(time_group))
             .mean())
        ax.plot(b.index,b)
        ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))
    plt.show()

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

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