Adding vertical average lines on top of a layered histogram in Altair - altair

I am trying to add vertical lines indicating the average of datasets in a layered histogram in Altair (based on their example). My attempt below is failing:
base = alt.Chart(outcomes)
bar = base.transform_fold(
['Push','Dealer Win','Player Win','Ace High Push'],
as_=['Outcome','Outcomes out of 1000']
).mark_bar(
opacity=0.3,
binSpacing=0
).encode(
alt.X('Outcomes out of 1000:Q', bin=alt.Bin(maxbins=100)),
alt.Y('count()', stack=None),
alt.Color('Outcome:N')
)
rule = base.transform_fold(
['Push','Dealer Win','Player Win','Ace High Push'],
as_=['Count','Outcome']
).mark_rule(
color='red'
).encode(
alt.X('mean(Outcome):Q'),
size=alt.value(2)
)
bar + rule
which results in:
When I do just bar though the layered histogram renders just fine:
Basically what I'm looking for is:
Thanks🙏
Update (less than an hour after original post):
Thanks #debbes for the speedy guidance! I was able to use your example to get this working via:
base = alt.Chart(outcomes).transform_fold(
['Push','Dealer Win','Player Win','Ace High Push'],
as_=['Outcome','Outcomes out of 1000']
).transform_bin(
field='Outcomes out of 1000',
as_='bin_meas',
bin=alt.Bin(maxbins=100)
).encode(
color='Outcome:N'
)
hist = base.mark_bar(
opacity=0.3,
binSpacing=0
).encode(
alt.X('bin_meas:Q'),
alt.Y('count()', stack=None)
)
rule = base.mark_rule(
size=2
).encode(
alt.X('mean(Outcomes out of 1000):Q')
)
hist + rule
which results in:

In this case you have to use the transform_bin instead of doing the binning in the X encoding:
base = alt.Chart(source).transform_fold(
['Trial A', 'Trial B', 'Trial C'],
as_=['Experiment', 'Measurement']
).transform_bin(
field='Measurement',
as_='bin_meas',
bin=alt.Bin(maxbins=100)
).encode(
color='Experiment:N'
)
hist = base.mark_bar(opacity=0.3,binSpacing=0).encode(
alt.X('bin_meas:Q'),
alt.Y('count()', stack=None),
)
rule = base.mark_rule(size=2).encode(alt.X('mean(Measurement):Q'),)
hist + rule

Related

How to avoid overlapping xticklabels in seaborn plot when spacing is narrow

