Remove repeating values from X axis label in Altair - python-3.x

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

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?

How to get final figure width (uselatex)

I am writing a latex document and using matplotlib for plots. I want to have the font and font size (9) of the captions of my latex document also for the plot axes and legend text.
Furthermore, I would like to fill out the \linewidth or \textwidth of my latex document, which is 369 pt. Now the matplotlib.pyplot.figure function accepts the input parameter figsize which should be in inches, so I duly specify it as 369/72 inches, 1/72 being the conversion factor from pt to inches.
Later I cut down excess white space by using the bbox_inches=tight and pad_inches=0 options of the savefig function.
The font and font size part works as intended. It looks exactly identical between the figure text and the caption text. However, I am still dissatisfied with the figure width.
Below is a minimal example of a figure I produce.
import matplotlib
import matplotlib.pyplot as plt
plt.rcdefaults()
plt.rcParams['font.size'] = '9'
plt.rcParams['figure.autolayout'] = False
matplotlib.rc('font', family='sans-serif', serif=['Palatino'])
matplotlib.rc('text', usetex=True)
params = {'text.latex.preamble': [
r'\usepackage[american]{babel}',
r'\usepackage{mathpazo}',
r'\usepackage{amsmath,amssymb,amsfonts,mathrsfs}',
r'\usepackage{textcomp}',
]}
plt.rcParams.update(params)
plt.rcParams['mathtext.default'] = 'regular'
plt.rcParams['legend.handlelength'] = 1
delta_adjust = 0
plt.rcParams['figure.subplot.bottom'] = delta_adjust
plt.rcParams['figure.subplot.top'] = 1 - delta_adjust
plt.rcParams['figure.subplot.left'] = delta_adjust
plt.rcParams['figure.subplot.right'] = 1 - delta_adjust
plt.rcParams['figure.subplot.hspace'] = 0.55
plt.rcParams['figure.subplot.wspace'] = 0.55
default_figsize=(369/72, 369/72)
fig = plt.figure(figsize=default_figsize)
sp1 = plt.subplot(3,3,1)
sp2 = plt.subplot(3,3,2)
sp3 = plt.subplot(3,3,3)
for sp in sp1, sp2, sp3:
sp.set_title('Title')
sp.set_xlabel('Xlabel')
sp.set_ylabel('Ylabel')
twin = sp3.twinx()
twin.set_ylabel('Ylabel')
fig.set_size_inches(default_figsize)
fig.savefig('./example.pdf', transparent=False, bbox_inches='tight', pad_inches=0, ending='.pdf')
This is the result of the above code. The figure has a width of 429.356 pt instead of the desired 369 pt. When I increase the delta_adjust parameter in the code, I get smaller pdf widths.
[philipp#desktop scripts]$ python minimal_example.py
[philipp#desktop scripts]$ pdfinfo example.pdf
Creator: matplotlib 3.1.2, http://matplotlib.org
Producer: matplotlib pdf backend 3.1.2
CreationDate: Thu Jan 13 11:41:13 2022 CET
Tagged: no
UserProperties: no
Suspects: no
Form: none
JavaScript: no
Pages: 1
Encrypted: no
Page size: 429.356 x 130.412 pts
Page rot: 0
File size: 102863 bytes
Optimized: no
PDF version: 1.4
When I scale the figsize parameter of the python code from 369 pt to 369*369/429 pt, I end up with a 386 pt pdf. I do not want to use a trial and error strategy to find the correct parameter. As a last resort, I could write an iterative program as a savefig routine but I would prefer to avoid this. For reference, here is the output of the program converted to png: image
In summary, I am looking for help on how to set the figure width reliably.
I am on Ubuntu 20.04, python 3.8, matplotlib 3.1.2, and I use the TkAgg backend which is the default.
Any help is appreciated.
Only after posting this question did this website recommend me the following question: How to get figure size and fontsize right for PDFs exported from matplotlib?
It turns out that bbox_inches=tight messes with the figure size.
I removed this option and set delta_adjust = 0.1 in the code above.
Now the figure has the expected size of exactly 369x369 pt.
Most of it is whitespace, which I can remove using the pdfcrop command line utility.
The current script looks like this.
import os
import matplotlib
import matplotlib.pyplot as plt
plt.rcdefaults()
plt.rcParams['font.size'] = '9'
plt.rcParams['figure.autolayout'] = False
matplotlib.rc('font', family='sans-serif', serif=['Palatino'])
matplotlib.rc('text', usetex=True)
params = {'text.latex.preamble': [
r'\usepackage[american]{babel}',
r'\usepackage{mathpazo}',
r'\usepackage{amsmath,amssymb,amsfonts,mathrsfs}',
r'\usepackage{textcomp}',
]}
plt.rcParams.update(params)
plt.rcParams['mathtext.default'] = 'regular'
plt.rcParams['legend.handlelength'] = 1
delta_adjust_h = 0.1
delta_adjust_v = 0.1
plt.rcParams['figure.subplot.bottom'] = delta_adjust_v
plt.rcParams['figure.subplot.top'] = 1 - delta_adjust_v
plt.rcParams['figure.subplot.left'] = delta_adjust_h
plt.rcParams['figure.subplot.right'] = 1 - delta_adjust_h
plt.rcParams['figure.subplot.hspace'] = 0.55
plt.rcParams['figure.subplot.wspace'] = 0.55
#factor = 369/429.356
factor = 1
default_figsize=(369/72*factor, 369/72*factor)
fig = plt.figure(figsize=default_figsize)
sp1 = plt.subplot(3,3,1)
sp2 = plt.subplot(3,3,2)
sp3 = plt.subplot(3,3,3)
for sp in sp1, sp2, sp3:
sp.set_title('Title')
sp.set_xlabel('Xlabel')
sp.set_ylabel('Ylabel')
twin = sp3.twinx()
twin.set_ylabel('Ylabel')
fig.set_size_inches(default_figsize)
#fig.savefig('./example.pdf', transparent=False, ending='.pdf', pad_inches=0)
# bbox_inches='tight', pad_inches=0, ending='.pdf')
fig.savefig('./example.pdf', transparent=True, pad_inches=0, ending='.pdf')
os.system('pdfcrop ./example.pdf ./example_cropped.pdf')
The output of pdfinfo is the following:
[philipp#desktop scripts]$ pdfinfo example_cropped.pdf
Creator: TeX
Producer: pdfTeX-1.40.20
CreationDate: Thu Jan 13 13:28:25 2022 CET
ModDate: Thu Jan 13 13:28:25 2022 CET
Tagged: no
UserProperties: no
Suspects: no
Form: none
JavaScript: no
Pages: 1
Encrypted: no
Page size: 364 x 112 pts
Page rot: 0
File size: 102853 bytes
Optimized: no
PDF version: 1.4
So it is still not perfect, as the figure width is slightly too small due to the cropping.
Also it is now not guaranteed that all the contents fit within the printed pdf, which previously was ensured by the bbox_inches option.
Nevertheless, this is an improvement as now the plot sizes can no longer exceed the latex \linewidth.
I may update this answer if I find a better solution.
Edit: There is a feature under development at matplotlib which would solve the problem: https://matplotlib.org/stable/tutorials/intermediate/constrainedlayout_guide.html
I tried it but it seems to work best when the subplots are all created at once, such as with plt.subplots. I will have to change all my scripts, because currently I add subplots one by one with plt.subplot. Moreover I need to set the vertical figure size explicitly instead of simply generating a square figure and cropping all the unused whitespace.

What is the proper way to employ date2num for timestamps using candlestick_ohlc

My data looks like this (Date, Open, High, Low, Close):
ohlc = [
[1502929058, 1.2652, 1.2653, 1.265, 1.2653],
[1502929059, 1.267, 1.267, 1.267, 1.267],
[1502929060, 1.2655, 1.2656, 1.2655, 1.2656],
[1502929061, 1.2652, 1.2653, 1.2652, 1.2653],
[1502929062, 1.2631, 1.2631, 1.263, 1.2631],
[1502929063, 1.2625, 1.2625, 1.2625, 1.2625],
[1502929064, 1.2619, 1.2619, 1.2619, 1.2619],
[1502929065, 1.2622, 1.2623, 1.2622, 1.2623],
[1502929066, 1.2622, 1.2623, 1.2622, 1.2623],
[1502929067, 1.2617, 1.262, 1.2617, 1.262]
]
and I'm using the code blow to plot the candlesticks:
for row in ohlc:
row[0] = mdates.date2num(datetime.datetime.fromtimestamp(row[0]))
fig = plt.figure()
ax1 = plt.subplot2grid((1,1), (0,0))
candlestick_ohlc(ax1,ohlc,width=0.1)
fig.subplots_adjust(bottom=0.3)
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d %H:%M:%S'))
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(45)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()
but the candlesticks being drawn on top of each other:
as I checked the code further, I noticed that mdates.date2num(datetime.datetime.fromtimestamp(row[0])) is actually generating dates with very minute differences (and therefore candlesticks being drawn on top of each other):
736558.1997453704
736558.1997569444
736558.1997685186
736558.1997800926
736558.1997916667
736558.1998032407
736558.1998148148
736558.1998263889
736558.199837963
736558.199849537
what is the solution to this problem?

pie doesn't allow negative values

I am trying to draw a pie chart using Matplotlib, though there are no negative values present, I keep getting the error "pie doesn't allow negative values"!
contrib = sales_data.groupby('Region')['Sales2016'].sum().round().reset_index()
contrib["Percentage"] = (contrib.Sales2016/sum(contrib.Sales2016))*100
contrib = contrib.drop(columns = ["Sales2016"])
contrib.plot(kind = "pie", subplots = True).plot(kind = "pie",subplots=True,legend=False,figsize=(12,5),autopct="%.2f%%")
plt.show()
Is it possible to point out where am I going wrong? The following is the output for contrib:
Region Percentage
0 Central 32.994771
1 East 42.701319
2 West 24.303911
Define argument y in the pie plot:
contrib.plot(kind = "pie",y="Percentage",labels=['Region'],legend=False,figsize=(12,5),autopct="%.2f%%")

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