【问题标题】:Matplotlib trouble plotting x-labelsMatplotlib 无法绘制 x 标签
【发布时间】:2015-07-02 00:59:11
【问题描述】:

在使用 set_xlim 时遇到问题。 (可能是因为日期时间对象??)

这是我的代码(在 ipython 笔记本中执行):

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import datetime

date_list = [datetime.datetime(2015, 6, 20, 0, 0), datetime.datetime(2015, 6, 21, 0, 0), datetime.datetime(2015, 6, 22, 0, 0), datetime.datetime(2015, 6, 23, 0, 0), datetime.datetime(2015, 6, 24, 0, 0), datetime.datetime(2015, 6, 25, 0, 0), datetime.datetime(2015, 6, 26, 0, 0)]
count_list = [11590, 10743, 27369, 31023, 30569, 31937, 30205]

fig=plt.figure(figsize=(10,3.5))
ax=fig.add_subplot(111)

width = 0.8

tickLocations = np.arange(7)

ax.set_title("Turnstiles Totals for Lexington Station C/A A002 Unit R051 from 6/20/15-6/26-15")
ax.bar(date_list, count_list, width, color='wheat', edgecolor='#8B7E66', linewidth=4.0)
ax.set_xticklabels(date_list, rotation = 315, horizontalalignment = 'left')

这给了我:

但是当我尝试用这段代码在最左边和最右边留出一些额外的空间时:

ax.set_xlim(xmin=-0.6, xmax=0.6)

我得到这个巨大的错误(这只是底部的 sn-p):

    223         tz = _get_rc_timezone()
    224     ix = int(x)
--> 225     dt = datetime.datetime.fromordinal(ix)
    226     remainder = float(x) - ix
    227     hour, remainder = divmod(24 * remainder, 1)

ValueError: ordinal must be >= 1

知道发生了什么事吗?谢谢!

【问题讨论】:

    标签: python datetime matplotlib


    【解决方案1】:

    由于各种历史原因,matplotlib 在幕后使用内部数字日期格式。实际的 x 值采用这种数据格式,其中 0.0 是 1900 年 1 月 1 日,1.0 的差异对应于 1 天。不允许使用负值。

    您遇到的错误是因为您尝试将 x 限制设置为包含负范围。不过,即使没有负数,它也是 1900 年 1 月 1 日的范围。

    无论如何,听起来你想要的根本不是ax.set_xlim。尝试ax.margins(x=0.05) 在 x 方向添加 5% 的填充。

    举个例子:

    import matplotlib.pyplot as plt
    import numpy as np
    import datetime
    
    count_list = [11590, 10743, 27369, 31023, 30569, 31937, 30205]
    date_list = [datetime.datetime(2015, 6, 20, 0, 0),
                 datetime.datetime(2015, 6, 21, 0, 0),
                 datetime.datetime(2015, 6, 22, 0, 0),
                 datetime.datetime(2015, 6, 23, 0, 0),
                 datetime.datetime(2015, 6, 24, 0, 0),
                 datetime.datetime(2015, 6, 25, 0, 0),
                 datetime.datetime(2015, 6, 26, 0, 0)]
    
    fig, ax = plt.subplots(figsize=(10,3.5))
    ax.set_title("Turnstiles Totals for Lexington Station C/A A002 Unit R051 from "
                 "6/20/15-6/26-15")
    
    # The only difference is the align kwarg: I've centered the bars on each date
    ax.bar(date_list, count_list, align='center', color='wheat',
           edgecolor='#8B7E66', linewidth=4.0)
    
    # This essentially just rotates the x-tick labels. We could have done
    # "fig.autofmt_xdate(rotation=315, ha='left')" to match what you had.
    fig.autofmt_xdate()
    
    # Add the padding that you're after. This is 5% of the data limits.
    ax.margins(x=0.05)
    
    plt.show()
    

    请注意,如果您想在每个方向上将 x 限制扩大 0.6,您可以执行以下操作:

    xmin, xmax = ax.get_xlim()
    ax.set_xlim([xmin - 0.6, xmax + 0.6])
    

    但是,ax.margins(percentage) 会容易得多,只要您同意“填充”是指当前轴限制的比率。

    【讨论】:

      猜你喜欢
      • 2017-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      相关资源
      最近更新 更多