我试图使用日期在x轴上获得一些绘图边距,因为末端的条纹是cliped的。但我不想在轴上使用更大的日期范围。我试过没有运气使用set_xmargin()。我该如何解决这个问题?
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 :(得分: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()