2013-09-24 87 views
5

Tôi nhận dữ liệu trong vài tháng, nhưng trong khoảng một vài tháng bị thiếu. Điều này có vẻ khá lạ nếu tôi vẽ toàn bộ tập dữ liệu trong một ô (nhiều khoảng trống ở giữa). Tôi đã viết kịch bản mẫu nhỏ để cho biết cách hoạt động (dựa trên: Python/Matplotlib - Is there a way to make a discontinuous axis?)Lô thời gian không liên tục âm mưu có ngày trên trục x

Vấn đề: Tôi không thể sử dụng trục x cùng định dạng ngày! Một trong hai axe hoặc ax2 là chính xác, nhưng không bao giờ cả hai. Bạn có ý tưởng gì không?

import numpy as np 
import matplotlib as mpl 
import matplotlib.pyplot as plt 
import datetime 

def getDates(startdate, enddate): 
    days = (enddate + datetime.timedelta(days=1) - startdate).days 
    dates = [ startdate + datetime.timedelta(days=x) for x in range(0,days) ] 
    return dates 

dates1 = getDates(datetime.datetime(2013,1,1), datetime.datetime(2013,1,31)) 
dates2 = getDates(datetime.datetime(2013,3,1), datetime.datetime(2013,3,31)) 
dates = dates1+dates2 
data = np.arange(len(dates)) 

Locator = mpl.dates.DayLocator(interval=5) 
Formatter = mpl.dates.DateFormatter('%d-%m-%y') 

fig,(ax,ax2) = plt.subplots(1,2,sharey=True) 
fig.subplots_adjust(wspace=0.05) 
fig.set_size_inches(10,3) 
ax.plot(dates, data) 
ax2.plot(dates, data) 
ax.legend(loc=1) 
ax.set_ylim(0, 61) 
ax.set_xlim(datetime.datetime(2013,1,1), datetime.datetime(2013,1,31)) 
ax2.set_xlim(datetime.datetime(2013,3,1), datetime.datetime(2013,3,31)) 
labels = ax.get_xticklabels() 
for label in labels: label.set_rotation(30) 
labels = ax2.get_xticklabels() 
for label in labels: label.set_rotation(30) 
ax.spines['right'].set_visible(False) 
ax2.spines['left'].set_visible(False) 
ax.tick_params(right='off') 
ax2.tick_params(left='off') 
ax2.yaxis.tick_right() 
ax.xaxis.set_major_locator(Locator) 
ax.xaxis.set_major_formatter(Formatter) 
ax2.xaxis.set_major_locator(Locator) 
ax2.xaxis.set_major_formatter(Formatter) 
plt.savefig("test.png", bbox_inches='tight') 

Kết quả: Result

+3

Xin đừng sụp đổ 'for' vòng để một dòng, đó là phong cách xấu và có thể nhầm lẫn độc giả của bạn (như tôi) – tacaswell

Trả lời

5

Bạn đã tìm thấy một chi tiết thú vị về bên trong của matplotlib. Đối tượng định vị bạn chuyển vào set_major_locator đối tượng được sử dụng bởi các trục để tìm ra vị trí đặt dấu tích của nó là cả hai axes đang sử dụng cùng một đối tượng locater. Là một phần của bản vẽ, bộ định vị tạo ra một danh sách các dấu tích nên dựa trên các giới hạn của các trục mà khi nó được thực hiện cho các trục thứ hai có nghĩa là không có dấu hiệu nào được hiển thị trong các trục đầu tiên. Bạn chỉ cần truyền các đối tượng định vị riêng biệt (riêng biệt), được thực hiện ở đây với copy.

import datetime 
import copy 

def getDates(startdate, enddate): 
    days = (enddate + datetime.timedelta(days=1) - startdate).days 
    dates = [ startdate + datetime.timedelta(days=x) for x in range(0, days) ] 
    return dates 

dates1 = getDates(datetime.datetime(2013, 1, 1), datetime.datetime(2013, 1, 31)) 
dates2 = getDates(datetime.datetime(2013, 3, 1), datetime.datetime(2013, 3, 31)) 
dates = dates1+dates2 
data = np.arange(len(dates)) 

Locator = mpl.dates.DayLocator(interval=5) 
Formatter = mpl.dates.DateFormatter('%d-%m-%y') 

fig, (ax, ax2) = plt.subplots(1, 2, sharey=True, tight_layout=True) 
fig.subplots_adjust(wspace=0.05) 
fig.set_size_inches(10, 3, forward=True) 

ax.plot(dates, data) 
ax2.plot(dates, data) 

ax.legend(loc=1) 
ax.set_ylim(0, 61) 
ax.set_xlim(datetime.datetime(2013, 1, 1), datetime.datetime(2013, 1, 31)) 
ax2.set_xlim(datetime.datetime(2013, 3, 1), datetime.datetime(2013, 3, 31)) 

labels = ax.get_xticklabels() 
for label in labels: 
    label.set_rotation(30) 
labels = ax2.get_xticklabels() 
for label in labels: 
    label.set_rotation(30) 

ax.spines['right'].set_visible(False) 
ax2.spines['left'].set_visible(False) 
ax.tick_params(right='off') 
ax2.tick_params(left='off') 
ax2.yaxis.tick_right() 


# note the copy here 
ax.xaxis.set_major_locator(copy.copy(Locator)) 
ax.xaxis.set_major_formatter(copy.copy(Formatter)) 
ax2.xaxis.set_major_locator(copy.copy(Locator)) 
ax2.xaxis.set_major_formatter(copy.copy(Formatter)) 

enter image description here

+0

trình, cảm ơn bạn! – HyperCube