I have the following dataframe:
,ENC,EPM,CPFNN,vMLP
cg19493601,0,0,0,2
cg17435445,0,0,0,2
cg02319392,0,0,0,2
cg04672495,0,0,0,2
cg09089913,0,0,0,2
cg21308111,0,0,0,2
cg03569073,0,0,0,2
cg26750487,0,0,0,1
cg05542262,0,0,0,2
cg19191454,0,0,0,2
cg20160885,0,0,0,2
cg02122467,0,0,0,2
cg27021986,0,0,0,2
cg22671421,0,0,0,2
cg06396762,0,0,0,2
cg03406626,0,0,0,2
cg02376827,0,0,0,2
cg04157865,0,0,0,2
cg14582226,0,0,0,2
cg19572264,0,0,0,2
cg10979436,0,0,0,1
cg15594550,0,0,0,1
cg06623057,0,0,1,1
cg14231987,0,0,0,2
cg14029283,0,0,0,2
cg24473385,0,0,0,1
cg19814830,0,0,0,2
cg14283099,0,0,0,2
cg16092645,0,0,0,2
cg02731774,0,0,0,2
cg19615721,0,0,0,2
cg18220632,0,0,0,1
cg25123102,0,0,0,2
cg04657715,0,0,0,1
cg21115608,0,0,0,2
cg13545874,0,0,0,2
cg11969637,0,0,0,1
cg03400437,0,0,0,2
cg25604067,0,0,0,1
cg20067598,0,0,0,2
cg17578235,0,0,1,1
cg05190577,0,0,0,2
cg04937422,0,0,0,2
cg27390496,0,0,0,2
cg18283673,0,0,0,1
cg01105403,0,0,0,1
cg06315607,0,0,0,2
cg27513574,0,0,0,2
cg10593416,0,0,0,1
cg19523338,0,0,0,1
cg10242862,0,0,0,2
cg01167177,0,0,0,1
cg18599069,0,0,0,2
cg20331814,0,0,0,2
cg10322510,0,0,0,2
cg09120267,0,0,0,1
cg05490132,0,0,0,1
cg02289168,0,0,0,1
cg09241267,0,0,0,1
cg03665605,0,0,0,1
cg20018782,0,0,0,1
cg13018197,0,0,0,2
cg24275159,0,0,0,2
cg14210236,0,0,0,2
cg03417342,0,0,0,1
cg25483123,0,0,0,2
cg03672854,0,0,0,2
cg26674929,0,0,0,2
cg16717099,0,0,0,2
cg14566393,0,0,0,1
cg18685561,0,0,0,1
cg18725681,0,0,0,2
cg13062821,0,0,0,1
cg15962547,0,0,0,2
cg19563510,0,0,0,1
cg25697726,0,0,0,1
cg10068989,0,0,0,2
cg04907885,0,0,0,2
cg16494530,0,0,0,1
cg09289712,0,0,0,2
cg18994446,0,0,0,1
cg10445447,0,0,0,2
cg11762629,0,0,0,2
cg07065737,0,0,0,1
cg14688108,0,1,1,1
cg14989522,0,0,0,1
cg18751682,0,0,0,2
cg17291435,0,0,0,2
cg20792512,0,0,0,2
cg21522303,0,0,0,2
cg09594069,0,0,0,2
cg03523550,0,0,0,2
cg08207707,0,0,0,2
cg06622408,0,0,0,2
cg07359633,0,0,0,2
cg19733833,0,0,1,2
cg10172801,0,0,0,1
cg14911690,0,0,0,2
cg01914744,0,0,0,2
cg20430572,0,0,0,1
cg05904213,0,0,0,2
cg19182423,0,0,0,1
cg15911859,0,0,0,2
cg25767192,0,0,0,2
cg03963391,0,0,0,2
cg25612710,0,0,0,2
cg22636108,0,0,0,2
cg21285525,0,0,1,1
cg11332928,0,0,0,2
cg11480264,0,0,0,1
cg09176740,0,0,0,1
cg14583871,0,0,0,2
cg12845923,0,0,0,1
cg06534313,0,0,0,1
cg04930858,0,0,0,1
cg04281268,0,0,0,2
cg17035899,0,0,1,1
cg18686155,0,0,0,2
cg04651042,0,0,0,2
cg18767088,0,0,1,2
cg10025443,0,0,0,2
cg01475538,0,0,1,1
cg24311272,0,0,0,2
cg18500674,0,0,0,2
cg21748418,0,0,0,1
cg11915997,0,0,0,2
cg03727342,0,0,0,2
cg09073441,0,0,0,2
cg21153962,0,0,0,2
cg02797548,0,0,0,2
cg27388777,0,0,0,2
cg17868287,0,0,0,2
cg01531388,0,0,0,2
cg07768201,0,0,0,2
cg26386968,0,1,1,1
cg14731657,0,0,0,2
cg00155063,0,0,1,2
cg09817427,0,0,0,2
cg22691746,0,0,0,2
cg09571376,0,0,0,1
cg21383280,0,0,0,1
cg21019315,0,0,0,2
cg07824824,0,0,0,1
cg03782778,0,0,0,2
cg20513721,0,0,0,2
cg04757012,0,0,0,2
cg09967192,0,0,0,2
cg26925114,0,0,0,1
cg19412667,0,0,0,2
cg13939664,0,0,0,2
cg15766595,0,0,0,2
cg12041266,0,0,0,2
cg07785447,0,0,0,2
cg13915354,0,0,0,1
cg15512534,0,0,0,1
cg24144083,0,0,1,1
cg17603502,0,0,0,2
cg11999631,0,0,0,2
cg26974111,0,0,1,1
cg09818930,0,0,0,2
cg19518388,0,0,0,2
cg07924892,0,0,0,2
cg03666316,0,0,0,2
cg26006440,0,0,0,2
cg24679567,0,0,0,1
cg15179515,0,0,0,2
cg22542751,0,0,0,2
cg18135796,0,0,0,2
cg22766230,0,0,0,1
cg18043157,0,0,0,1
cg10367023,0,0,0,2
cg07747661,0,0,0,1
cg00915818,0,0,0,2
cg21216268,0,0,0,2
cg09268672,0,0,0,1
cg00641009,0,0,0,2
cg21175685,0,0,0,2
cg09478268,0,0,0,1
cg07452625,0,0,0,2
cg08881785,0,0,0,1
cg18147605,0,0,0,2
cg15202378,0,0,0,2
cg07693657,0,0,0,1
cg02493205,0,0,0,1
cg08376310,0,0,0,2
cg18049142,0,0,0,2
cg16132219,0,0,0,2
cg09112760,0,0,0,2
cg20152891,0,0,0,2
cg12956472,0,0,0,1
cg10151901,0,0,0,2
cg26785154,0,0,0,1
cg01196079,0,0,0,1
cg10227919,0,0,0,1
cg17799601,0,0,0,1
cg22960907,0,0,0,2
cg20932768,0,0,0,2
cg10278931,0,0,0,1
cg13539424,0,0,0,1
cg10188732,0,0,0,2
cg18424968,0,0,0,2
cg13787272,0,0,0,2
cg08642716,0,0,0,2
cg01972418,0,0,0,2
cg21955796,0,0,0,2
cg09796320,0,0,0,1
cg00752480,0,0,0,1
cg20225546,0,0,0,2
cg05529157,0,0,0,1
cg21025501,0,0,0,2
cg24842597,0,0,0,1
cg16700779,0,0,1,2
cg23340104,0,0,0,2
cg03516318,0,0,0,2
cg09560650,0,0,0,1
cg06819687,0,0,0,2
cg00106074,0,0,0,2
cg21965516,0,0,0,1
cg01328119,0,0,0,2
cg13948585,0,0,0,2
cg05494465,0,0,0,2
cg22532475,0,0,0,1
cg00920348,0,0,0,2
cg20938572,0,0,0,2
cg21453831,0,0,0,2
cg04241652,0,0,0,2
cg02757572,0,0,0,2
cg02600349,0,0,0,2
cg02626667,0,0,0,1
cg00611495,0,0,0,1
cg00290373,0,0,0,2
cg07556829,0,0,0,2
cg04497611,0,0,0,2
cg18402615,0,0,0,2
cg18360825,0,0,0,1
cg03702919,0,0,0,2
cg26060489,0,0,0,2
cg13178766,0,0,0,2
cg00401972,0,0,0,1
cg11791710,0,0,0,2
cg19766441,0,0,0,2
cg19961480,0,0,0,2
cg01965950,0,0,0,1
cg19996355,0,0,0,2
cg23292266,0,0,0,2
cg25801502,0,0,0,1
cg22854549,0,0,0,2
cg02105326,0,0,0,2
cg06928993,0,0,0,2
cg08152564,0,0,0,2
cg03867759,0,0,0,2
cg18145196,0,0,0,1
cg08051076,0,0,0,1
cg20946369,0,0,0,1
cg22679120,0,0,0,2
cg21548029,0,0,0,2
cg16715692,0,0,0,1
cg22591433,0,0,0,2
cg13242468,0,0,0,2
cg23169614,0,0,0,2
cg12368612,0,0,0,2
cg19722639,0,0,0,2
cg05027085,0,0,0,2
cg02980621,0,0,0,2
cg10985993,0,0,0,2
cg18997875,0,0,0,2
cg02716556,0,0,0,2
cg01054478,0,0,0,1
cg26381783,0,0,0,2
cg25990363,0,0,0,2
cg17759806,0,0,0,2
cg18589102,0,0,0,2
cg16133088,0,0,0,2
cg04725507,0,0,0,2
cg26748945,0,0,0,2
cg26824709,0,0,0,1
cg25857710,0,0,0,2
cg01616215,0,0,0,2
cg02254554,0,0,0,2
cg06131936,0,0,1,1
cg00913799,0,0,0,2
cg23149687,0,0,0,2
cg25153196,0,0,0,1
cg24695614,0,0,1,1
cg08573355,0,0,0,2
cg02413370,0,0,0,1
cg05204798,0,0,1,1
cg16977596,0,0,0,2
cg09879895,0,0,0,2
cg08541521,0,0,0,2
cg04843615,0,0,0,2
cg00799631,0,0,0,2
cg02540094,0,0,0,2
cg11908557,0,0,0,2
cg06842071,0,0,0,1
cg01323274,0,0,0,2
cg05195017,0,0,0,1
cg05601917,0,0,1,1
cg27079740,0,0,0,1
cg13785536,0,0,0,2
cg22775138,0,0,0,2
cg26230417,0,0,0,2
cg14102055,0,0,0,1
cg07227926,0,0,0,2
cg12804441,0,0,0,2
cg14170181,0,0,0,2
cg06005098,0,0,0,2
cg18569885,0,0,0,1
cg27295716,0,0,0,2
cg06622725,0,0,0,2
cg27603366,0,0,0,2
cg20158796,0,0,0,2
cg14920696,0,0,0,2
cg25722423,0,0,0,2
cg22736354,0,0,0,2
cg03505427,0,0,0,1
cg01217204,0,0,0,2
cg09967647,0,0,0,2
cg22159421,0,0,0,2
cg19995828,0,0,0,2
cg23472930,0,0,0,2
cg00702008,0,0,0,1
cg25534294,0,0,0,1
cg27201301,0,0,0,2
cg25735887,0,0,0,2
cg06208926,0,0,0,1
cg05945782,0,0,0,2
cg01112249,0,0,0,1
cg12781568,0,0,0,2
cg04787317,0,0,0,2
cg07365960,0,0,0,2
cg15435996,0,0,0,1
cg20077393,0,0,0,2
cg15394350,0,0,0,2
cg07793849,0,0,0,2
cg06143732,0,0,0,2
cg17922215,0,0,0,2
cg21619814,0,0,0,1
cg03840496,0,0,0,1
cg00716309,0,0,0,1
cg07023324,0,0,0,1
cg15788149,0,0,1,1
cg02745321,0,0,0,1
cg17273683,0,0,0,1
cg10709593,0,0,0,2
cg25523538,0,0,0,2
cg08210342,0,0,0,1
cg07332683,0,0,0,1
cg14566475,0,0,0,1
cg26116495,0,0,0,2
cg12169365,0,0,0,2
cg02879662,0,0,0,2
cg03867475,0,0,0,2
cg03660500,0,0,0,2
cg22855900,0,0,0,2
cg00076998,0,0,0,2
cg21216010,0,0,0,2
cg22337605,0,0,0,2
cg24663541,0,0,0,2
cg08898442,0,0,0,1
cg17830959,0,0,0,2
cg25617230,0,0,1,1
cg01073605,0,0,0,2
cg07645736,0,0,0,2
cg17906269,0,0,0,2
cg01689641,0,0,0,2
cg21727214,0,0,0,1
ch.11.1543446R,0,0,0,2
cg12897947,0,0,0,2
cg02916525,0,0,0,2
cg20449382,0,0,0,2
cg27050747,0,0,0,1
cg08596000,0,0,0,2
cg15442907,0,0,0,1
cg02422902,0,0,0,2
cg20536512,0,0,0,2
cg15475080,0,0,0,2
cg22484737,0,0,0,2
cg20283971,0,0,0,2
cg08369436,0,0,1,1
cg03598440,0,0,0,1
cg20005056,0,0,0,1
cg09790502,0,0,0,2
cg00009916,0,0,0,1
cg03179043,0,0,0,1
cg04227079,0,0,0,1
cg26931862,0,0,0,1
cg07527324,0,0,0,2
cg26144458,0,0,0,2
cg02245998,0,0,0,1
cg20068496,0,0,0,2
cg04768927,0,0,0,2
cg08097877,0,0,0,1
cg03957204,0,0,0,1
cg07967210,0,0,0,2
cg11227822,0,0,0,2
cg12738979,0,0,0,2
cg23501567,0,0,0,1
cg14539442,0,1,1,1
cg04471454,0,0,0,2
cg04012618,0,0,0,2
cg03738352,0,0,0,2
cg06510397,0,0,0,2
cg03809954,0,0,0,1
cg02028389,0,0,0,2
cg09308829,0,0,0,2
cg03930532,0,0,0,2
cg09383860,0,0,0,1
cg08798933,0,0,0,2
cg04969688,0,0,0,1
cg07311521,0,0,0,2
cg21586215,0,0,0,2
cg18356159,0,0,0,2
cg04497154,0,0,0,1
cg08146865,0,0,0,2
cg18589016,0,0,0,2
cg05397886,0,0,0,1
cg13679048,0,0,0,2
cg21946299,0,0,0,2
cg19788741,0,0,0,2
cg04323979,0,0,0,1
cg13580857,0,0,0,2
cg08016802,0,0,0,2
cg18319687,0,0,0,1
cg00257542,0,0,0,1
cg26512993,0,0,0,1
cg02117859,0,0,0,1
cg21622555,0,0,0,2
cg00540941,0,0,0,1
cg24332767,0,0,0,2
cg02052774,0,0,0,2
cg15627380,0,0,0,1
cg22562590,0,0,0,1
cg00871979,0,0,0,1
cg04012364,0,0,0,2
cg15952045,0,0,0,1
cg13576200,0,0,0,1
cg22264014,0,0,0,2
cg26673648,0,0,0,1
cg01381130,0,0,0,1
cg22294804,0,0,0,2
cg01727686,0,0,0,1
cg21932368,0,0,0,2
cg06536629,0,0,0,2
cg10915772,0,0,0,1
cg18449721,0,0,0,2
cg19697530,0,0,0,2
cg19253643,0,0,0,1
cg26635603,0,0,0,1
cg00517407,0,0,0,2
cg21291641,0,0,0,2
cg13914598,0,0,0,2
cg05516842,0,0,0,1
cg03187614,0,0,0,1
cg05272099,0,0,0,2
cg10661615,0,0,0,2
cg05601623,0,0,0,2
cg13118545,0,0,0,2
cg12690313,0,0,0,2
cg06369090,0,0,0,2
cg08743392,0,0,0,2
cg02276361,0,0,0,1
cg08915922,0,0,0,2
cg04169908,0,0,0,1
cg12440258,0,0,0,2
cg26986937,0,0,0,2
cg22606205,0,0,0,2
cg27168632,0,0,0,2
cg25609143,0,0,0,2
cg01273565,0,0,0,2
cg08506672,0,0,0,1
cg22675486,0,0,0,2
cg05063395,0,0,0,2
cg01405761,0,0,0,2
cg10373196,0,0,0,2
cg00761129,0,0,0,2
cg14946515,0,0,0,2
cg25841943,0,0,0,2
cg25004270,0,1,1,1
cg19190269,0,0,0,2
cg03064832,0,0,0,2
cg17199468,0,0,0,2
cg22387756,0,0,0,1
cg04257169,0,0,0,2
cg09763325,0,0,0,2
cg12034118,0,0,0,2
cg13159559,0,0,0,2
cg17353057,0,0,0,2
cg00140191,0,0,1,1
cg06390079,0,0,0,2
cg01201782,0,0,0,2
cg09457801,0,0,0,2
cg06516800,0,0,0,1
cg24938727,0,0,0,2
cg05198733,0,0,0,1
cg01897756,0,0,0,2
cg01212071,0,0,0,2
cg25284762,0,0,0,1
cg21024422,0,0,0,1
cg06553513,0,0,0,2
cg10976318,0,0,0,2
cg13742526,0,0,0,2
cg08005992,0,0,0,2
cg11807492,0,0,0,2
cg25190513,0,0,0,1
cg14416559,0,0,0,2
cg02086801,0,0,0,2
cg02525995,0,0,0,1
cg24018756,0,0,0,1
cg27056129,0,0,0,2
cg18753594,0,0,0,2
cg01159380,0,0,1,1
cg23620822,0,0,0,2
cg01163842,0,0,0,2
cg22947959,0,0,0,1
cg18396811,0,0,0,2
cg26470101,0,0,0,1
cg00570697,0,0,0,2
cg23727043,0,0,1,2
cg07330196,0,0,0,1
cg05784562,0,0,0,1
cg08715988,0,0,0,1
cg05979118,0,0,0,2
cg12148940,0,0,0,1
cg08579962,0,0,0,1
cg04845171,0,0,0,1
cg03149432,0,0,0,1
cg20440575,0,0,0,2
cg13657659,0,0,0,2
cg04849201,0,0,0,1
cg19147912,0,0,0,1
cg12728517,0,0,0,2
cg03447530,0,0,0,1
cg21184800,0,0,0,2
cg11362449,0,0,0,1
cg12311636,0,0,0,1
cg06437740,0,0,0,1
cg03999216,0,0,0,2
cg17477493,0,0,0,1
cg22259778,0,0,0,1
cg10120572,0,0,0,1
cg07797660,0,0,0,2
cg08677954,0,0,0,2
cg06635552,0,0,0,2
cg09899094,0,0,0,2
cg13845147,0,0,0,2
cg23037132,0,0,0,2
cg15262505,0,0,0,2
cg00056489,0,0,0,2
cg09759737,0,0,0,1
cg12188268,0,0,0,2
cg24011500,0,0,0,1
cg15002713,0,0,0,1
cg13817545,0,0,0,2
cg03553786,0,0,0,2
cg06218627,0,0,0,2
cg17298884,0,0,0,1
cg18231614,0,0,0,2
cg14835981,0,0,0,1
cg08418980,0,0,0,2
cg14007549,0,0,0,2
cg08317133,0,1,1,1
cg26741350,0,0,1,1
cg01682784,0,0,0,2
cg17279652,0,0,0,1
cg05128414,0,0,0,1
cg04132146,0,0,0,2
cg23970331,0,0,0,1
cg15521264,0,0,0,2
cg07291005,0,0,0,2
cg27194152,0,0,0,1
cg10403934,0,0,0,1
cg10922264,0,0,0,1
cg22583444,0,0,0,1
cg18507707,0,0,0,2
cg02761568,0,0,0,2
cg05495029,0,0,0,2
cg08645889,0,0,1,2
cg00945293,0,0,0,2
cg25501930,0,0,0,1
cg10090836,0,0,0,1
cg15189070,0,0,0,1
cg10497884,0,0,0,2
cg26345216,0,0,0,1
cg09566131,0,0,0,2
cg09561280,0,0,0,2
cg11296715,0,0,0,2
cg26659805,0,0,0,2
cg01449168,0,0,0,2
cg00896540,0,0,0,2
cg21963854,0,0,0,2
cg16240137,0,0,0,1
cg23050300,0,0,0,2
cg08471800,0,0,0,2
cg07905273,0,0,0,2
cg24277586,0,0,0,1
cg03871549,0,0,0,2
cg00123181,0,0,0,2
cg06549530,0,0,0,2
cg02535735,0,0,0,1
cg04327529,0,0,0,2
cg17639046,0,0,0,2
cg01082601,0,0,0,2
cg19042136,0,0,0,2
cg10846615,0,0,1,1
cg17179051,0,0,0,1
cg05184917,0,0,0,2
cg27271756,0,0,0,2
cg07823913,0,0,0,1
cg01040169,0,0,0,1
cg09441152,0,0,0,2
cg23623107,0,0,0,1
cg16622863,0,0,0,2
cg22358236,0,0,0,2
cg08153404,0,0,0,2
cg19317333,0,0,0,2
cg20548032,0,0,0,1
cg13824156,0,0,0,1
cg06237092,0,0,0,2
cg19090522,0,0,0,1
cg06679538,0,0,0,2
cg21834739,0,0,0,2
cg18626098,0,0,0,2
cg13717425,0,0,0,2
cg12876900,0,0,0,1
cg16050974,0,0,0,2
cg19499998,0,0,1,1
cg00877056,0,0,0,1
cg10607485,0,0,0,2
cg18275316,0,0,1,1
cg24040576,0,0,0,1
cg19238531,0,0,0,2
cg07202110,0,0,0,1
cg08276984,0,0,0,2
cg26281453,0,0,0,2
cg14354327,0,0,0,1
cg25397973,0,0,0,2
cg09449449,0,0,0,2
cg06023349,0,0,0,2
cg19968946,0,0,0,2
cg10962407,0,0,0,1
cg24044238,0,0,0,2
cg19987142,0,0,0,2
cg25575845,0,0,0,1
cg05121812,0,0,0,2
cg12671565,0,0,0,2
cg01802295,0,0,0,2
cg11372696,0,0,0,1
cg14371636,0,0,0,2
cg00500498,0,0,0,2
cg11278260,0,0,0,2
cg07468260,0,0,0,2
cg13536051,0,0,1,1
cg13353683,0,0,0,2
cg13873762,0,0,0,2
cg18537571,0,0,0,2
cg07429394,0,0,0,2
ch.X.703923F,0,0,0,1
cg16562486,0,0,0,2
cg26932226,0,0,0,2
cg02250708,0,0,0,1
cg22354618,0,0,0,2
cg19671246,0,0,0,2
cg11442717,0,0,0,1
cg04941630,0,0,0,2
cg19995539,0,0,0,1
cg24341220,0,0,0,2
cg05670459,0,0,0,2
cg17706896,0,0,0,2
cg19855618,0,0,0,2
cg10778240,0,0,0,2
cg20078119,0,0,0,2
cg26879788,0,0,0,1
cg20776947,0,0,0,2
cg26377880,0,0,0,2
cg07791065,0,0,0,2
cg23086176,0,0,0,1
cg04864083,0,0,0,2
cg23719318,0,0,0,2
cg27403098,0,0,0,2
cg03720043,0,0,0,2
cg16256065,0,0,0,1
cg16837557,0,0,0,2
cg17662493,0,0,1,1
cg11505338,0,0,0,1
cg04878644,0,0,0,2
cg18710784,0,0,0,1
cg17152214,0,0,0,1
cg10865856,0,0,0,1
cg03868159,0,0,0,2
cg15439078,0,0,1,1
cg24223558,0,0,0,1
cg14480858,0,0,0,2
cg09644356,0,0,0,1
cg04100684,0,0,0,2
cg04760708,0,0,0,2
cg27373972,0,0,0,1
cg25181749,0,0,0,1
cg10251973,0,0,0,2
cg20172500,0,0,0,1
cg25883405,0,0,0,2
cg06932776,0,0,0,2
cg11188679,0,0,0,2
cg23328050,0,0,0,2
cg16107322,0,0,0,2
cg04552470,0,0,0,2
cg08393356,0,0,0,1
cg01284869,0,0,0,2
cg07896108,0,0,0,1
cg22571393,0,0,1,1
cg18988170,0,0,0,2
cg16592453,0,0,0,1
cg06211255,0,0,0,2
cg22426938,0,0,0,2
cg03944089,0,0,0,2
cg09595479,0,0,0,2
cg26258845,0,0,0,1
cg09892203,0,0,0,1
cg00221327,0,0,0,1
cg27504292,0,0,0,2
cg19267760,0,0,0,1
cg26864395,0,0,0,2
cg12856183,0,0,0,2
cg07829465,0,0,0,1
cg15215830,0,0,0,2
cg14318942,0,0,0,2
cg11229715,0,0,0,2
cg11691189,0,0,0,1
cg12991830,0,0,0,2
cg22699052,0,0,1,1
cg09485472,0,0,0,1
cg14752227,0,0,1,1
cg04787343,0,0,0,2
cg11746846,0,0,0,1
cg17852021,0,0,0,2
cg15120477,0,0,0,1
cg24572400,0,0,0,2
cg00117869,0,0,0,1
cg01216607,0,0,0,2
cg17222164,0,0,0,2
cg01204964,0,0,1,1
cg07955004,1,1,1,1
cg04371440,0,0,0,2
cg15035364,0,0,0,2
cg19710662,0,0,0,1
cg16595365,0,0,0,2
cg03370106,0,0,0,1
cg18571419,0,0,0,2
cg20624137,0,0,0,1
cg15412736,0,0,0,2
cg00889769,0,0,0,2
cg14649140,0,0,0,2
cg25531618,0,0,0,1
cg07594031,0,0,0,2
cg05816239,0,0,0,2
cg00295604,0,0,0,2
cg04941721,0,0,0,2
cg11613164,0,0,0,2
cg02387679,0,0,0,2
cg22134372,0,0,0,2
cg27099166,0,0,0,2
cg09735674,0,0,0,1
cg23173517,0,0,0,2
cg20713333,0,0,0,1
cg01520402,0,0,0,1
cg00328593,0,0,0,2
cg17348479,0,0,0,1
cg26643142,0,0,0,2
cg14575053,0,0,0,2
cg05092885,0,0,0,1
cg08620751,0,0,0,1
cg21562321,0,0,0,1
cg22374901,0,0,0,2
cg27613976,0,0,0,2
cg06127885,0,0,1,1
cg14840664,0,0,0,1
cg25045242,0,0,0,1
cg12747844,0,0,0,1
cg14534464,0,0,0,2
cg21508023,0,0,0,2
cg13417559,0,0,0,2
cg14461650,0,0,0,1
cg03885264,0,0,0,2
cg02868338,0,0,0,2
cg08846467,0,0,0,2
cg27565938,0,0,0,1
cg08904363,0,0,0,2
cg12253071,0,0,0,1
cg06259664,0,1,1,1
cg18453904,0,0,0,2
cg19144392,0,0,0,1
cg16189596,0,0,0,2
And I want to create a seaborn heatmap like this:
plt.figure(figsize=(470, 60))
sns.set(font_scale = 14)
df=comparison.T
# create a Boolean mask of df
mask = df.ge(1).all()
# use the mask to update a list of labels
cols = [col if m else '' for (col, m) in zip(df.columns, mask)]
# plot with custom labels
ax = sns.heatmap(df, xticklabels=cols,cmap="crest_r")
ax.set_xticklabels(labels=cols, fontsize=200)
plt.show()
However, sometimes due to the narrow space the xtick labels overlap. Is there any way to add more spacing while still providing a readable image (not too small so that it cannot be read) or to put them one below the other?

