0

So, my task is pretty simple. I need to draw both bar and stacked bar charts for two columns in Time Series. I draw them, everything is nice, but because of the large range of dates, dates can not be distinguished. Only the start and end dates are visible on the plot:

enter image description here

So, I need to change day ticks to weekly ticks. I traditionally apply DateFormatter and WeekdayLocator and get the plot. Again I get a nice plot except for the fact that the plot actually goes on beyond the last date of the dataset. I am stuck and can not explain myself why such a thing happens. Plot goes on by itself or do I have an error somewhere?

enter image description here

As you can see in the graph, the last tick is 07-28, whereas the dataset's last date is 2021-07-10.

Here is my code:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.dates import DateFormatter

vaccinations_final = pd.read_csv('dataset.csv', index_col = 'date')

#Bar Chart with days on x-axis
ax = vaccinations_final[["cumPeopleVaccinatedFirstDoseByVaccinationDate", "cumPeopleVaccinatedSecondDoseByVaccinationDate"]].plot(kind="bar", figsize=(20, 10)) 

#Bar Chart with months and weeks on x-axis
ax_1 = vaccinations_final[["cumPeopleVaccinatedFirstDoseByVaccinationDate", "cumPeopleVaccinatedSecondDoseByVaccinationDate"]].plot(kind="bar", figsize=(20, 10))
date_form = DateFormatter("%m-%d")
ax_1.xaxis.set_major_formatter(date_form)
ax_1.xaxis.set_major_locator(mdates.WeekdayLocator(interval=1))

Here is the dataset:

date,newPeopleVaccinatedFirstDoseByVaccinationDate,cumPeopleVaccinatedFirstDoseByVaccinationDate,newPeopleVaccinatedSecondDoseByVaccinationDate,cumPeopleVaccinatedSecondDoseByVaccinationDate,Year,Month,Week
2020-12-08,5294,5294,0.0,0.0,2020,December,50
2020-12-09,9580,14874,0.0,0.0,2020,December,50
2020-12-10,11808,26682,0.0,0.0,2020,December,50
2020-12-11,12474,39156,0.0,0.0,2020,December,50
2020-12-12,10574,49730,0.0,0.0,2020,December,50
2020-12-13,6136,55866,0.0,0.0,2020,December,50
2020-12-14,12185,68051,0.0,0.0,2020,December,51
2020-12-15,59770,127821,0.0,0.0,2020,December,51
2020-12-16,97265,225086,0.0,0.0,2020,December,51
2020-12-17,96740,321826,0.0,0.0,2020,December,51
2020-12-18,77505,399331,0.0,0.0,2020,December,51
2020-12-19,90487,489818,0.0,0.0,2020,December,51
2020-12-20,79397,569215,0.0,0.0,2020,December,51
2020-12-21,64128,633343,0.0,0.0,2020,December,52
2020-12-22,86948,720291,0.0,0.0,2020,December,52
2020-12-23,80648,800939,0.0,0.0,2020,December,52
2020-12-24,27500,828439,0.0,0.0,2020,December,52
2020-12-25,257,828696,0.0,0.0,2020,December,52
2020-12-26,1750,830446,0.0,0.0,2020,December,52
2020-12-27,4903,835349,0.0,0.0,2020,December,52
2020-12-28,9031,844380,0.0,0.0,2020,December,53
2020-12-29,46606,890986,2281.0,2281.0,2020,December,53
2020-12-30,109559,1000545,4268.0,6549.0,2020,December,53
2020-12-31,89804,1090349,4801.0,11350.0,2020,December,53
2021-01-01,24716,1115065,1099.0,12449.0,2021,January,53
2021-01-02,29992,1145057,4700.0,17149.0,2021,January,53
2021-01-03,16363,1161420,3808.0,20957.0,2021,January,53
2021-01-04,27734,1189154,9290.0,30247.0,2021,January,1
2021-01-05,58379,1247533,47105.0,77352.0,2021,January,1
2021-01-06,89357,1336890,80848.0,158200.0,2021,January,1
2021-01-07,202662,1539552,79148.0,237348.0,2021,January,1
2021-01-08,249336,1788888,59548.0,296896.0,2021,January,1
2021-01-09,212986,2001874,63532.0,360428.0,2021,January,1
2021-01-10,108961,2110835,44770.0,405198.0,2021,January,1
2021-01-11,112313,2223148,15940.0,421138.0,2021,January,2
2021-01-12,175173,2398321,10524.0,431662.0,2021,January,2
2021-01-13,252374,2650695,6375.0,438037.0,2021,January,2
2021-01-14,287367,2938062,3720.0,441757.0,2021,January,2
2021-01-15,333264,3271326,2250.0,444007.0,2021,January,2
2021-01-16,289577,3560903,1243.0,445250.0,2021,January,2
2021-01-17,149137,3710040,756.0,446006.0,2021,January,2
2021-01-18,150308,3860348,922.0,446928.0,2021,January,3
2021-01-19,295781,4156129,1233.0,448161.0,2021,January,3
2021-01-20,317720,4473849,1597.0,449758.0,2021,January,3
2021-01-21,357976,4831825,1472.0,451230.0,2021,January,3
2021-01-22,470245,5302070,627.0,451857.0,2021,January,3
2021-01-23,460520,5762590,546.0,452403.0,2021,January,3
2021-01-24,189168,5951758,473.0,452876.0,2021,January,3
2021-01-25,189269,6141027,469.0,453345.0,2021,January,4
2021-01-26,226303,6367330,395.0,453740.0,2021,January,4
2021-01-27,234530,6601860,532.0,454272.0,2021,January,4
2021-01-28,335285,6937145,1071.0,455343.0,2021,January,4
2021-01-29,474557,7411702,2196.0,457539.0,2021,January,4
2021-01-30,460598,7872300,2250.0,459789.0,2021,January,4
2021-01-31,278236,8150536,2349.0,462138.0,2021,January,4
2021-02-01,261426,8411962,1269.0,463407.0,2021,February,5
2021-02-02,295615,8707577,1194.0,464601.0,2021,February,5
2021-02-03,362123,9069700,2246.0,466847.0,2021,February,5
2021-02-04,389299,9458999,2651.0,469498.0,2021,February,5
2021-02-05,399052,9858051,1675.0,471173.0,2021,February,5
2021-02-06,471419,10329470,549.0,471722.0,2021,February,5
2021-02-07,223494,10552964,338.0,472060.0,2021,February,5
2021-02-08,234993,10787957,915.0,472975.0,2021,February,6
2021-02-09,304251,11092208,2356.0,475331.0,2021,February,6
2021-02-10,332557,11424765,4295.0,479626.0,2021,February,6
2021-02-11,389056,11813821,4313.0,483939.0,2021,February,6
2021-02-12,428682,12242503,4075.0,488014.0,2021,February,6
2021-02-13,435573,12678076,2266.0,490280.0,2021,February,6
2021-02-14,183410,12861486,722.0,491002.0,2021,February,6
2021-02-15,200653,13062139,1542.0,492544.0,2021,February,7
2021-02-16,307293,13369432,3240.0,495784.0,2021,February,7
2021-02-17,416652,13786084,4685.0,500469.0,2021,February,7
2021-02-18,393881,14179965,4815.0,505284.0,2021,February,7
2021-02-19,316884,14496849,4328.0,509612.0,2021,February,7
2021-02-20,308613,14805462,2777.0,512389.0,2021,February,7
2021-02-21,110666,14916128,2300.0,514689.0,2021,February,7
2021-02-22,145177,15061305,5330.0,520019.0,2021,February,8
2021-02-23,281355,15342660,9780.0,529799.0,2021,February,8
2021-02-24,399281,15741941,14890.0,544689.0,2021,February,8
2021-02-25,437005,16178946,17312.0,562001.0,2021,February,8
2021-02-26,447691,16626637,16649.0,578650.0,2021,February,8
2021-02-27,376071,17002708,15235.0,593885.0,2021,February,8
2021-02-28,156614,17159322,9205.0,603090.0,2021,February,8
2021-03-01,146124,17305446,11740.0,614830.0,2021,March,9
2021-03-02,171927,17477373,28631.0,643461.0,2021,March,9
2021-03-03,228358,17705731,45729.0,689190.0,2021,March,9
2021-03-04,320777,18026508,46135.0,735325.0,2021,March,9
2021-03-05,386735,18413243,37539.0,772864.0,2021,March,9
2021-03-06,389716,18802959,21862.0,794726.0,2021,March,9
2021-03-07,140419,18943378,8296.0,803022.0,2021,March,9
2021-03-08,167342,19110720,22811.0,825833.0,2021,March,10
2021-03-09,176671,19287391,48388.0,874221.0,2021,March,10
2021-03-10,203198,19490589,71330.0,945551.0,2021,March,10
2021-03-11,136358,19626947,54149.0,999700.0,2021,March,10
2021-03-12,312105,19939052,68570.0,1068270.0,2021,March,10
2021-03-13,471245,20410297,39085.0,1107355.0,2021,March,10
2021-03-14,224875,20635172,13300.0,1120655.0,2021,March,10
2021-03-15,320010,20955182,30680.0,1151335.0,2021,March,11
2021-03-16,372748,21327930,67258.0,1218593.0,2021,March,11
2021-03-17,389415,21717345,95451.0,1314044.0,2021,March,11
2021-03-18,458065,22175410,108578.0,1422622.0,2021,March,11
2021-03-19,541501,22716911,101864.0,1524486.0,2021,March,11
2021-03-20,696474,23413385,71630.0,1596116.0,2021,March,11
2021-03-21,294410,23707795,29113.0,1625229.0,2021,March,11
2021-03-22,265368,23973163,64481.0,1689710.0,2021,March,12
2021-03-23,261521,24234684,140387.0,1830097.0,2021,March,12
2021-03-24,268817,24503501,218056.0,2048153.0,2021,March,12
2021-03-25,282042,24785543,230029.0,2278182.0,2021,March,12
2021-03-26,319186,25104729,230489.0,2508671.0,2021,March,12
2021-03-27,413261,25517990,238048.0,2746719.0,2021,March,12
2021-03-28,209591,25727581,93322.0,2840041.0,2021,March,12
2021-03-29,173722,25901303,133054.0,2973095.0,2021,March,13
2021-03-30,168508,26069811,241516.0,3214611.0,2021,March,13
2021-03-31,178634,26248445,348480.0,3563091.0,2021,March,13
2021-04-01,117396,26365841,419876.0,3982967.0,2021,April,13
2021-04-02,66845,26432686,219432.0,4202399.0,2021,April,13
2021-04-03,73831,26506517,156274.0,4358673.0,2021,April,13
2021-04-04,25732,26532249,37151.0,4395824.0,2021,April,13
2021-04-05,16616,26548865,47123.0,4442947.0,2021,April,14
2021-04-06,49097,26597962,156859.0,4599806.0,2021,April,14
2021-04-07,53792,26651754,380998.0,4980804.0,2021,April,14
2021-04-08,50900,26702654,405706.0,5386510.0,2021,April,14
2021-04-09,58681,26761335,410084.0,5796594.0,2021,April,14
2021-04-10,73397,26834732,449206.0,6245800.0,2021,April,14
2021-04-11,34451,26869183,157013.0,6402813.0,2021,April,14
2021-04-12,22475,26891658,166741.0,6569554.0,2021,April,15
2021-04-13,35085,26926743,232473.0,6802027.0,2021,April,15
2021-04-14,77785,27004528,290422.0,7092449.0,2021,April,15
2021-04-15,93424,27097952,365256.0,7457705.0,2021,April,15
2021-04-16,101763,27199715,444250.0,7901955.0,2021,April,15
2021-04-17,114255,27313970,475074.0,8377029.0,2021,April,15
2021-04-18,69583,27383553,192078.0,8569107.0,2021,April,15
2021-04-19,81862,27465415,199688.0,8768795.0,2021,April,16
2021-04-20,82676,27548091,269307.0,9038102.0,2021,April,16
2021-04-21,92471,27640562,338251.0,9376353.0,2021,April,16
2021-04-22,102594,27743156,348886.0,9725239.0,2021,April,16
2021-04-23,107817,27850973,392462.0,10117701.0,2021,April,16
2021-04-24,125321,27976294,490890.0,10608591.0,2021,April,16
2021-04-25,61356,28037650,223306.0,10831897.0,2021,April,16
2021-04-26,62010,28099660,236214.0,11068111.0,2021,April,17
2021-04-27,83534,28183194,307317.0,11375428.0,2021,April,17
2021-04-28,101368,28284562,387867.0,11763295.0,2021,April,17
2021-04-29,110516,28395078,396310.0,12159605.0,2021,April,17
2021-04-30,114538,28509616,356533.0,12516138.0,2021,April,17
2021-05-01,122597,28632213,333769.0,12849907.0,2021,May,17
2021-05-02,70375,28702588,133916.0,12983823.0,2021,May,17
2021-05-03,57866,28760454,93368.0,13077191.0,2021,May,18
2021-05-04,96913,28857367,206063.0,13283254.0,2021,May,18
2021-05-05,106524,28963891,349931.0,13633185.0,2021,May,18
2021-05-06,100379,29064270,422425.0,14055610.0,2021,May,18
2021-05-07,108093,29172363,415026.0,14470636.0,2021,May,18
2021-05-08,139088,29311451,404566.0,14875202.0,2021,May,18
2021-05-09,73933,29385384,155794.0,15030996.0,2021,May,18
2021-05-10,73637,29459021,179758.0,15210754.0,2021,May,19
2021-05-11,99251,29558272,306643.0,15517397.0,2021,May,19
2021-05-12,150597,29708869,403797.0,15921194.0,2021,May,19
2021-05-13,177485,29886354,376635.0,16297829.0,2021,May,19
2021-05-14,188506,30074860,339890.0,16637719.0,2021,May,19
2021-05-15,212088,30286948,355329.0,16993048.0,2021,May,19
2021-05-16,107268,30394216,154747.0,17147795.0,2021,May,19
2021-05-17,81311,30475527,201272.0,17349067.0,2021,May,20
2021-05-18,155790,30631317,266243.0,17615310.0,2021,May,20
2021-05-19,238844,30870161,307420.0,17922730.0,2021,May,20
2021-05-20,236990,31107151,367285.0,18290015.0,2021,May,20
2021-05-21,191701,31298852,370141.0,18660156.0,2021,May,20
2021-05-22,184593,31483445,521386.0,19181542.0,2021,May,20
2021-05-23,103383,31586828,219941.0,19401483.0,2021,May,20
2021-05-24,87801,31674629,258365.0,19659848.0,2021,May,21
2021-05-25,157798,31832427,322044.0,19981892.0,2021,May,21
2021-05-26,207408,32039835,365661.0,20347553.0,2021,May,21
2021-05-27,237420,32277255,356469.0,20704022.0,2021,May,21
2021-05-28,177220,32454475,371164.0,21075186.0,2021,May,21
2021-05-29,158868,32613343,378696.0,21453882.0,2021,May,21
2021-05-30,97803,32711146,173324.0,21627206.0,2021,May,21
2021-05-31,69204,32780350,147579.0,21774785.0,2021,May,22
2021-06-01,71483,32851833,270053.0,22044838.0,2021,June,22
2021-06-02,130140,32981973,281223.0,22326061.0,2021,June,22
2021-06-03,157718,33139691,308588.0,22634649.0,2021,June,22
2021-06-04,147170,33286861,320424.0,22955073.0,2021,June,22
2021-06-05,178722,33465583,424735.0,23379808.0,2021,June,22
2021-06-06,98255,33563838,217332.0,23597140.0,2021,June,22
2021-06-07,83298,33647136,234096.0,23831236.0,2021,June,23
2021-06-08,104586,33751722,246275.0,24077511.0,2021,June,23
2021-06-09,149010,33900732,252538.0,24330049.0,2021,June,23
2021-06-10,173728,34074460,248622.0,24578671.0,2021,June,23
2021-06-11,175724,34250184,247517.0,24826188.0,2021,June,23
2021-06-12,229147,34479331,278298.0,25104486.0,2021,June,23
2021-06-13,121770,34601101,151417.0,25255903.0,2021,June,23
2021-06-14,108399,34709500,163081.0,25418984.0,2021,June,24
2021-06-15,149478,34858978,170873.0,25589857.0,2021,June,24
2021-06-16,169813,35028791,177107.0,25766964.0,2021,June,24
2021-06-17,210870,35239661,171032.0,25937996.0,2021,June,24
2021-06-18,194883,35434544,162662.0,26100658.0,2021,June,24
2021-06-19,252424,35686968,198470.0,26299128.0,2021,June,24
2021-06-20,137860,35824828,78874.0,26378002.0,2021,June,24
2021-06-21,120320,35945148,92339.0,26470341.0,2021,June,25
2021-06-22,156271,36101419,119655.0,26589996.0,2021,June,25
2021-06-23,171415,36272834,118190.0,26708186.0,2021,June,25
2021-06-24,190776,36463610,132297.0,26840483.0,2021,June,25
2021-06-25,171341,36634951,129967.0,26970450.0,2021,June,25
2021-06-26,207493,36842444,176341.0,27146791.0,2021,June,25
2021-06-27,113342,36955786,97258.0,27244049.0,2021,June,25
2021-06-28,96824,37052610,100652.0,27344701.0,2021,June,26
2021-06-29,107634,37160244,110531.0,27455232.0,2021,June,26
2021-06-30,110991,37271235,133946.0,27589178.0,2021,June,26
2021-07-01,113877,37385112,143023.0,27732201.0,2021,July,26
2021-07-02,98204,37483316,135887.0,27868088.0,2021,July,26
2021-07-03,117866,37601182,166703.0,28034791.0,2021,July,26
2021-07-04,59401,37660583,85152.0,28119943.0,2021,July,26
2021-07-05,56118,37716701,103801.0,28223744.0,2021,July,27
2021-07-06,65344,37782045,105163.0,28328907.0,2021,July,27
2021-07-07,67852,37849897,124235.0,28453142.0,2021,July,27
2021-07-08,76995,37926892,131701.0,28584843.0,2021,July,27
2021-07-09,72439,37999331,137868.0,28722711.0,2021,July,27
2021-07-10,74837,38074168,170408.0,28893119.0,2021,July,27
Coup
  • 695
  • 4
  • 6

