在时间序列的同一图上绘制线图和堆积条形图

问题描述

因此,我的目标是使用时间序列中的 Pandas matplotlib 在同一图形上显示两条线图和一个堆积条形图。我已按照以下链接 Plot Pandas DataFrame as Bar and Line on the same one chart 中的步骤操作,但没有按预期工作。我得到的只是显示的堆积条形图:

Only stacked bar plot displayed

这是我的代码

import pandas as pd
import matplotlib.pyplot as plt

df_max_scaled = pd.read_csv('dataset.csv',index_col='sample_date')

start,end = '2021-06-01','2021-07-08'
fig,ax = plt.subplots(figsize=(20,10))
ax.plot(df_max_scaled.loc[start:end,'cumPeopleVaccinatedFirstDoseByVaccinationDate'],marker='.',linestyle='-',linewidth=0.5,label='FirstVaccine')
ax.plot(df_max_scaled.loc[start:end,'cumPeopleVaccinatedSecondDoseByVaccinationDate'],label='SecondVaccine')
df_max_scaled.loc[start:end].plot(kind='bar',stacked=True,y=['B.1.1.7','B.1.617.2'],ax=ax)

这是数据集.csv:

sample_date,B.1.1.7,B.1.617.2,cumPeopleVaccinatedFirstDoseByVaccinationDate,cumPeopleVaccinatedSecondDoseByVaccinationDate
2020-06-14,0.00031938677738741617,0.0,0.0
2020-06-22,0.0
2020-08-30,0.0
2020-09-03,0.0
2020-09-04,0.0
2020-09-20,0.0
2020-09-21,0.0
2020-09-23,0.0
2020-09-30,0.0006387735547748323,0.0
2020-10-01,0.0
2020-10-08,0.0
2020-10-09,0.0
2020-10-11,0.0009581603321622484,0.0
2020-10-12,0.0
2020-10-15,0.0
2020-10-17,0.0
2020-10-19,0.0
2020-10-20,0.0
2020-10-21,0.0047908016608112424,0.0
2020-10-22,0.00415202810603641,0.0
2020-10-23,0.0019163206643244969,0.0
2020-10-24,0.0015969338869370809,0.0
2020-10-25,0.0
2020-10-26,0.0
2020-10-27,0.0
2020-10-29,0.005748961992973491,0.0
2020-10-30,0.0028744809964867456,0.0
2020-10-31,0.0
2020-11-01,0.012456084318109231,0.0
2020-11-02,0.019801980198019802,0.0
2020-11-03,0.0025550942190993293,0.0
2020-11-04,0.0
2020-11-05,0.03257745129351645,0.0
2020-11-06,0.01341424465027148,0.0
2020-11-07,0.0
2020-11-08,0.031938677738741615,0.0
2020-11-09,0.013733631427658896,0.0
2020-11-10,0.0
2020-11-11,0.013094857872884063,0.0
2020-11-12,0.048866176940274675,0.0
2020-11-13,0.05908655381667199,0.0
2020-11-14,0.041200894282976686,0.0
2020-11-15,0.028744809964867453,0.0
2020-11-16,0.0
2020-11-17,0.032896838070903864,0.0
2020-11-18,0.0316192909613542,0.0
2020-11-19,0.021079527307569467,0.0
2020-11-20,0.011817310763334398,0.0
2020-11-21,0.03928457361865219,0.0
2020-11-22,0.08463749600766528,0.0
2020-11-23,0.0
2020-11-24,0.0
2020-11-25,0.0063877355477483235,0.0
2020-11-26,0.03385499840306611,0.0
2020-11-27,0.029383583519642285,0.0
2020-11-28,0.024912168636218462,0.0
2020-11-29,0.01820504631108272,0.0
2020-11-30,0.012136697540721815,0.0
2020-12-01,0.0
2020-12-02,0.023634621526668797,0.0
2020-12-03,0.018524433088470137,0.0
2020-12-04,0.053018205046311086,0.0
2020-12-05,0.03481315873522836,0.0
2020-12-06,0.02331523474928138,0.0
2020-12-07,0.09773235388054935,0.0
2020-12-08,0.07697221335036729,0.0001401194667996399,0.0
2020-12-09,0.06259980836793357,0.0003936790610460604,0.0
2020-12-10,0.3136378153944427,0.0007062084648938405,0.0
2020-12-11,0.12903225806451613,0.001036365289385474,0.0
2020-12-12,0.09070584477802619,0.0013162336766048529,0.0
2020-12-13,0.18300862344298946,0.001478638861395671,0.0
2020-12-14,0.2111146598530821,0.0018011465499022088,0.0
2020-12-15,0.23890130948578728,0.0033831149160930807,0.0
2020-12-16,0.25167678058128395,0.00595748589045405,0.0
2020-12-17,0.31523474928137973,0.008517961375568739,0.0
2020-12-18,0.32864899393165126,0.01056933260229826,0.0
2020-12-19,0.17470456723091665,0.012964306193590104,0.0
2020-12-20,0.15713829447460875,0.015065754116803365,0.0
2020-12-21,0.21047588629830724,0.01676306827753765,0.0
2020-12-22,0.14723730437559884,0.01906437303751028,0.0
2020-12-23,0.14564037048866177,0.021198931926527536,0.0
2020-12-24,0.15170871925902268,0.02192679088704701,0.0
2020-12-25,0.021933593059878045,0.0
2020-12-26,0.2823379112104759,0.02197991135736565,0.0
2020-12-27,0.19801980198019803,0.02210968199312663,0.0
2020-12-28,0.1782178217821782,0.022348710875761225,0.0
2020-12-29,0.4231874800383264,0.023582259774451067,8.051846123113752e-05
2020-12-30,0.264771638454168,0.026482023405562088,0.00023117729180303356
2020-12-31,0.30150111785372086,0.02885891962703448,0.0004006508263802765
2021-01-01,0.2034493771957841,0.02951309279315082,0.00043944512225621693
2021-01-02,0.5397636537847333,0.030306909009292643,0.0006053533939731596
2021-01-03,0.41807729160012774,0.03073999832460101,0.0007397743936961635
2021-01-04,0.4573618652187799,0.03147405070318454,0.0010677079775792268
2021-01-05,0.39220696263174704,0.03301920263977241,0.002730497156138075
2021-01-06,0.25902267646119453,0.03538426784468655,0.005584401826727731
2021-01-07,0.3813478122005749,0.040748244304933734,0.008378297122441045
2021-01-08,0.49313318428617053,0.047347569460573134,0.01048031962546243
2021-01-09,0.5889492175023954,0.05298479740839862,0.012722975863488133
2021-01-10,0.4072181411689556,0.05586873341556816,0.014303340400672711
2021-01-11,0.5959757266049186,0.05884138881312539,0.01486601653921911
2021-01-12,0.7294793995528586,0.06347779745643731,0.01523750986933594
2021-01-13,0.46343021398914086,0.0701575311765152,0.015462545025122218
2021-01-14,0.5752155860747366,0.07776344557315519,0.015593859657204565
2021-01-15,0.6528265729798787,0.0865841433411029,0.015673283829835016
2021-01-16,0.4768444586394123,0.0942485511305701,0.015717161272759305
2021-01-17,0.48067709996806135,0.09819584937766074,0.015743847794763136
2021-01-18,0.6384541679974449,0.10217414118267024,0.015776394055725482
2021-01-19,0.5835196422868093,0.11000275395363061,0.01581991850232697
2021-01-20,0.5263494091344618,0.11841203936949415,0.01587629201507845
2021-01-21,0.5052698818268924,0.12788680443316394,0.015928253073794904
2021-01-22,0.6684765250718621,0.14033306032005413,0.015950385943234592
2021-01-23,0.510380070265091,0.15252191881090607,0.01596965954245958
2021-01-24,0.4065793676141808,0.157528741496126,0.015986356268528117
2021-01-25,0.9773235388054935,0.16253823740880094,0.01600291179606753
2021-01-26,0.8543596295113383,0.1685279343667078,0.01601685515081821
2021-01-27,0.8361545832002555,0.1747353802580035,0.016035634555191275
2021-01-28,0.7869690194825935,0.18360956904264975,0.01607344046136337
2021-01-29,1.0,0.1961699532145494,0.01615095845385069
2021-01-30,0.7502395400830406,0.20836087617808935,0.016230382626481143
2021-01-31,0.442031299904184,0.2157251149322383,0.016313301462707333
2021-02-01,0.5059086553816672,0.22264443335452064,0.01635809669607091
2021-02-02,0.45959757266049184,0.23046865250411935,0.016400244457013467
2021-02-03,0.4410731395720217,0.24005317869903547,0.016479527431114797
2021-02-04,0.5534972852123922,0.2503569883525362,0.016573106756289608
2021-02-05,0.4682210156499521,0.26091893649483505,0.016632233640358946
2021-02-06,0.42606196103481314,0.27339626534243977,0.016651613138480775
2021-02-07,0.44746087511977006,0.2793116148159794,0.016663544414191482
2021-02-08,0.6256786969019482,0.2855313152054104,0.016695843577727867
2021-02-09,0.5570105397636538,0.2935841085362108,0.01677900951138002
2021-02-10,0.6202491216863621,0.3023860937119735,0.01693062143202348
2021-02-11,0.3126834717390231,0.017082868746047985
2021-02-12,0.6327052060044714,0.3240296548267835,0.017226714747589803
2021-02-13,0.47971893963589907,0.3355582261362507,0.01730670371433674
2021-02-14,0.5729798786330246,0.00024857071836937607,0.34041264838893714,0.01733219004884304
2021-02-15,0.8738422229319707,0.34572345144366856,0.01738662208181911
2021-02-16,0.7093580325774513,0.35385675920930165,0.01750099289040696
2021-02-17,0.36488453708633295,0.0176663716676397
2021-02-18,0.5509421909932929,0.3753096212764555,0.017836339397068867
2021-02-19,0.5723411050782498,0.3836967797799193,0.017989116205577575
2021-02-20,0.5688278505269881,0.3918650247756573,0.018087143284419693
2021-02-21,0.6675183647396997,0.39479408803837907,0.018168332438664152
2021-02-22,0.6387735547748323,0.39863657459515495,0.018356479478717623
2021-02-23,0.560204407537528,0.40608336578922605,0.018701709882417983
2021-02-24,0.5519003513254551,0.4166513750115961,0.01922732140707017
2021-02-25,0.8153944426700734,0.4282178479222075,0.019838428641105004
2021-02-26,0.5697860108591505,0.44006715359107745,0.020426132218938062
2021-02-27,0.39252634940913445,0.45002084985076907,0.02096392211672692
2021-02-28,0.422868093260939,0.4541660463323253,0.021288855231866164
2021-03-01,0.6499520919833919,0.4580335977049416,0.02170327291483572
2021-03-02,0.6084318109230278,0.4625840925233137,0.02271393668665014
2021-03-03,0.613222612583839,0.46862818039627024,0.024328153571191433
2021-03-04,0.4525710635579687,0.47711837726094497,0.025956702106438488
2021-03-05,0.5560523794314916,0.48735432399172673,0.027281815002604938
2021-03-06,0.3538805493452571,0.4976691706338289,0.02805353556351468
2021-03-07,0.25806451612903225,0.5013857243566355,0.028346381312911225
2021-03-08,0.527307569466624,0.50581486523559,0.02915160122485488
2021-03-09,0.5104909223415514,0.030859679831629225
2021-03-10,0.7920792079207921,0.5158690854346291,0.03337760260217593
2021-03-11,0.7604599169594379,0.5194781542396659,0.035289042390516515
2021-03-12,0.6180134142446503,0.5277388240895907,0.037709538175969864
2021-03-13,0.5413605876716704,0.5402115475750452,0.03908922430364151
2021-03-14,0.5138933248163526,0.5461634488022022,0.03955870941296817
2021-03-15,0.8907697221335037,0.5546333450187781,0.04064170213132473
2021-03-16,0.7141488342382626,0.5644990894484404,0.04301588479922646
2021-03-17,0.5748059693433746,0.046385270000003885
2021-03-18,0.4816352603002236,0.0004971414367387521,0.586929849879751,0.0502180334737235
2021-03-19,0.40242733950814435,0.6012620809699422,0.05381379521631385
2021-03-20,0.5822420951772597,0.6196960752124455,0.056342307876544616
2021-03-21,0.5927818588310444,0.6274884009057742,0.057369986071118095
2021-03-22,0.9220696263174705,0.6345120546015971,0.05964614166017771
2021-03-23,0.6949856275950176,0.641433887445743,0.06460175113709823
2021-03-24,0.6129032258064516,0.6485488278890145,0.07229904775358964
2021-03-25,0.6595336953050144,0.6560138023232993,0.08041898686737191
2021-03-26,0.5528585116576173,0.6644618892386582,0.08855516381200305
2021-03-27,0.5091025231555414,0.6753999154889578,0.09695817067704024
2021-03-28,0.46151389332481635,0.6809472859396574,0.10025240296069311
2021-03-29,0.6368572341105079,0.6855452900974524,0.10494916023410293
2021-03-30,0.4899393165122964,0.0009942828734775043,0.6900052921963329,0.11347458622388784
2021-03-31,0.47013733631427657,0.0007457121551081282,0.694733305198276,0.12577580208089215
2021-04-01,0.4270201213669754,0.6978404953993359,0.14059727048417364
2021-04-02,0.3647396997764293,0.6996097220253694,0.1483431393946826
2021-04-03,0.3695305014372405,0.0014914243102162564,0.7015638512949736,0.15385955412963867
2021-04-04,0.31555413605876714,0.7022449155412313,0.1551709707684804
2021-04-05,0.3618652187799425,0.0012428535918468805,0.7026847011589764,0.1568343953404203
2021-04-06,0.38486106675183646,0.004225702212279393,0.70398418084569,0.1623714603602603
2021-04-07,0.3474928137975088,0.003231419338801889,0.7054079259076633,0.17582054965975213
2021-04-08,0.26700734589587993,0.0017399950285856326,0.7067551266745884,0.1901418222736232
2021-04-09,0.272436921111466,0.7083082718259427,0.20461763667761695
2021-04-10,0.20568508463749602,0.7102509141577699,0.22047444329567675
2021-04-11,0.002982848620432513,0.7111627493959102,0.22601694445888787
2021-04-12,0.3024592781858831,0.7117576086736438,0.2319028404449208
2021-04-13,0.312360268284893,0.002734277902063137,0.7126862243692739,0.24010905185999587
2021-04-14,0.3043755988502076,0.00571712652249565,0.7147450065236014,0.2503608416660763
2021-04-15,0.28393484509741296,0.007208550832711907,0.7172177154518766,0.2632542441542132
2021-04-16,0.25135739380389654,0.008451404424558787,0.71991113768458,0.2789361057946923
2021-04-17,0.21909932928776749,0.011185682326621925,0.7229351931585493,0.2957060433005763
2021-04-18,0.18428617055253912,0.7247768880694521,0.3024863260696927
2021-04-19,0.3557968700095816,0.017399950285856326,0.7269435786231264,0.3095352390404614
2021-04-20,0.25934206323858194,0.013174248073576932,0.7291318138020322,0.3190416771109454
2021-04-21,0.3359948898115618,0.015659955257270694,0.7315792991088756,0.3309818130293555
2021-04-22,0.3417438518045353,0.019388516032811335,0.734294715915986,0.34329736053706555
2021-04-23,0.23826253593101246,0.7371483729903979,0.35715112482101763
2021-04-24,0.15937400191632067,0.014168530947054437,0.7404653189100802,0.3744793613110453
2021-04-25,0.1526668795911849,0.02212279393487447,0.7420892648875941,0.38236198099700774
2021-04-26,0.33567550303417437,0.025105642555306985,0.7437305206745691,0.39070024833644307
2021-04-27,0.2551900351325455,0.745941465053043,0.40154842542989744
2021-04-28,0.25998083679335676,0.03231419338801889,0.7486244325843135,0.415239987903522
2021-04-29,0.2618971574576813,0.02609992542878449,0.7515495257072506,0.4292295851724883
2021-04-30,0.21271159374001916,0.03529704200845141,0.7545810715116135,0.4418150689682451
2021-05-01,0.14595975726604918,0.03156848123291076,0.7578259196928064,0.4535969919347753
2021-05-02,0.10507824976045992,0.03181705195128014,0.7596885769417722,0.4583241774912107
2021-05-03,0.17087192590226766,0.036539895600298286,0.7612201510002966,0.46162003355794845
2021-05-04,0.2510380070265091,0.05070842654735272,0.7637852053799629,0.4688939816845034
2021-05-05,0.22452890450335355,0.06587124036788466,0.7666046398494312,0.48124641730794626
2021-05-06,0.2235707441711913,0.06388267462092966,0.7692614309257214,0.49615786447390997
2021-05-07,0.20121366975407218,0.06984837186179468,0.7721223930573372,0.5108081296606325
2021-05-08,0.13765570105397637,0.055182699478001494,0.7758037184064547,0.5250891606937041
2021-05-09,0.12871287128712872,0.061894108873974646,0.7777605473711124,0.5305886316051657
2021-05-10,0.11981108625403927,0.779709541926595,0.53693402290459
2021-05-11,0.19482593420632385,0.09942828734775043,0.7823364775517048,0.5477584080458875
2021-05-12,0.17725966145001598,0.10937111608252548,0.7863224184926993,0.5620122936617357
2021-05-13,0.0686055182699478,0.7910200202238921,0.575307370665589
2021-05-14,0.14116895560523796,0.08376833209047974,0.7960093213588624,0.5873053626813064
2021-05-15,0.10284254231874801,0.0842654735272185,0.8016227814031771,0.5998483457197978
2021-05-16,0.07952730756946662,0.07680835197613721,0.8044619077659771,0.605310857916262
2021-05-17,0.18811881188118812,0.18568232662192394,0.8066140146728427,0.6124156855045625
2021-05-18,0.14500159693388695,0.20059656972408652,0.8107374018531819,0.62181396550174
2021-05-19,0.17885659533695306,0.20730797912005966,0.8170590289646842,0.6326657784573192
2021-05-20,0.15298626636857235,0.19786229182202336,0.8233315851484482,0.6456308038993527
2021-05-21,0.09517725966145002,0.16281382053194135,0.8284054502608316,0.6586966450911784
2021-05-22,0.08272117534334078,0.17176236639323889,0.8332911836826196,0.6771013791672231
2021-05-23,0.06802938358351965,0.16803380561769823,0.8360274834249972,0.6848652155905627
2021-05-24,0.10092622165442351,0.29778772060651254,0.8383513650465453,0.6939854050846367
2021-05-25,0.09453848610667519,0.3161819537658464,0.8425278991648016,0.7053534398626816
2021-05-26,0.10986905142127117,0.32861048968431517,0.8480174908478353,0.718261138701892
2021-05-27,0.1047588629830725,0.2923191648023863,0.8543014280989819,0.7308443633211829
2021-05-28,0.10795273075694667,0.3755903554561273,0.858992015916555,0.7439463160368313
2021-05-29,0.4021874223216505,0.8631968703652754,0.7573141455828141
2021-05-30,0.3731046482724335,0.8657854809076639,0.7634324190481475
2021-05-31,0.06930693069306931,0.4335073328361919,0.8676171445987109,0.7686419034804273
2021-06-01,0.08559565633982753,0.5824011931394482,0.8695091279468858,0.7781746750765922
2021-06-02,0.08623442989460237,0.6211782252050708,0.8729536212240496,0.7881017435653271
2021-06-03,0.060044714148834236,0.6132239622172508,0.877128037934421,0.7989947864914096
2021-06-04,0.09421909932928776,0.6619438230176485,0.8810232744151355,0.8103056358651606
2021-06-05,0.06739061002874482,0.6273924931643052,0.8857536165657524,0.8252986251816916
2021-06-06,0.05557329926541041,0.6189410887397464,0.888354190462692,0.8329703648644121
2021-06-07,0.07569466624081762,0.8883917474521501,0.8905588884879048,0.841233867582678
2021-06-08,0.05365697860108592,0.7874720357941835,0.8933270287513553,0.8499272845224844
2021-06-09,0.044714148834238264,0.6967437235893612,0.8972709656134282,0.8588417830592617
2021-06-10,0.0297029702970297,0.5575441213025105,0.9018691285768148,0.8676180482360297
2021-06-11,0.01916320664324497,0.4337559035545613,0.9065201208669356,0.8763553073191281
2021-06-12,0.016927499201533056,0.3348247576435496,0.9125850916751594,0.8861791243834434
2021-06-13,0.008942829766847652,0.29132488192890876,0.9158080511523397,0.8915240888044145
2021-06-14,0.009262216544235069,0.3338304747700721,0.9186771123691161,0.8972807881363019
2021-06-15,0.022037687639731716,0.6952522992791449,0.9226334360673172,0.9033125422029166
2021-06-16,0.015011178537208559,0.623912503107134,0.9271279783823242,0.9095643541771661
2021-06-17,0.009581603321622485,0.49540144171016653,0.9327092008915875,0.9156017208853133
2021-06-18,0.6850608998260005,0.9378672859026027,0.9213436296712754
2021-06-19,0.3867760377827492,0.9445483430026088,0.9283495476899267
2021-06-20,0.46109868257519265,0.9481971661406893,0.9311337708863953
2021-06-21,0.596818294804872,0.9513817475999512,0.9343933036315167
2021-06-22,0.009900990099009901,0.7991548595575442,0.9555178656951999,0.9386170811320041
2021-06-23,0.686055182699478,0.960054809103107,0.942789144671201
2021-06-24,0.00830405621207282,0.7119065374098931,0.9651041916868184,0.9474591801229747
2021-06-25,0.00670712232513574,0.4742729306487696,0.9696391764924318,0.9520469674315356
2021-06-26,0.7979120059656972,0.9751310179213434,0.9582717398874584
2021-06-27,0.006068348770360907,0.5846383296047726,0.9781309084778232,0.9617049115237661
2021-06-28,0.005110188438198659,0.540641312453393,0.9806936072412173,0.9652578901120329
2021-06-29,0.983542420745092,0.9691595937675958
2021-06-30,0.4926671638081034,0.9864800859773472,0.9738878383129995
2021-07-01,0.30002485707183696,0.9894941366990591,0.9789364976206106
2021-07-02,0.9920933607484719,0.9837332587522702
2021-07-03,0.995212990720857,0.9896178133522765
2021-07-04,0.002235707441711913,0.9967851925431775,0.9926236476402002
2021-07-05,0.9982705012394115,0.9962877847705173
2021-07-06,1.0

此外,当我删除堆叠的代码行时,我会显示两条线图:

Two line plots displayed when stacked bar line of code removed

这很奇怪。为什么我不能同时显示所有 3 个图?

解决方法

我将您的 CSV 数据保存到文件中(感谢您将其添加到您的问题中)并发现您的代码似乎按预期运行而无需修改:

Example Plot

您可能遇到了某种环境问题。请使用您的操作系统、pandas 和 matplotlib 版本更新您的问题。如果您在 IDE(如 Spyder 或 PyCharm)中运行代码,请一并列出。