Remove repeating values from X axis label in Altair

I am having trouble with Altair repeating X axis label values.
Data:
rule_abbreaviation flagged_claim bill_month
0 CONCIDPROC 1 Apr2022
1 CONTUSMAT1 1 Apr2022
2 COVID05 1 Jun2021
3 FILTROTUB2 1 Sep2021
4 MEPIARTRO1 1 Mar2022
#Code to generate Altair Bar Chart
bar = alt.Chart(Data).mark_bar().encode(
x=alt.X('flagged_claim:Q', axis=alt.Axis(title='Flagged Claims', format= ',.0f'), stack='zero'),
y=alt.Y('rule_abbreaviation:N', axis=alt.Axis(title='Component Abbreviation'), sort=alt.SortField(field=measure, order='descending')),
tooltip=[alt.Tooltip('max(ClaimRuleName):N', title='Claim Component'), alt.Tooltip('flagged_claim:Q', title='Flagged Claims', format= ',.0f')],
color=alt.Color('bill_month', legend=None)
).properties(width=485,
title = alt.TitleParams(text = 'Bottom Components',
font = 'Arial',
fontSize = 16,
color = '#000080',
)
).interactive()
X axis label generated by this chart contains repeated 0 and 1
Image of Visualization: https://i.stack.imgur.com/0XdWB.png
The reason this is happening is because you have format= ',.0f' which tells Altair to include 0 decimals in the axis labels. Remove it or change to 1f to see decimals in the labels. In general, a good way to troubleshoot problems like this is to remove part of your code at a time to identify which part is causing the unexpected behavior.
To reduce the number of ticks you can use alt.Axis(title='Flagged Claims', format='d', tickCount=1) or alt.Axis(title='Flagged Claims', format='d', values=[0, 1]). See also Changing Number of y-axis Ticks in Altair

