2015-08-19 19 views
6

Tôi đang cố tạo biểu đồ trực quan hấp dẫn bằng Python. Tôi đã sử dụng ví dụ của Randal Olson http://www.randalolson.com/2014/06/28/how-to-make-beautiful-data-visualizations-in-python-with-matplotlib/ ở đây và cố gắng thực hiện một số điều chỉnh.Thay đổi xlim theo ngày trong Matplotlib

Dữ liệu của tôi rất đơn giản,

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 

Vấn đề là các dữ liệu bắt đầu tải tại tháng hai năm 2015, và tôi muốn bắt đầu hiển thị ở Tháng Tư năm 2015.

Dưới đây là mã của tôi

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)  

Các xlim là những gì tôi đang đấu tranh với. Tôi gặp lỗi

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

TypeError: 'module' object is not callable 

Nếu có ai có thể giúp điều này thật tuyệt vời. Ngoài ra bất kỳ đầu vào trên cố gắng để làm cho nó đẹp hơn cũng sẽ được đánh giá cao.

+2

'datetime.datetime.now' không nhận được c alled, chỉ nhận được bí danh. Thêm parens. Ngoài ra, không gọi 'từ gói nhập *. Đó là yucky. :) ** Chỉnh sửa: ** và 'nhập datetime như datetime' là dư thừa. ** Chỉnh sửa thứ hai: ** 'dstart = ...' phải là 'dstart = datetime.datetime (2015, 4, 1)' –

+0

@AndyKubiak Cảm ơn câu trả lời. Cách tốt hơn để làm 'từ việc nhập gói * là gì? – jenryb

+2

'gói nhập ' –

Trả lời

5

Bạn cần phải gọi datetime.datetime.now() với ngoặc ở cuối dòng, và cho dstart, bạn cần phải sử dụng phương pháp datetime của module datetime: datetime.datetime(2015,4,1)

import datetime 

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

EDIT: Để đặt xticks để ngày đầu tiên của tháng (nhờ @AndyKubiak):

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

Cảm ơn câu trả lời. Có cách nào để chỉ liệt kê đầu tiên của tháng không? Tôi nhận được nhãn ngày 08 tháng 4, ngày 29 tháng 4, ngày 20 tháng 5, ngày 01 tháng 7, v.v. – jenryb

+2

'firsts = []; cho i trong phạm vi (1, 13): firsts.append (datetime.datetime (2015, i, 1)) ' –

+0

@jenryb: Xem tôi chỉnh sửa, sử dụng phương thức từ @AndyKubiak (nhưng giới hạn xticks cho' dstart của bạn 'to' datenow' range) – tom

Các vấn đề liên quan