本文介绍了在Matplotlib中按日期更改xlim的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Python中制作一个视觉上吸引人的图形。我正在使用Randal Olson的例子这里试图让一些调整。



我的数据很简单,

  dispute_percentage 
出[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月开始显示。



这是我的代码

 来自__future__进口部门
来自集合import OrderedDict
将pandas作为pd
从集合import import Counter b $ b from pylab import *
导入日期时间作为日期时间
disp ute_percentage.plot(kind ='line')
plt.xlabel('Date')
plt.ylabel('Percent')
plt.title('2015财年争议百分比')

#删除绘图框线。他们是不必要的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)


#确保轴刻度只显示在情节的底部和左侧。
#右边和右上方的滴答通常是不必要的chartjunk。
ax.get_xaxis()。tick_bottom()
#ax.get_yaxis()。tick_left()

#将绘图的范围限制在数据所在的位置。
#避免不必要的空白。
datenow = datetime.datetime.now
dstart = datetime(2015,4,1)
print datenow
plt.ylim(0,.14)
plt。 xlim(dstart,datenow)

xlim是我挣扎着的。我在< module>中得到错误

 文件C:/Mypath/name.py,第52行。 
dstart = datetime(2015,4,1)

TypeError:'module'对象不可调用

如果任何人都能帮上忙,那将会很棒。此外,任何关于试图使它更漂亮的输入也将不胜感激。 你需要调用 datetime .datetime.now()加上括号,并且对于 dstart ,您需要使用 datetime datetime 模块的方法: datetime.datetime(2015,4,1)

 导入日期时间

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

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

  firsts = [] 
我在范围内(dstart.month,datenow.month + 1):
firsts.append(datetime.datetime(2015,i,1))
plt.xticks(第一)


I am trying to make a visually appealing graph in Python. I was using Randal Olson's example http://www.randalolson.com/2014/06/28/how-to-make-beautiful-data-visualizations-in-python-with-matplotlib/ here and trying to make some adjustments.

My data is simple,

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

The problem is that the data starts loading at Feb 2015, and I want to start displaying at April 2015.

Here is my code

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)    

The xlim is what I am struggling with. I'm getting the error

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

TypeError: 'module' object is not callable

If anyone could help with this that would be great. Also any input on trying to make it prettier would also be appreciated.

解决方案

You need to call datetime.datetime.now() with the parentheses on the end, and for dstart, you need to use the datetime method of the datetime module: datetime.datetime(2015,4,1)

import datetime

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

EDIT: To set the xticks to the first of the month (thanks to @AndyKubiak):

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

这篇关于在Matplotlib中按日期更改xlim的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 22:07