월별 데이터로 matplotlib의 막대 너비 제어
-
23-08-2019 - |
문제
바로 한 달에 샘플링 된 데이터를 플로팅하면 폭이 매우 얇습니다. X Axis Minor Locator를 Daylocator ()로 설정하면 막대 너비가 1 일로 조정되는 것을 볼 수 있지만 한 달 내내 채우기를 원합니다.
미미한 진드기 로케이터를 효과없이 Monsonlocator ()로 설정하려고했습니다.
편집하다
어쩌면 예는 더 명확 할 것입니다. 여기에 있습니다. ipython -pylab
내가 의미하는 바의 예 :
x = [datetime.datetime(2008, 1, 1, 0, 0),
datetime.datetime(2008, 2, 1, 0, 0),
datetime.datetime(2008, 3, 1, 0, 0),
datetime.datetime(2008, 4, 1, 0, 0),
datetime.datetime(2008, 5, 1, 0, 0),
datetime.datetime(2008, 6, 1, 0, 0),
datetime.datetime(2008, 7, 1, 0, 0),
datetime.datetime(2008, 8, 1, 0, 0),
datetime.datetime(2008, 9, 1, 0, 0),
datetime.datetime(2008, 10, 1, 0, 0),
datetime.datetime(2008, 11, 1, 0, 0),
datetime.datetime(2008, 12, 1, 0, 0)]
y = cos(numpy.arange(12) * 2)
bar(x, y)
이것은 12 개의 2 픽셀 넓은 바를 제공합니다.
해결책
그냥 사용하십시오 width
키워드 인수 :
bar(x, y, width=30)
또는 달이 다른 날이 다르기 때문에 좋아 보이기 위해 시퀀스를 사용할 수 있습니다.
bar(x, y, width=[(x[j+1]-x[j]).days for j in range(len(x)-1)] + [30])
제휴하지 않습니다 StackOverflow