gpt4 book ai didi

python - 在 Matplotlib 中按日期更改 xlim

转载 作者:太空狗 更新时间:2023-10-30 00:02:22 25 4
gpt4 key购买 nike

我正在尝试用 Python 制作一个视觉上吸引人的图表。我使用的是 Randal Olson 的示例 http://www.randalolson.com/2014/06/28/how-to-make-beautiful-data-visualizations-in-python-with-matplotlib/在这里并尝试进行一些调整。

我的数据很简单,

dispute_percentage
Out[34]:

2015-08-11 0.017647
2015-08-12 0.004525
2015-08-13 0.006024
2015-08-14 0.000000
2015-08-15 0.000000
2015-08-17 0.000000

问题是数据从2015年2月开始加载,我想在2015年4月开始显示。

这是我的代码

from __future__ import division
from collections import OrderedDict
import pandas as pd
from collections import Counter
from pylab import *
import datetime as datetime
dispute_percentage.plot(kind = 'line')
plt.xlabel('Date')
plt.ylabel('Percent')
plt.title('Percent Disputes In FY2015')

# Remove the plot frame lines. They are unnecessary chartjunk.
ax = plt.subplot(111)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)


# Ensure that the axis ticks only show up on the bottom and left of the plot.
# Ticks on the right and top of the plot are generally unnecessary chartjunk.
ax.get_xaxis().tick_bottom()
#ax.get_yaxis().tick_left()

# Limit the range of the plot to only where the data is.
# Avoid unnecessary whitespace.
datenow = datetime.datetime.now
dstart = datetime(2015,4,1)
print datenow
plt.ylim(0, .14)
plt.xlim(dstart, datenow)

xlim 是我正在努力解决的问题。我遇到了错误

File "C:/Mypath/name.py", line 52, in <module>
dstart = datetime(2015,4,1)

TypeError: 'module' object is not callable

如果有人可以帮助解决这个问题,那就太好了。此外,任何有关尝试使其更漂亮的意见也将不胜感激。

最佳答案

您需要调用datetime.datetime.now()括号在末尾,对于 dstart , 你需要使用 datetime datetime 的方法模块:datetime.datetime(2015,4,1)

import datetime

datenow = datetime.datetime.now()
dstart = datetime.datetime(2015,4,1)

编辑:设置 xticks到本月的第一天(感谢@AndyKubiak):

firsts=[]
for i in range(dstart.month, datenow.month+1):
firsts.append(datetime.datetime(2015,i,1))
plt.xticks(firsts)

关于python - 在 Matplotlib 中按日期更改 xlim,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32100912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com