简体   繁体   English

Matplotlib麻烦绘制x标签

[英]Matplotlib trouble plotting x-labels

having issues using set_xlim . 使用set_xlim有问题。 (possibly because of datetime objects??) (可能由于日期时间对象?)

here's my code (doing it in ipython notebook): 这是我的代码(在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')

This gives me: 这给了我:

在此处输入图片说明

But when I try to make some extra space on the leftmost and rightmost edges with this code: 但是,当我尝试使用此代码在最左边和最右边留出一些额外的空间时:

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

I get this huge error (this is just the bottom snippet): 我收到这个巨大的错误(这只是最下面的代码段):

    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

Any idea what's going on guys? 知道伙计们怎么了吗? thanks! 谢谢!

For various historical reasons, matplotlib uses an internal numerical date format behind-the-scenes. 由于各种历史原因,matplotlib在幕后使用内部数字日期格式。 The actual x-values are in this data format, where 0.0 is Jan 1st 1900, and a difference of 1.0 corresponds to 1 day. 实际的x值采用这种数据格式,其中0.0为1900年1月1日,而相差1.0则相当于1天。 Negative values aren't allowed. 不允许使用负值。

The error you're getting is because you're trying to set the x-limits to include a negative range. 您收到的错误是因为您试图将x限制设置为包括负范围。 Even without the negative number, though, it would be a range on Jan. 1st, 1900. 即使没有负数,它的范围也是1900年1月1日。

Regardless, it sounds like what you're wanting isn't ax.set_xlim at all. 无论如何,听起来您想要的根本不是ax.set_xlim Try ax.margins(x=0.05) to add 5% padding in the x-direction. 尝试使用ax.margins(x=0.05)在x方向上添加5%的填充。

As an example: 举个例子:

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()

在此处输入图片说明

Note that if you wanted to expand the x-limits by exactly 0.6 in each direction, you'd do something like: 请注意,如果您想将x限制在每个方向上精确地扩展为0.6,则可以执行以下操作:

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

However, ax.margins(percentage) is much easier as long as you're okay with the "padding" being in terms of a ratio of the current axes limits. 但是,只要您对“填充”表示的是当前轴限制的比率,则ax.margins(percentage)会容易得多。

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

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