Pan Tompkins Lowpass filter overflow

The Pan Tompkins algorithm1 for removing noise from an ECG/EKG is cited often. They use a low pass filter, followed by a high pass filter. The output of the high pass filter looks great. But (depending on starting conditions) the output of the low pass filter will continuously increase or decrease. Given enough time, your numbers will eventually get to a size that the programming language cannot handle and rollover. If I run this on an Arduino (which uses a variant of C), it rolls over on the order of 10 seconds. Not ideal. Is there a way to get rid of this bias? I've tried messing with initial conditions, but I'm fresh out of ideas. The advantage of this algorithm is that it's not very computationally intensive and will run comfortably on a modest microprocessor.
1 Pan, Jiapu; Tompkins, Willis J. (March 1985). "A Real-Time QRS Detection Algorithm". IEEE Transactions on Biomedical Engineering. BME-32 (3): 230–236.
Python code to illustrate problem. Uses numpy and matplotlib:
import numpy as np
import matplotlib.pyplot as plt
#low-pass filter
def lpf(x):
y = x.copy()
for n in range(len(x)):
if(n < 12):
continue
y[n,1] = 2*y[n-1,1] - y[n-2,1] + x[n,1] - 2*x[n-6,1] + x[n-12,1]
return y
#high-pass filter
def hpf(x):
y = x.copy()
for n in range(len(x)):
if(n < 32):
continue
y[n,1] = y[n-1,1] - x[n,1]/32 + x[n-16,1] - x[n-17,1] + x[n-32,1]/32
return y
ecg = np.loadtxt('ecg_data.csv', delimiter=',',skiprows=1)
plt.plot(ecg[:,0], ecg[:,1])
plt.title('Raw Data')
plt.grid(True)
plt.savefig('raw.png')
plt.show()
#Application of lpf
f1 = lpf(ecg)
plt.plot(f1[:,0], f1[:,1])
plt.title('After Pan-Tompkins LPF')
plt.xlabel('time')
plt.ylabel('mV')
plt.grid(True)
plt.savefig('lpf.png')
plt.show()
#Application of hpf
f2 = hpf(f1[16:,:])
print(f2[-300:-200,1])
plt.plot(f2[:-100,0], f2[:-100,1])
plt.title('After Pan-Tompkins LPF+HPF')
plt.xlabel('time')
plt.ylabel('mV')
plt.grid(True)
plt.savefig('hpf.png')
plt.show()
raw data in CSV format:
timestamp,ecg_measurement
96813044,2.2336266040
96816964,2.1798632144
96820892,2.1505377292
96824812,2.1603128910
96828732,2.1554253101
96832660,2.1163244247
96836580,2.0576734542
96840500,2.0381231307
96844420,2.0527858734
96848340,2.0674486160
96852252,2.0283479690
96856152,1.9648094177
96860056,1.9208210945
96863976,1.9159335136
96867912,1.9208210945
96871828,1.8768328666
96875756,1.7986314296
96879680,1.7448680400
96883584,1.7155425548
96887508,1.7057673931
96891436,1.6520038604
96895348,1.5591397285
96899280,1.4809384346
96903196,1.4467253684
96907112,1.4369501113
96911032,1.3978494453
96914956,1.3440860509
96918860,1.2952101230
96922788,1.3000977039
96926684,1.3343108892
96930604,1.3440860509
96934516,1.3489736318
96938444,1.3294233083
96942364,1.3782991170
96946284,1.4222873687
96950200,1.4516129493
96954120,1.4369501113
96958036,1.4320625305
96961960,1.4565005302
96965872,1.4907135963
96969780,1.5053763389
96973696,1.4613881111
96977628,1.4125122070
96981548,1.4076246261
96985476,1.4467253684
96989408,1.4809384346
96993324,1.4760508537
96997236,1.4711632728
97001160,1.4907135963
97005084,1.5444769859
97008996,1.5982404708
97012908,1.5835777282
97016828,1.5591397285
97020756,1.5786901473
97024676,1.6324535369
97028604,1.6911046504
97032516,1.6959922313
97036444,1.6764417648
97040364,1.6813293457
97044296,1.7155425548
97048216,1.7448680400
97052120,1.7253177165
97056048,1.6911046504
97059968,1.6911046504
97063880,1.7302052974
97067796,1.7741935253
97071724,1.7693059444
97075644,1.7350928783
97079564,1.7595307826
97083480,1.8719452857
97087396,2.0381231307
97091316,2.2482893466
97095244,2.4828934669
97099156,2.7468230724
97103088,2.9960899353
97106996,3.0987291336
97110912,2.9178886413
97114836,2.5171065330
97118756,2.0185728073
97122668,1.5053763389
97126584,1.1094819307
97130492,0.8015640258
97134396,0.5767350673
97138308,0.4545454502
97142212,0.4349951267
97146124,0.4692081928
97150020,0.4887585639
97153924,0.4594330310
97157828,0.4105571746
97161740,0.3861192512
97165660,0.3763440847
97169580,0.3714565038
97173492,0.3225806236
97177404,0.2639296054
97181316,0.2394916772
97185236,0.2297165155
97189148,0.2443792819
97193060,0.2248289346
97196972,0.1857282543
97200900,0.1808406734
97204812,0.2199413537
97208732,0.2492668628
97212652,0.2443792819
97216572,0.2199413537
97220484,0.2248289346
97224404,0.2834799575
97228316,0.3274682044
97232228,0.3665689229
97236132,0.3861192512
97240036,0.4398827075
97243936,0.5083088874
97247836,0.6109481811
97251748,0.7086998939
97255660,0.7771260738
97259568,0.8553275108
97263476,0.9775171279
97267392,1.1094819307
97271308,1.1974585056
97275228,1.2512218952
97279148,1.2952101230
97283056,1.3734115362
97286992,1.4760508537
97290900,1.5493645668
97294820,1.5738025665
97298740,1.5982404708
97302652,1.6471162796
97306584,1.7106549739
97310500,1.7546432018
97314420,1.7546432018
97318340,1.7644183635
97322272,1.8084066390
97326168,1.8621701240
97330072,1.8963831901
97333988,1.8817204475
97337912,1.8572825431
97341840,1.8670577049
97345748,1.8866080284
97349668,1.8768328666
97353580,1.8230694770
97357500,1.7595307826
97361424,1.7302052974
97365332,1.7350928783
97369252,1.6959922313
97373168,1.6226783752
97377092,1.5298142433
97381012,1.4613881111
97384940,1.4320625305
97388860,1.4076246261
97392780,1.3440860509
97396676,1.2658846378
97400604,1.2121212482
97404532,1.1974585056
97408444,1.1779080629
97412356,1.1192570924
97416264,1.0361680984
97420164,0.9628542900
97424068,0.9286412239
97427988,0.9042033195
97431892,0.8406646728
97435804,0.7575757503
97439708,0.6940371513
97443628,0.6793744087
97447540,0.6793744087
97451452,0.6549364089
97455356,0.6060606002
97459240,0.5767350673
97463140,0.6011730194
97467044,0.6451612472
97470964,0.6842619895
97474884,0.6891495704
97478796,0.7184750556
97482700,0.8064516067
97486612,0.8846529960
97490516,0.9335288047
97494428,0.9530791282
97498340,0.9481915473
97502256,0.9726295471
97506156,0.9921798706
97510060,0.9726295471
97513980,0.8846529960
97517884,0.7869012832
97521796,0.7086998939
97525692,0.6549364089
97529604,0.5913978099
97533516,0.4887585639
97537428,0.3567937374
97541348,0.2639296054
97545260,0.2003910064
97549148,0.1417399787
97553060,0.0928641223
97556980,0.0537634420
97560892,0.0342130994
97564804,0.0146627559
97568708,0.0244379281
97572628,0.0048875851
97576500,0.0000000000
97580324,0.0000000000
97584172,0.0097751703
97588060,0.0244379281
97591980,0.0195503416
97595900,0.0146627559
97599812,0.0488758563
97603724,0.1319648027
97607628,0.2248289346
97611548,0.3030303001
97615444,0.3665689229
97619364,0.4496578693
97623276,0.5718474864
97627176,0.7038123130
97631076,0.8064516067
97634988,0.8699902534
97638900,0.9384163856
97642816,1.0361680984
97646720,1.1485825777
97650644,1.2365591526
97654572,1.2658846378
97658492,1.2805473804
97662404,1.3294233083
97666324,1.3782991170
97670244,1.3831867027
97674148,1.3489736318
97678068,1.3049852848
97681988,1.2903225421
97685908,1.3000977039
97689812,1.3098728656
97693728,1.2463343143
97697648,1.1876833438
97701568,1.1681329011
97705488,1.1876833438
97709412,1.1827956390
97713328,1.1339198350
97717244,1.0752688646
97721144,1.0557184219
97725056,1.0703812837
97728972,1.0850440216
97732872,1.0752688646
97736788,1.0459432601
97740696,1.0508308410
97744600,1.0948191833
97748520,1.1290322542
97752444,1.1192570924
97756364,1.0850440216
97760272,1.0801564407
97764168,1.1094819307
97768072,1.1339198350
97771996,1.1143695116
97775920,1.0557184219
97779840,1.0166177749
97783756,0.9970674514
97787668,0.9921798706
97791580,0.9530791282
97795500,0.8846529960
97799412,0.8504399299
97803316,0.8455523490
97807212,0.8699902534
97811124,0.8699902534
97815028,0.8308895111
97818940,0.8064516067
97822844,0.8211143493
97826756,0.8651026725
97830668,0.9042033195
97834572,0.8895405769
97838476,0.8993157386
97842396,0.9530791282
97846304,1.0410556793
97850204,1.0850440216
97854112,1.0899316024
97858020,1.1192570924
97861940,1.2267839908
97865860,1.4320625305
97869772,1.6911046504
97873688,1.9892473220
97877604,2.3020527362
97881524,2.6197457313
97885452,2.8299119949
97889372,2.7761485576
97893292,2.4535679817
97897216,1.9745845794
97901136,1.4956011772
97905052,1.0899316024
97908964,0.8260019302
97912864,0.6695992469
97916772,0.6353860855
97920684,0.7038123130
97924588,0.8553275108
97928496,1.0215053558
97932408,1.1388074159
97936324,1.2023460865
97940228,1.2463343143
97944148,1.3098728656
97948064,1.3734115362
97951988,1.3929618644
97955912,1.3734115362
97959836,1.3636363744
97963764,1.3782991170
97967684,1.4173997879
97971612,1.4173997879
97975540,1.3880742835
97979460,1.3831867027
97983372,1.4027370452
97987292,1.4467253684
97991216,1.4565005302
97995124,1.4320625305
97999036,1.4173997879
98002964,1.4662756919
98006884,1.5249266624
98010812,1.5689149856
98014740,1.5689149856
98018668,1.5689149856
98022592,1.6129032135
98026516,1.6715541839
98030436,1.6911046504
98034348,1.6617790222
98038268,1.6422286987
98042192,1.6715541839
98046124,1.7204301357
98050032,1.7399804592
98053952,1.7155425548
98057880,1.6911046504
98061800,1.7106549739
98065716,1.7595307826
98069636,1.7937438488
98073556,1.7888562679
98077476,1.7741935253
98081408,1.8084066390
98085312,1.8719452857
98089228,1.9305962562
98093144,1.9257086753
98097048,1.9257086753
98100968,1.9599218368
98104884,2.0332355499
98108804,2.0967741012
98112724,2.1016616821
98116652,2.0869989395
98120564,2.0967741012
98124484,2.1456501483
98128404,2.1847507953
98132324,2.1749756336
98136252,2.1212120056
98140156,2.0967741012
98144068,2.1114368438
98147996,2.0967741012
98151908,2.0430107116
98155824,1.9501466751
98159748,1.8817204475
98163664,1.8475073814
98167584,1.8377322196
98171508,1.7937438488
98175440,1.7253177165
98179364,1.7057673931
98183296,1.7106549739
98187200,1.7448680400
98191108,1.7546432018
98195032,1.7302052974
98198952,1.7302052974
98202868,1.7741935253
98206800,1.8426198005
98210712,1.8866080284
98214628,1.8914956092
98218548,1.8914956092
98222472,1.9403715133
98226396,2.0087976455
98230308,2.0527858734
98234212,2.0527858734
98238132,2.0527858734
98242044,2.0869989395
98245964,2.1407625675
98249892,2.1798632144
98253812,2.1749756336
98257740,2.1652004718
98261660,2.1896383762
98265588,2.2385141849
98269516,2.2678396701
98273444,2.2385141849
98277364,2.1896383762
98281292,2.1798632144
98285212,2.1994135379
98289140,2.2091886997
98293052,2.1798632144
98296980,2.1212120056
98300892,2.0918865203
98304804,2.1114368438
98308732,2.1163244247
98312660,2.0674486160
98316572,2.0087976455
98320480,1.9892473220
98324392,1.9892473220
98328308,2.0087976455
98332216,1.9892473220
98336132,1.9501466751
98340048,1.9354838371
98343972,1.9696969985
98347888,1.9843597412
98351812,1.9696969985
98355736,1.9159335136
98359664,1.8866080284
98363576,1.9012707710
98367484,1.9305962562
98371408,1.9208210945
98375324,1.8817204475
98379240,1.8719452857
98383156,1.8817204475
98387072,1.9305962562
98390984,1.9403715133
98394904,1.9159335136
98398832,1.9012707710
98402744,1.9354838371
98406672,1.9794721603
98410584,1.9941349029
98414492,1.9696969985
98418416,1.9550342559
98422336,1.9843597412
98426260,2.0430107116
98430164,2.0723361968
98434076,2.0527858734
98437988,2.0381231307
98441900,2.0625610351
98445820,2.1065492630
98449740,2.1309874057
98453660,2.1065492630
98457572,2.0869989395
98461492,2.0918865203
98465404,2.1456501483
98469324,2.1847507953
98473236,2.1749756336
98477148,2.1505377292
98481052,2.1652004718
98484972,2.1945259571
98488900,2.2287390232
98492820,2.2091886997
98496732,2.1700880527
98500644,2.1652004718
98504556,2.2091886997
98508476,2.2531769275
98512404,2.2336266040
98516324,2.1994135379
98520244,2.2043011188
98524152,2.2531769275
98528068,2.2873899936
98531988,2.2727272510
98535908,2.2238514423
98539836,2.1994135379
98543764,2.2336266040
98547676,2.2580645084
98551588,2.2482893466
98555508,2.1994135379
98559436,2.1652004718
98563356,2.1603128910
98567268,2.1700880527
98571164,2.1309874057
98575068,2.0527858734
98578992,1.9843597412
98582920,1.9648094177
98586840,1.9696969985
98590756,1.9501466751
98594680,1.8963831901
98598596,1.8523949623
98602528,1.8572825431
98606456,1.8621701240
98610376,1.8670577049
98614292,1.8328446388
98618204,1.8132943153
98622132,1.8426198005
98626048,1.8963831901
98629968,1.9257086753
98633892,1.8914956092
98637808,1.8670577049
98641716,1.8914956092
98645640,1.9941349029
98649556,2.1456501483
98653476,2.3313782215
98657404,2.5708699226
98661316,2.8885631561
98665236,3.2306940555
98669148,3.4799609184
98673064,3.4604105949
98676972,3.1769306659
98680884,2.7614858150
98684796,2.2678396701
98688720,1.8230694770
98692628,1.4418377876
98696556,1.2023460865
98700476,1.1241446733
98704400,1.1876833438
98708316,1.3098728656
98712228,1.4125122070
98716148,1.4858260154
98720060,1.5493645668
98723988,1.6275659561
98727908,1.6764417648
98731836,1.6911046504
98735748,1.6617790222
98739676,1.6471162796
98743608,1.6715541839
98747532,1.7057673931
98751452,1.7057673931
98755372,1.6568914413
98759300,1.6275659561
98763220,1.6422286987
98767140,1.6862169265
98771056,1.6911046504
98774964,1.6617790222
98778884,1.6568914413
98782812,1.6911046504
98786728,1.7448680400
98790632,1.7790811061
98794544,1.7693059444
98798452,1.7644183635
98802384,1.7888562679
98806312,1.8279570579
98810224,1.8426198005
98814132,1.8181818962
98818044,1.7986314296
98821972,1.8181818962
98825900,1.8523949623
98829832,1.8719452857
98833760,1.8377322196
98837684,1.8035190582
98841596,1.7986314296
98845528,1.8377322196
98849456,1.8670577049
98853368,1.8523949623
98857292,1.8181818962
98861220,1.8328446388
98865140,1.8866080284
98869048,1.9305962562
98872968,1.9305962562
98876888,1.9012707710
98880800,1.9208210945
98884704,1.9599218368
98888624,1.9892473220
98892544,1.9599218368
98896464,1.8866080284
98900376,1.8426198005
98904296,1.8377322196
98908216,1.8328446388
98912132,1.7839686870
98916040,1.7008798122
98919956,1.6471162796
98923884,1.6373411178
98927812,1.6324535369
98931740,1.5982404708
98935644,1.5151515007
98939564,1.4613881111
98943492,1.4418377876
98947424,1.4271749496
98951348,1.3685239553
98955260,1.2707722187
98959180,1.1925709247
98963092,1.1534701585
98967008,1.1339198350
98970932,1.1045943498
98974840,1.0215053558
98978748,0.9677418708
98982660,0.9579667091
98986572,0.9775171279
98990492,0.9824047088
98994396,0.9237536430
98998308,0.8748778343
99002212,0.8797654151
99006132,0.9188660621
99010036,0.9286412239
99013956,0.9090909004
99017836,0.8895405769
99021740,0.9042033195
99025644,0.9530791282
99029556,0.9921798706
99033468,0.9970674514
99037380,0.9872922897
99041296,1.0166177749
99045196,1.0752688646
99049100,1.1192570924
99053016,1.1290322542
99056940,1.0997067642
99060840,1.1094819307
99064744,1.1485825777
99068668,1.1925709247
99072588,1.2023460865
99076508,1.1925709247
99080428,1.2023460865
99084340,1.2658846378
99088252,1.3343108892
99092168,1.3587487936
99096084,1.3343108892
99100004,1.3294233083
99103924,1.3636363744
99107860,1.4027370452
99111772,1.3831867027
99115700,1.3343108892
99119628,1.3147605657
99123556,1.3343108892
99127480,1.3587487936
99131404,1.3538612127
99135324,1.3049852848
99139236,1.2756597995
99143156,1.2903225421
99147076,1.3196481466
99151012,1.3147605657
99154924,1.2707722187
99158844,1.2072336673
99162764,1.2023460865
99166676,1.2267839908
99170588,1.2365591526
99174508,1.1974585056
99178420,1.1632453203
99182340,1.1534701585
99186248,1.1876833438
99190164,1.1974585056
99194072,1.1583577394
99197996,1.1192570924
99201916,1.1192570924
99205832,1.1730204820
99209748,1.2072336673
99213668,1.2023460865
99217588,1.1779080629
99221488,1.1876833438
99225412,1.2267839908
99229332,1.2707722187
99233244,1.2609970569
99237152,1.2365591526
99241068,1.2463343143
99244988,1.2805473804
99248900,1.2952101230
99252820,1.2805473804
99256732,1.2316715717
99260660,1.2316715717
99264588,1.2854349613
99268512,1.3391984701
99272436,1.3538612127
99276364,1.3343108892
99280292,1.3391984701
99284212,1.3782991170
99288116,1.4271749496
99292040,1.4369501113
99295964,1.4076246261
99299892,1.4076246261
99303816,1.4662756919
99307740,1.5395894050
99311652,1.5640274047
99315564,1.5444769859
99319484,1.5444769859
99323412,1.5786901473
99327332,1.6275659561
99331252,1.6520038604
99335156,1.6422286987
99339076,1.6275659561
99343004,1.6422286987
99346924,1.6666666030
99350844,1.6568914413
99354764,1.6031280517
99358676,1.5542521476
99362604,1.5542521476
99366532,1.5835777282
99370460,1.5982404708
99374372,1.5835777282
99378300,1.5640274047
99382204,1.5835777282
99386132,1.6373411178
99390056,1.6715541839
99393980,1.6520038604
99397892,1.6275659561
99401812,1.6422286987
99405736,1.6862169265
99409664,1.7106549739
99413580,1.6911046504
99417500,1.6568914413
99421432,1.6715541839
99425348,1.7204301357
99429256,1.8084066390
99433164,1.9208210945
99437068,2.0918865203
99440980,2.3655912876
99444912,2.7321603298
99448828,3.0596284866
99452752,3.2453567981
99456680,3.1867058277
99460600,2.9374389648
99464516,2.5610947608
99468428,2.1163244247
99472356,1.6813293457
99476284,1.3343108892
99480200,1.1436949968
99484112,1.1339198350
99488036,1.2365591526
99491956,1.3440860509
99495864,1.4320625305
99499780,1.5298142433
99503708,1.6422286987
99507636,1.7350928783
99511556,1.7644183635
99515480,1.7399804592
99519396,1.7350928783
99523320,1.7448680400
99527220,1.7350928783
99531140,1.6862169265
99535064,1.5933528900
99538980,1.5102639198
99542892,1.4711632728
99546820,1.4467253684
99550748,1.3978494453
99554668,1.3049852848
99558588,1.2072336673
99562504,1.1485825777
99566428,1.1192570924
99570348,1.0752688646
99574256,1.0068426132
99578176,0.9384163856
99582084,0.9188660621
99585988,0.9188660621
99589900,0.9188660621
99593812,0.8895405769
99597716,0.8748778343
99601636,0.8651026725
99605552,0.9090909004
99609436,0.9481915473
99613356,0.9530791282
99617268,0.9237536430
99621180,0.9335288047
99625080,1.0019550323
99628980,1.0752688646
99632888,1.0801564407
99636792,1.0703812837
99640704,1.0899316024
99644616,1.1436949968
99648536,1.2170088291
99652444,1.2170088291
99656356,1.2023460865
99660268,1.2072336673
99664180,1.2561094760
99668084,1.3000977039
99671980,1.3147605657
99675900,1.2952101230
99679820,1.3000977039
99683728,1.3587487936
99687652,1.4027370452
99691568,1.4222873687
99695484,1.3978494453
99699404,1.3880742835
99703328,1.4173997879
99707248,1.4565005302
99711156,1.4760508537
99715064,1.4271749496
99718988,1.3929618644
99722908,1.3929618644
99726828,1.4076246261
99730748,1.3831867027
99734668,1.3147605657
99738580,1.2561094760
99742492,1.2414467334
99746420,1.2658846378
99750340,1.2658846378
99754252,1.2365591526
99758168,1.2121212482
99762084,1.2365591526
99766012,1.3000977039
99769916,1.3538612127
99773856,1.3685239553
99777780,1.3929618644
99781704,1.4662756919
99785620,1.5640274047
99789532,1.6568914413
99793460,1.6959922313
99797392,1.7057673931
99801312,1.7399804592
99805228,1.7937438488
99809148,1.8377322196
99813072,1.8377322196
99816996,1.8230694770
99820920,1.8475073814
99824840,1.9061583518
99828756,1.9501466751
99832680,1.9599218368
99836608,1.9501466751
99840536,1.9599218368
99844452,2.0087976455
99848364,2.0527858734
99852268,2.0527858734
99856184,2.0283479690
99860092,2.0185728073
99864012,2.0576734542
99867932,2.0967741012
99871836,2.0869989395
99875740,2.0478982925
99879652,2.0234603881
99883564,2.0527858734
99887484,2.1065492630
99891404,2.1163244247
99895332,2.0772237777
99899236,2.0527858734
99903156,2.0821113586
99907076,2.1065492630
99910996,2.1016616821
99914916,2.0576734542
99918828,2.0283479690
99922740,2.0430107116
99926652,2.0821113586
99930572,2.1016616821
99934492,2.0576734542
99938404,2.0332355499
99942316,2.0674486160
99946220,2.1309874057
99950124,2.1749756336
99954052,2.1652004718
99957972,2.1260998249
99961892,2.1456501483
99965804,2.1945259571
99969732,2.2336266040
99973644,2.2189638614
99977564,2.1945259571
99981492,2.2043011188
99985404,2.2482893466
99989332,2.2922775745
99993252,2.2580645084
99997164,2.2238514423
100001084,2.2189638614
100005044,2.2678396701
100009004,2.2776148319
100012956,2.2385141849
100016924,2.1798632144
100020892,2.1603128910
100024844,2.1798632144
100028804,2.2140762805
100032756,2.1798632144
100036716,2.1358749866
100040676,2.1163244247
100044644,2.1358749866
100048604,2.1603128910
100052556,2.1407625675
100056516,2.0967741012
100060468,2.0918865203
100064420,2.1163244247
100068384,2.1407625675
100072340,2.1065492630
100076292,2.0478982925
100080244,2.0332355499
100084196,2.0478982925
100088156,2.0674486160
100092100,2.0332355499
100096056,1.9696969985
100100004,1.9110459327
100103968,1.9061583518
100107928,1.9208210945
100111884,1.8768328666
100115844,1.8181818962
100119812,1.7888562679
100123776,1.8084066390
100127712,1.8475073814
100131672,1.8523949623
100135636,1.8181818962
100139604,1.8035190582
100143560,1.8377322196
100147524,1.8768328666
100151488,1.8719452857
100155448,1.8523949623
100159404,1.8132943153
100163376,1.8426198005
100167328,1.8963831901
100171276,1.9110459327
100175232,1.9061583518
100179188,1.9501466751
100183132,2.1016616821
100187084,2.3216030597
100191036,2.5904202461
100194996,2.8787879943
100198956,3.1769306659
100202916,3.4555230140
100206876,3.5826001167
100210836,3.4115347862
100214804,3.0205278396
100218748,2.5317692756
100222708,2.1016616821
100226676,1.7937438488
100230640,1.5933528900
100234592,1.4858260154
100238548,1.5053763389
100242500,1.6422286987
100246456,1.8377322196
100250424,1.9990224838
100254380,2.0967741012
100258340,2.1652004718
100262284,2.2434017658
100266236,2.3411533832
100270196,2.4242424964
100274140,2.4731183052
100278112,2.4975562095
100282068,2.5562071800
100286020,2.6441838741
100289992,2.7028348445
100293948,2.7077224254
100297908,2.7126100063
100301868,2.7468230724
100305820,2.8054740905
100309764,2.8250244140
100313724,2.7908113002
100317676,2.7370479106
100321636,2.7223851680
100325600,2.7419354915
100329556,2.7517106533
100333516,2.7126100063
100337468,2.6735093593
100341428,2.6686217784
100345392,2.7028348445
100349348,2.7272727489
100353316,2.6881721019
100357260,2.6441838741
100361212,2.6588466167
100365180,2.6832845211
100369140,2.7077224254
100373100,2.6783969402
100377052,2.6148581504
100381012,2.6001954078
100384960,2.6246333122
100388916,2.6490714550
100392860,2.6197457313
100396828,2.5659823417
100400788,2.5562071800
100404740,2.5806450843
100408692,2.6099705696
100412644,2.5904202461
100416588,2.5366568565
100420548,2.5268816947
100424508,2.5610947608
100428460,2.5953078269
100432412,2.5757575035
100436372,2.5171065330
100440324,2.4926686286
100444276,2.5219941139
100448228,2.5366568565
100452196,2.5073313713
100456148,2.4389052391
100460108,2.3949170112
100464068,2.3753666877
100468028,2.3655912876
100471988,2.3069403171
The trick seems to be the initial conditions. Load the first 13 values of input and output of low pass filter to zero and the bias goes away.
#low-pass filter
def lpf(x):
y = x.copy()
for n in range(13):
y[n,1] = 0
x[n,1] = 0
for n in range(len(x)):
if(n < 12):
continue
y[n,1] = 2*y[n-1,1] - y[n-2,1] + x[n,1] - 2*x[n-6,1] + x[n-12,1]
return y

