2016-01-18 247 views
1

我試圖在x軸上使用日期來繪製一些繪製邊緣,因爲末端的條紋被粘住了。但我不想在軸上使用較大的日期範圍。我嘗試過使用set_xmargin()而沒有運氣。我該如何解決這個問題?繪製x軸的邊緣

import numpy as np 
import matplotlib.pyplot as plt 
import datetime 
import matplotlib.dates as mdates 

today = datetime.date.today() 

imsiDate = [datetime.date(2016, 1, 11), datetime.date(2016, 1, 14), datetime.date(2016, 1, 18), ] 
imsiUp = [13, 6, 24] 
imsiDown = [4, 23, 1] 

ax = plt.subplot() 
ax.set_xmargin(0.75) 
ax.xaxis_date() 
ax.autoscale_view() 

ax.yaxis.grid() 
plotUp = ax.bar(imsiDate, imsiUp, width=0.75, color='r', align='center') 
plotDown = ax.bar(imsiDate, imsiDown, width=0.75, color='y', align='center', bottom=imsiUp) 
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) 
ax.xaxis.set_minor_locator(mdates.DayLocator()) 

#limits 
t0 = today - datetime.timedelta(7) 
t1 = today 
ax.set_xbound(t0, t1) 

plt.xticks(rotation='vertical') 
plt.subplots_adjust(bottom=0.25) 

plt.show() 

回答

0

我想你需要在繪圖後設置頁邊距後調用ax.autoscale_view()。 (0.75是很多餘量,雖然!)

import numpy as np 
import matplotlib.pyplot as plt 
import datetime 
import matplotlib.dates as mdates 

today = datetime.date.today() 

imsiDate = [datetime.date(2016, 1, 11), datetime.date(2016, 1, 14), datetime.date(2016, 1, 18), ] 
imsiUp = [13, 6, 24] 
imsiDown = [4, 23, 1] 

ax = plt.subplot() 
ax.xaxis_date() 

ax.yaxis.grid() 
plotUp = ax.bar(imsiDate, imsiUp, width=0.75, color='r', align='center') 
plotDown = ax.bar(imsiDate, imsiDown, width=0.75, color='y', align='center', bottom=imsiUp) 
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) 
ax.xaxis.set_minor_locator(mdates.DayLocator()) 

#limits 
t0 = today - datetime.timedelta(7) 
t1 = today 
ax.set_xbound(t0, t1) 

ax.set_xmargin(0.75) 
ax.autoscale_view()     # <---- THIS 
plt.xticks(rotation='vertical') 
plt.subplots_adjust(bottom=0.25) 

plt.show() 

enter image description here