2013-09-24 416 views
5

我獲得了幾個月的數據,但在幾個月之間失蹤。如果我將整個數據集繪製在一個繪圖(大量空白空間)中,這看起來很奇怪。 我寫了小示例腳本來展示它是如何工作的(基於:Python/Matplotlib - Is there a way to make a discontinuous axis?不連續時間序列在x軸上的日期繪製

問題:我無法使x軸使用相同的日期格式!斧頭或ax2是正確的,但從來都沒有。 你有什麼想法嗎?

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

結果: Result

+3

請不要崩潰'for'循環到一條線,這是不好的風格,可以迷惑你的讀者(像我) – tacaswell

回答

5

你找到關於matplotlib的內部一個有趣的細節。您傳遞給set_major_locator的定位器對象是軸所使用的對象找出在哪裏放置它的刻度。兩個axes都使用相同的定位器對象。作爲繪圖的一部分,定位器生成一個列表,顯示蜱應該基於軸的界限,當第二個軸完成後,第一個軸上沒有蜱。你只需要傳遞不同的(單獨實例化)定位器對象,在這裏完成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

作品,謝謝! – HyperCube

相關問題