Altair - Gradient above line

I want to create an area chart, however the gradient should run from the line up to the top of the chart. Any ideas?
example of a regular gradient chart here https://altair-viz.github.io/gallery/area_chart_gradient.html
alt.Chart(source).transform_filter(
'datum.symbol==="GOOG"'
).mark_area(
line={'color':'darkgreen'},
color=alt.Gradient(
gradient='linear',
stops=[alt.GradientStop(color='white', offset=0),
alt.GradientStop(color='darkgreen', offset=1)],
x1=1,
x2=1,
y1=1,
y2=0
)
).encode(
alt.X('date:T'),
alt.Y('price:Q')
)
You can do this by setting the y2 encoding to alt.value(0) – the zero in this case measures pixels from the top of the chart axis:
import altair as alt
from vega_datasets import data
source = data.stocks()
alt.Chart(source).transform_filter(
'datum.symbol==="GOOG"'
).mark_area(
line={'color':'darkgreen'},
color=alt.Gradient(
gradient='linear',
stops=[alt.GradientStop(color='white', offset=0),
alt.GradientStop(color='darkgreen', offset=1)],
x1=1,
x2=1,
y1=1,
y2=0
)
).encode(
alt.X('date:T'),
alt.Y('price:Q'),
y2=alt.value(0)
)