1 Answers1

0

Pandas.plot Datetime X Axis Correction With Fixed Locator/Formatter

When plotting a bar chart, the xaxis is an array from 0-length(x). Text labels are then placed at these locations. WeekdayLocator then observes x values from 0-len(x) and sets the date to be 1970 (from epoch). In the code, if you set your strftime to include year, you will notice it is the year 1970. It was just a coincidence that the months came close to lining up with your dataset.

To my knowledge, there is not a simple or clever way to solve this for a bar chart. The best solution is often to just use your own fixed locator and formatter which samples the data at the intended interval.

ax.xaxis.set_major_locator(ticker.FixedLocator(np.arange(0, len(vaccinations_final), 15)))
ax.xaxis.set_major_formatter(ticker.FixedFormatter(vaccinations_final.index[::15].strftime('%m-%d')))

Also, the date index should be converted to a datetime.

vaccinations_final.index = pd.to_datetime(vaccinations_final.index)

enter image description here

#Bar Chart with days on x-axis
ax = vaccinations_final[["cumPeopleVaccinatedFirstDoseByVaccinationDate", "cumPeopleVaccinatedSecondDoseByVaccinationDate"]].plot(kind="bar", figsize=(12, 8)) 

ax.xaxis.set_major_locator(ticker.FixedLocator(np.arange(0, len(vaccinations_final), 15)))
ax.xaxis.set_major_formatter(ticker.FixedFormatter(vaccinations_final.index[::15].strftime('%m-%d')))

#Bar Chart with months and weeks on x-axis
ax_1 = vaccinations_final[["cumPeopleVaccinatedFirstDoseByVaccinationDate", "cumPeopleVaccinatedSecondDoseByVaccinationDate"]].plot(kind="bar", figsize=(12, 8))
ax_1.xaxis.set_major_locator(ticker.FixedLocator(np.arange(0, len(vaccinations_final), 15)))
ax_1.xaxis.set_major_formatter(ticker.FixedFormatter(vaccinations_final.index[::15].strftime('%m-%d')))
Coup
  • 695
  • 4
  • 6
  • Thank you for the clear answer! I also wondered which packages I should import to run the code you have provided? Apart from my original packages I import matplotlib and matplotlib.tickers, but it still gives me the following error: 'NameError: name 'ticker' is not defined'. Thank you in advance! –  Jul 18 '21 at 22:46
  • I have resolved the issue above! Just needed to do the following way: 'from matplotlib import ticker'. –  Jul 18 '21 at 22:50