Obtaining hyperpolarization depth from electrophysiological graph

I am working on electrophysiological data which is in .abf format.
I want to obtain the hyperpolarization depth as indicated above in the figure. This is what I have done so far;
import matplotlib.pyplot as plt
import pyabf
import pandas as pd
abf = pyabf.ABF("test.abf")
abf.setSweep(10) # I can access a given sweep. Here sweep 10
df = pd.DataFrame({'time': abf.sweepX, 'current':abf.sweepY})
df1 = df.loc[15650:15800]
df1.plot(x='time', y='current')
I am thinking to apply change in derivative to find the first point of interest (x1,y1) and then lower point (x2,y2), but it looks complex. I would appreciate if someone give some hint or procedure.
The dataset as follow,
time current
0.7825 -63.323975
0.78255 -63.171387
0.7826 -62.89673
0.78265 -62.713623
0.7827 -62.469482
0.78275 -62.37793
0.7828 -62.10327
0.78285 -61.950684
0.7829 -61.76758
0.78295 -61.584473
0.783 -61.401367
0.78305 -61.24878
0.7831 -61.035156
0.78315 -60.85205
0.7832 -60.72998
0.78325 -60.516357
0.7833 -60.455322
0.78335 -60.2417
0.7834 -60.08911
0.78345 -59.96704
0.7835 -59.814453
0.78355 -59.661865
0.7836 -59.509277
0.78365 -59.417725
0.7837 -59.23462
0.78375 -59.11255
0.7838 -58.95996
0.78385 -58.86841
0.7839 -58.685303
0.78395 -58.59375
0.784 -58.441162
0.78405 -58.34961
0.7841 -58.19702
0.78415 -58.044434
0.7842 -57.922363
0.78425 -57.769775
0.7843 -57.678223
0.78435 -57.434082
0.7844 -57.34253
0.78445 -56.9458
0.7845 -56.274414
0.78455 -54.96216
0.7846 -53.253174
0.78465 -51.208496
0.7847 -48.950195
0.78475 -46.325684
0.7848 -43.09082
0.78485 -38.42163
0.7849 -31.036377
0.78495 -22.033691
0.785 -13.397217
0.78505 -6.072998
0.7851 -0.61035156
0.78515 2.7160645
0.7852 3.9367676
0.78525 3.4179688
0.7853 1.3427734
0.78535 -1.4953613
0.7854 -5.0964355
0.78545 -9.185791
0.7855 -13.641357
0.78555 -18.249512
0.7856 -23.132324
0.78565 -27.98462
0.7857 -32.714844
0.78575 -37.261963
0.7858 -41.47339
0.78585 -45.22705
0.7859 -48.553467
0.78595 -51.54419
0.786 -53.985596
0.78605 -56.18286
0.7861 -58.013916
0.78615 -59.539795
0.7862 -60.760498
0.78625 -61.88965
0.7863 -62.652588
0.78635 -63.323975
0.7864 -63.934326
0.78645 -64.2395
0.7865 -64.60571
0.78655 -64.78882
0.7866 -65.00244
0.78665 -64.971924
0.7867 -65.093994
0.78675 -65.03296
0.7868 -64.971924
0.78685 -64.819336
0.7869 -64.78882
0.78695 -64.66675
0.787 -64.48364
0.78705 -64.42261
0.7871 -64.2395
0.78715 -64.11743
0.7872 -63.964844
0.78725 -63.842773
0.7873 -63.659668
0.78735 -63.568115
0.7874 -63.446045
0.78745 -63.26294
0.7875 -63.171387
0.78755 -62.98828
0.7876 -62.89673
0.78765 -62.74414
0.7877 -62.713623
0.78775 -62.530518
0.7878 -62.438965
0.78785 -62.37793
0.7879 -62.25586
0.78795 -62.164307
0.788 -62.042236
0.78805 -62.01172
0.7881 -61.88965
0.78815 -61.88965
0.7882 -61.73706
0.78825 -61.706543
0.7883 -61.645508
0.78835 -61.61499
0.7884 -61.523438
0.78845 -61.462402
0.7885 -61.431885
0.78855 -61.340332
0.7886 -61.37085
0.78865 -61.279297
0.7887 -61.279297
0.78875 -61.157227
0.7888 -61.187744
0.78885 -61.09619
0.7889 -61.157227
0.78895 -61.12671
0.789 -61.09619
0.78905 -61.12671
0.7891 -61.00464
0.78915 -61.00464
0.7892 -60.97412
0.78925 -60.97412
0.7893 -60.943604
0.78935 -61.00464
0.7894 -60.913086
0.78945 -60.97412
0.7895 -60.943604
0.78955 -60.913086
0.7896 -60.943604
0.78965 -60.85205
0.7897 -60.85205
0.78975 -60.821533
0.7898 -60.88257
0.78985 -60.88257
0.7899 -60.913086
0.78995 -60.88257
0.79 -60.913086
We can plot the difference in current between consecutive points (which essentially is to a constant factor the derivative, since times are evenly spaced). First chart shows the actual diffs. Based on this we can set some threshold, such as 0.3, and apply it to filter the main DataFrame. The filtered values are shown in orange on the second chart:
fig, ax = plt.subplots(2, figsize=(8,8))
# plot derivative
df['current'].diff().plot(ax=ax[0])
# current
threshold = 0.4
df['filtered'] = df.loc[df['current'].diff().abs() > threshold]
df.plot(ax=ax[1])
# add spans
x = df['filtered'].dropna()
ax[1].axhspan(x.iloc[0], x.iloc[-1], alpha=0.3, edgecolor='skyblue', facecolor="none", hatch='////')
ax[1].axvspan(x.index.min(), x.index.max(), alpha=0.3, edgecolor='orange', facecolor="none", hatch='\\\\')
Output:
If you're interested in range values, you can dropna values in the filtered subset and find min and max from the index:
print('min', df['filtered'].dropna().index.min())
print('max', df['filtered'].dropna().index.max())
Output:
min 0.78445
max 0.7865
For the value of the gap you can use:
abs(df['filtered'].dropna().iloc[-1] - df['filtered'].dropna().iloc[0])
Output:
7.6599100000000035
Note: We can alternatively also get left edges of these spans as points where diff in the point is lower than the threshold and diff in the next point is higher than the threshold, and similarly for the right edges. This would also work in case we have multiple peaks:
threshold = 0.3
x = df['current'].diff().abs()
spanA = df.loc[(x < threshold) & (x.shift(-1) >= threshold)]
spanB = df.loc[(x >= threshold) & (x.shift(-1) < threshold)]
print(spanA)
current
time
0.7844 -57.34253
print(spanB)
current
time
0.7865 -64.60571

Resources