When x_range is set, how can I make the time scale of the X-axis automatically display the corresponding details as the zoom occurs?

Hi,

Here is my code:

import pandas as pd
from bokeh.io import output_notebook
from bokeh.models import ColumnDataSource
from bokeh.palettes import Bokeh8
from bokeh.plotting import figure, show

output_notebook()
data = {'date': ['2019-08-21',
                 '2019-09-02',
                 '2019-10-08',
                 '2019-11-01',
                 '2019-12-02',
                 '2020-01-02',
                 '2020-02-03',
                 '2020-03-02',
                 '2020-04-01',
                 '2020-05-06',
                 '2020-06-01',
                 '2020-07-01',
                 '2020-08-03',
                 '2020-09-01',
                 '2020-10-09',
                 '2020-11-02',
                 '2020-12-01',
                 '2021-01-04',
                 '2021-02-01',
                 '2021-03-01',
                 '2021-04-01',
                 '2021-05-06',
                 '2021-06-01',
                 '2021-07-01',
                 '2021-08-02',
                 '2021-09-01',
                 '2021-10-08',
                 '2021-11-01',
                 '2021-12-01',
                 '2022-01-04',
                 '2022-02-07',
                 '2022-03-01',
                 '2022-04-01',
                 '2022-05-05',
                 '2022-06-01',
                 '2022-07-01',
                 '2022-08-01',
                 '2022-09-01',
                 '2022-10-10',
                 '2022-11-01',
                 '2022-12-01',
                 '2023-01-03',
                 '2023-02-01',
                 '2023-03-01',
                 '2023-04-03',
                 '2023-05-04',
                 '2023-06-01',
                 '2023-07-03',
                 '2023-08-01',
                 '2023-09-01',
                 '2023-10-09',
                 '2023-11-01',
                 '2023-12-01',
                 '2024-01-02',
                 '2024-02-01',
                 '2024-03-01',
                 '2024-04-01',
                 '2024-05-06',
                 '2024-06-03',
                 '2024-07-01'],
        'AAA': [38.548501355706556,
                47.88238931293438,
                68.1553420611671,
                61.50283320583812,
                33.696499165875956,
                48.53466303474638,
                51.65866576895849,
                35.94462300035041,
                63.34115752295874,
                38.053395874857316,
                29.833590569385297,
                35.81508411386094,
                40.35360896782375,
                55.01534082331641,
                80.67954996060007,
                58.58564232425918,
                54.47585528685438,
                58.13707733614629,
                51.357312548966846,
                70.16268840811519,
                61.20089201050857,
                49.03330677664287,
                52.17942347974438,
                83.66722527892423,
                38.105843161662726,
                75.64412679447241,
                74.30301115878919,
                61.780178806274,
                51.393361945047054,
                64.96906799198881,
                62.66255786417349,
                75.60701635334077,
                88.07239920879474,
                63.153916709650105,
                53.61965606935439,
                79.2525179604621,
                48.669474667731514,
                60.597704217990525,
                77.13653727931367,
                65.80902804392302,
                44.55475216447764,
                54.16497425930436,
                58.548028325588255,
                49.83730249356159,
                58.4205897753709,
                44.405341658643245,
                68.33270949809335,
                72.48823353967538,
                65.07616166557801,
                57.97860226333309,
                59.4732785338992,
                71.26454893388495,
                53.87553926642376,
                45.04700046049706,
                58.33088024170677,
                51.190029673478335,
                70.87724582614395,
                62.81680164723068,
                71.15831329606891,
                75.26475547736815],
        'BBB': [8.139767300098688,
                6.379246258316986,
                2.1397876745729327,
                6.313706149780265,
                15.098701765214418,
                7.7490382079431805,
                5.4726797054267635,
                10.405559742857772,
                2.986876875013502,
                4.669670292672878,
                7.534593039764434,
                2.787635835876732,
                12.118937055028901,
                11.702122297877466,
                2.8311461815551,
                7.501054558928145,
                3.6628575569337225,
                8.239654014974363,
                4.954066297839982,
                2.083447797206059,
                2.921473222095478,
                5.137534178496049,
                7.4582543454771635,
                2.487862346261772,
                3.531719837796348,
                4.045284649360511,
                1.7491675845137844,
                3.3466232150159514,
                4.840143341535361,
                4.9917899774011145,
                2.9899770578389937,
                1.4854231702920762,
                1.2255755891115165,
                4.217472534148868,
                2.243957753288968,
                0.4403110214968118,
                3.0835689089384903,
                1.358814085474778,
                1.32401918006161,
                1.0346393179145001,
                3.120564972772968,
                3.491203517758961,
                3.712850268944131,
                4.988604699054436,
                2.0553956517902305,
                4.202924464452326,
                5.646048112953468,
                3.134292131741094,
                2.3498068682642175,
                3.9067051131274053,
                3.468626646538365,
                3.0846532069075083,
                3.173765652430829,
                4.656468001242938,
                4.623212759956554,
                4.605090488799595,
                1.280748354179071,
                4.3397422194240445,
                4.245698908677754,
                3.8753027881689577],
        'CCC': [20.33619640167309,
                21.839053421519523,
                12.984387113073856,
                17.037439813468634,
                30.605229365542254,
                20.129919416080906,
                12.520100978849971,
                18.799694232825843,
                13.031657582957715,
                34.57799846816344,
                31.48451858867735,
                21.398982791901407,
                13.880594036442448,
                16.359987287525573,
                9.576664332282931,
                18.982371743610287,
                13.345619464746417,
                11.382398022766575,
                20.007435257759017,
                7.9718626130888595,
                14.16661151855533,
                24.714673934208427,
                14.467953604503977,
                8.031869997453756,
                14.341776606694575,
                3.9418839554092053,
                4.564235327892936,
                7.486700473316088,
                17.59347697991627,
                12.473576787207497,
                15.04802964216387,
                10.192898968388441,
                3.624050090172247,
                11.076907165314589,
                20.735698860060626,
                11.139008999260989,
                22.53908395429051,
                9.363077384109896,
                6.03765386981592,
                7.858175963833991,
                13.756148766416365,
                12.991557327103815,
                14.902961334387957,
                12.92833522977923,
                24.240163453474683,
                7.470141718699687,
                6.107220998859199,
                7.9960017545868425,
                11.332971437039006,
                17.353084244155685,
                21.790791062441144,
                11.267577893518558,
                26.875857820099007,
                28.33963149477056,
                12.295962219956854,
                8.008082923230933,
                11.498047450042755,
                11.76844418653792,
                10.034094521997764,
                9.1514947839658],
        'DDD': [31.070222323496406,
                22.39069553159074,
                15.20599254915021,
                11.433062868932623,
                16.63054694914773,
                17.73999088605368,
                11.536109040385249,
                5.94744735263699,
                8.215867704420134,
                4.581166834727733,
                13.661244663404798,
                18.11867720632882,
                18.918353106383204,
                2.148206358192932,
                2.129410525603654,
                5.175257751107644,
                10.375251539124092,
                14.424937621863728,
                11.772293710124755,
                8.517816424911617,
                10.006216206400245,
                10.416508463177399,
                12.89717856168254,
                2.406021221902175,
                20.90493365194458,
                6.47207286470695,
                6.725649596411921,
                10.611713116209543,
                13.269476983765664,
                6.829261074943621,
                3.838434899630019,
                6.18507459115281,
                2.123947386164871,
                7.517018945207147,
                8.06897785261463,
                3.1154100903667428,
                9.820284993268954,
                7.9154055109322465,
                4.907888529545782,
                9.080783383593868,
                16.237142357324924,
                12.105998348145983,
                11.276331206371859,
                14.566789325570584,
                8.095837208666254,
                31.809784213595332,
                4.449284501526217,
                4.60115379026931,
                10.00211500817693,
                9.970006959423198,
                7.297299021246675,
                6.308433390618397,
                6.519174687365989,
                7.464980124896396,
                6.143702102581181,
                11.470405217149835,
                10.742121711138521,
                8.28345856640664,
                6.051668845604065,
                3.0236137587073757],
        'EEE': [1.905312619025266,
                1.508615475638359,
                1.5144906020358848,
                3.7129579619803543,
                3.969022754219645,
                5.846388455175859,
                4.806725478330563,
                3.0997337417149606,
                2.3812000701467135,
                3.232288993513746,
                2.495291068737484,
                3.3049396374412248,
                1.4991101051932496,
                1.3972568989086704,
                1.300419913633468,
                4.4854324561116625,
                2.278505504418036,
                3.2668163251214404,
                3.378128590527836,
                2.295955054159599,
                4.279948757941389,
                4.361435962058709,
                2.9785614652010297,
                0.7904443052775995,
                9.881493651844071,
                2.1654982104236375,
                2.487593615611728,
                8.654151321228905,
                2.6928469132045016,
                4.399264158891095,
                8.041776030490281,
                2.4059682971238145,
                1.4876397054355845,
                5.547905672493609,
                7.290249721864706,
                3.048220024855806,
                4.246335920918918,
                9.382396127074584,
                5.046769737500849,
                6.715388617357094,
                5.688932277466472,
                8.66045301040564,
                3.317153951903546,
                5.36844564972132,
                1.8222360398536301,
                5.028298589246329,
                5.437604978634036,
                7.770611578553886,
                4.929525904497059,
                5.693375496236237,
                3.4575641074691306,
                1.6911828946664809,
                4.0572910223095,
                5.394409600446887,
                11.61615955606676,
                13.620242330855644,
                1.0446260006821124,
                2.1788413865634495,
                1.9883885592363564,
                2.835818161097098],
        'FFF': [0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                14.005719028048984,
                25.802941929614015,
                10.043240244503219,
                14.885479536064869,
                14.99076207003063,
                18.574680414590883,
                13.229396729128446,
                13.377086334178953,
                3.4828090863247776,
                5.270241165983078,
                10.341074161225704,
                3.4935202035093234,
                4.829537964020513,
                7.375165079199482,
                5.7836137148547895,
                4.903330675320816,
                7.83665860459272,
                1.8025234144639548,
                11.291889894154314,
                6.094607058597666,
                7.986408019845895,
                5.5232303323366505,
                8.177747791310704,
                4.366037129787624,
                5.206753693605719,
                1.87468061662862,
                1.6607404595406488,
                6.506089290369925,
                5.568879933344677,
                1.5083032544691546,
                8.600861432871042,
                5.082503906682552,
                2.3062030825683615,
                7.191355638638881,
                12.81952998752914,
                5.0769150176586715,
                6.625999371619594,
                8.89162925149309,
                2.7596235315099236,
                4.641602012410844,
                8.07035542708865,
                1.453794632680936,
                2.9886514432763236,
                2.0350371068789914,
                3.828881690130035,
                4.385823310507942,
                2.772365990386064,
                6.796166343935865,
                3.8825397757433975,
                9.12145468824788,
                2.4329337225366556,
                8.013712864420087,
                3.9894513471148487,
                3.431229155470149],
        'GGG': [0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                5.520836486697661,
                1.0555964756182752,
                3.7012256307610416,
                1.5930646233191965,
                1.641244569644223,
                1.4332100100957361,
                2.1819699387981877,
                0.8140534357164931,
                1.942343195903382,
                1.6365264670296171,
                2.18393469693455,
                2.597402735618861,
                2.0329460452204486,
                1.9710028797802668,
                2.21247081209762,
                2.2489380030734702,
                1.805647560780391,
                1.980689682815763,
                2.4725798094719904,
                1.4962286490883918,
                3.040390121980568,
                6.300098767735419,
                3.24092832119379,
                2.310629034738645,
                3.822929474012491,
                3.508898519622578,
                1.616675541184667,
                3.4188933508197654,
                2.6061543393343722,
                2.4419073429522444,
                1.9567764828450604,
                2.5559125724925607,
                3.3207676731684526,
                3.0631888168454005,
                0.6835589382754452,
                1.9977803698961467,
                2.7260055609848433,
                2.301343974210285,
                3.1075433439884845,
                1.9846946782377872,
                2.1242769352769364,
                2.5989991294171886,
                2.5323845213002922,
                2.417785875222468]}
df = pd.DataFrame(data)
df.set_index("date", inplace=True)
source = ColumnDataSource(df)
p = figure(x_range=df.index.tolist(),
           # x_axis_type="datetime",
           tooltips=[("date", "@date"), ("name", "$name"), ("value", "@$name")], sizing_mode="stretch_width")
p.vbar_stack(df.columns.to_list(), x='date', color=Bokeh8[:len(df.columns)], source=data, legend_label=df.columns.to_list())
show(p)

Now, the problem is, if I don’t set “x_axis_type=‘datetime’” in figure parameter, the date appears crowded on the x-axis, like “figure 1”.

But, if I set "x_axis_type=‘datetime’, the date displaying is wrong, like “figure 2”.

How can I make the time scale of the X-axis automatically display the corresponding details as the zoom occurs, like “figure 3”, and when zoom-in, like “figure 4”:

By passing a list of strings for the range, you are creating a categorical axis, which is probably not at all what you want. More likely, you want an actual datetime axis, and for that you will need to convert the data to actual datetime values. See the docs, e.g.

https://docs.bokeh.org/en/latest/docs/user_guide/basic/axes.html#datetime-axes

Hi @Bryan,

I tried, basically added “df[‘date’] = pd.to_datetime(df[‘date’])” at line 489:

import pandas as pd
from bokeh.io import output_notebook
from bokeh.models import ColumnDataSource
from bokeh.palettes import Bokeh8
from bokeh.plotting import figure, show

output_notebook()
data = {'date': ['2019-08-21',
                 '2019-09-02',
                 '2019-10-08',
                 '2019-11-01',
                 '2019-12-02',
                 '2020-01-02',
                 '2020-02-03',
                 '2020-03-02',
                 '2020-04-01',
                 '2020-05-06',
                 '2020-06-01',
                 '2020-07-01',
                 '2020-08-03',
                 '2020-09-01',
                 '2020-10-09',
                 '2020-11-02',
                 '2020-12-01',
                 '2021-01-04',
                 '2021-02-01',
                 '2021-03-01',
                 '2021-04-01',
                 '2021-05-06',
                 '2021-06-01',
                 '2021-07-01',
                 '2021-08-02',
                 '2021-09-01',
                 '2021-10-08',
                 '2021-11-01',
                 '2021-12-01',
                 '2022-01-04',
                 '2022-02-07',
                 '2022-03-01',
                 '2022-04-01',
                 '2022-05-05',
                 '2022-06-01',
                 '2022-07-01',
                 '2022-08-01',
                 '2022-09-01',
                 '2022-10-10',
                 '2022-11-01',
                 '2022-12-01',
                 '2023-01-03',
                 '2023-02-01',
                 '2023-03-01',
                 '2023-04-03',
                 '2023-05-04',
                 '2023-06-01',
                 '2023-07-03',
                 '2023-08-01',
                 '2023-09-01',
                 '2023-10-09',
                 '2023-11-01',
                 '2023-12-01',
                 '2024-01-02',
                 '2024-02-01',
                 '2024-03-01',
                 '2024-04-01',
                 '2024-05-06',
                 '2024-06-03',
                 '2024-07-01'],
        'AAA': [38.548501355706556,
                47.88238931293438,
                68.1553420611671,
                61.50283320583812,
                33.696499165875956,
                48.53466303474638,
                51.65866576895849,
                35.94462300035041,
                63.34115752295874,
                38.053395874857316,
                29.833590569385297,
                35.81508411386094,
                40.35360896782375,
                55.01534082331641,
                80.67954996060007,
                58.58564232425918,
                54.47585528685438,
                58.13707733614629,
                51.357312548966846,
                70.16268840811519,
                61.20089201050857,
                49.03330677664287,
                52.17942347974438,
                83.66722527892423,
                38.105843161662726,
                75.64412679447241,
                74.30301115878919,
                61.780178806274,
                51.393361945047054,
                64.96906799198881,
                62.66255786417349,
                75.60701635334077,
                88.07239920879474,
                63.153916709650105,
                53.61965606935439,
                79.2525179604621,
                48.669474667731514,
                60.597704217990525,
                77.13653727931367,
                65.80902804392302,
                44.55475216447764,
                54.16497425930436,
                58.548028325588255,
                49.83730249356159,
                58.4205897753709,
                44.405341658643245,
                68.33270949809335,
                72.48823353967538,
                65.07616166557801,
                57.97860226333309,
                59.4732785338992,
                71.26454893388495,
                53.87553926642376,
                45.04700046049706,
                58.33088024170677,
                51.190029673478335,
                70.87724582614395,
                62.81680164723068,
                71.15831329606891,
                75.26475547736815],
        'BBB': [8.139767300098688,
                6.379246258316986,
                2.1397876745729327,
                6.313706149780265,
                15.098701765214418,
                7.7490382079431805,
                5.4726797054267635,
                10.405559742857772,
                2.986876875013502,
                4.669670292672878,
                7.534593039764434,
                2.787635835876732,
                12.118937055028901,
                11.702122297877466,
                2.8311461815551,
                7.501054558928145,
                3.6628575569337225,
                8.239654014974363,
                4.954066297839982,
                2.083447797206059,
                2.921473222095478,
                5.137534178496049,
                7.4582543454771635,
                2.487862346261772,
                3.531719837796348,
                4.045284649360511,
                1.7491675845137844,
                3.3466232150159514,
                4.840143341535361,
                4.9917899774011145,
                2.9899770578389937,
                1.4854231702920762,
                1.2255755891115165,
                4.217472534148868,
                2.243957753288968,
                0.4403110214968118,
                3.0835689089384903,
                1.358814085474778,
                1.32401918006161,
                1.0346393179145001,
                3.120564972772968,
                3.491203517758961,
                3.712850268944131,
                4.988604699054436,
                2.0553956517902305,
                4.202924464452326,
                5.646048112953468,
                3.134292131741094,
                2.3498068682642175,
                3.9067051131274053,
                3.468626646538365,
                3.0846532069075083,
                3.173765652430829,
                4.656468001242938,
                4.623212759956554,
                4.605090488799595,
                1.280748354179071,
                4.3397422194240445,
                4.245698908677754,
                3.8753027881689577],
        'CCC': [20.33619640167309,
                21.839053421519523,
                12.984387113073856,
                17.037439813468634,
                30.605229365542254,
                20.129919416080906,
                12.520100978849971,
                18.799694232825843,
                13.031657582957715,
                34.57799846816344,
                31.48451858867735,
                21.398982791901407,
                13.880594036442448,
                16.359987287525573,
                9.576664332282931,
                18.982371743610287,
                13.345619464746417,
                11.382398022766575,
                20.007435257759017,
                7.9718626130888595,
                14.16661151855533,
                24.714673934208427,
                14.467953604503977,
                8.031869997453756,
                14.341776606694575,
                3.9418839554092053,
                4.564235327892936,
                7.486700473316088,
                17.59347697991627,
                12.473576787207497,
                15.04802964216387,
                10.192898968388441,
                3.624050090172247,
                11.076907165314589,
                20.735698860060626,
                11.139008999260989,
                22.53908395429051,
                9.363077384109896,
                6.03765386981592,
                7.858175963833991,
                13.756148766416365,
                12.991557327103815,
                14.902961334387957,
                12.92833522977923,
                24.240163453474683,
                7.470141718699687,
                6.107220998859199,
                7.9960017545868425,
                11.332971437039006,
                17.353084244155685,
                21.790791062441144,
                11.267577893518558,
                26.875857820099007,
                28.33963149477056,
                12.295962219956854,
                8.008082923230933,
                11.498047450042755,
                11.76844418653792,
                10.034094521997764,
                9.1514947839658],
        'DDD': [31.070222323496406,
                22.39069553159074,
                15.20599254915021,
                11.433062868932623,
                16.63054694914773,
                17.73999088605368,
                11.536109040385249,
                5.94744735263699,
                8.215867704420134,
                4.581166834727733,
                13.661244663404798,
                18.11867720632882,
                18.918353106383204,
                2.148206358192932,
                2.129410525603654,
                5.175257751107644,
                10.375251539124092,
                14.424937621863728,
                11.772293710124755,
                8.517816424911617,
                10.006216206400245,
                10.416508463177399,
                12.89717856168254,
                2.406021221902175,
                20.90493365194458,
                6.47207286470695,
                6.725649596411921,
                10.611713116209543,
                13.269476983765664,
                6.829261074943621,
                3.838434899630019,
                6.18507459115281,
                2.123947386164871,
                7.517018945207147,
                8.06897785261463,
                3.1154100903667428,
                9.820284993268954,
                7.9154055109322465,
                4.907888529545782,
                9.080783383593868,
                16.237142357324924,
                12.105998348145983,
                11.276331206371859,
                14.566789325570584,
                8.095837208666254,
                31.809784213595332,
                4.449284501526217,
                4.60115379026931,
                10.00211500817693,
                9.970006959423198,
                7.297299021246675,
                6.308433390618397,
                6.519174687365989,
                7.464980124896396,
                6.143702102581181,
                11.470405217149835,
                10.742121711138521,
                8.28345856640664,
                6.051668845604065,
                3.0236137587073757],
        'EEE': [1.905312619025266,
                1.508615475638359,
                1.5144906020358848,
                3.7129579619803543,
                3.969022754219645,
                5.846388455175859,
                4.806725478330563,
                3.0997337417149606,
                2.3812000701467135,
                3.232288993513746,
                2.495291068737484,
                3.3049396374412248,
                1.4991101051932496,
                1.3972568989086704,
                1.300419913633468,
                4.4854324561116625,
                2.278505504418036,
                3.2668163251214404,
                3.378128590527836,
                2.295955054159599,
                4.279948757941389,
                4.361435962058709,
                2.9785614652010297,
                0.7904443052775995,
                9.881493651844071,
                2.1654982104236375,
                2.487593615611728,
                8.654151321228905,
                2.6928469132045016,
                4.399264158891095,
                8.041776030490281,
                2.4059682971238145,
                1.4876397054355845,
                5.547905672493609,
                7.290249721864706,
                3.048220024855806,
                4.246335920918918,
                9.382396127074584,
                5.046769737500849,
                6.715388617357094,
                5.688932277466472,
                8.66045301040564,
                3.317153951903546,
                5.36844564972132,
                1.8222360398536301,
                5.028298589246329,
                5.437604978634036,
                7.770611578553886,
                4.929525904497059,
                5.693375496236237,
                3.4575641074691306,
                1.6911828946664809,
                4.0572910223095,
                5.394409600446887,
                11.61615955606676,
                13.620242330855644,
                1.0446260006821124,
                2.1788413865634495,
                1.9883885592363564,
                2.835818161097098],
        'FFF': [0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                14.005719028048984,
                25.802941929614015,
                10.043240244503219,
                14.885479536064869,
                14.99076207003063,
                18.574680414590883,
                13.229396729128446,
                13.377086334178953,
                3.4828090863247776,
                5.270241165983078,
                10.341074161225704,
                3.4935202035093234,
                4.829537964020513,
                7.375165079199482,
                5.7836137148547895,
                4.903330675320816,
                7.83665860459272,
                1.8025234144639548,
                11.291889894154314,
                6.094607058597666,
                7.986408019845895,
                5.5232303323366505,
                8.177747791310704,
                4.366037129787624,
                5.206753693605719,
                1.87468061662862,
                1.6607404595406488,
                6.506089290369925,
                5.568879933344677,
                1.5083032544691546,
                8.600861432871042,
                5.082503906682552,
                2.3062030825683615,
                7.191355638638881,
                12.81952998752914,
                5.0769150176586715,
                6.625999371619594,
                8.89162925149309,
                2.7596235315099236,
                4.641602012410844,
                8.07035542708865,
                1.453794632680936,
                2.9886514432763236,
                2.0350371068789914,
                3.828881690130035,
                4.385823310507942,
                2.772365990386064,
                6.796166343935865,
                3.8825397757433975,
                9.12145468824788,
                2.4329337225366556,
                8.013712864420087,
                3.9894513471148487,
                3.431229155470149],
        'GGG': [0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                5.520836486697661,
                1.0555964756182752,
                3.7012256307610416,
                1.5930646233191965,
                1.641244569644223,
                1.4332100100957361,
                2.1819699387981877,
                0.8140534357164931,
                1.942343195903382,
                1.6365264670296171,
                2.18393469693455,
                2.597402735618861,
                2.0329460452204486,
                1.9710028797802668,
                2.21247081209762,
                2.2489380030734702,
                1.805647560780391,
                1.980689682815763,
                2.4725798094719904,
                1.4962286490883918,
                3.040390121980568,
                6.300098767735419,
                3.24092832119379,
                2.310629034738645,
                3.822929474012491,
                3.508898519622578,
                1.616675541184667,
                3.4188933508197654,
                2.6061543393343722,
                2.4419073429522444,
                1.9567764828450604,
                2.5559125724925607,
                3.3207676731684526,
                3.0631888168454005,
                0.6835589382754452,
                1.9977803698961467,
                2.7260055609848433,
                2.301343974210285,
                3.1075433439884845,
                1.9846946782377872,
                2.1242769352769364,
                2.5989991294171886,
                2.5323845213002922,
                2.417785875222468]}
df = pd.DataFrame(data)
df['date'] = pd.to_datetime(df['date'])
df.set_index("date", inplace=True)
source = ColumnDataSource(df)
p = figure(x_range=df.index.tolist(),
           x_axis_type="datetime",
           tooltips=[("date", "@date"), ("name", "$name"), ("value", "@$name")])
p.vbar_stack(df.columns.to_list(), x='date', color=Bokeh8[:len(df.columns)], source=data, legend_label=df.columns.to_list())
show(p)

It gives error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[4], line 492
    490 df.set_index("date", inplace=True)
    491 source = ColumnDataSource(df)
--> 492 p = figure(x_range=df.index.tolist(),
    493            x_axis_type="datetime",
    494            tooltips=[("date", "@date"), ("name", "$name"), ("value", "@$name")])
    495 p.vbar_stack(df.columns.to_list(), x='date', color=Bokeh8[:len(df.columns)], source=data, legend_label=df.columns.to_list())
    496 show(p)

File Y:\python_projects\pybrokerProjects\.venv\Lib\site-packages\bokeh\plotting\_figure.py:191, in figure.__init__(self, *arg, **kw)
    190 def __init__(self, *arg, **kw) -> None:
--> 191     opts = FigureOptions(kw)
    193     names = self.properties()
    194     for name in kw.keys():

File Y:\python_projects\pybrokerProjects\.venv\Lib\site-packages\bokeh\util\options.py:75, in Options.__init__(self, kw)
     72     if k in kw:
     73         props[k] = kw.pop(k)
---> 75 super().__init__(**props)

File Y:\python_projects\pybrokerProjects\.venv\Lib\site-packages\bokeh\core\has_props.py:306, in HasProps.__init__(self, **properties)
    304     if value is Undefined or value is Intrinsic:
    305         continue
--> 306     setattr(self, name, value)
    308 initialized = set(properties.keys())
    309 for name in self.properties(_with_props=True): # avoid set[] for deterministic behavior

File Y:\python_projects\pybrokerProjects\.venv\Lib\site-packages\bokeh\core\has_props.py:338, in HasProps.__setattr__(self, name, value)
    336 properties = self.properties(_with_props=True)
    337 if name in properties:
--> 338     return super().__setattr__(name, value)
    340 descriptor = getattr(self.__class__, name, None)
    341 if isinstance(descriptor, property): # Python property

File Y:\python_projects\pybrokerProjects\.venv\Lib\site-packages\bokeh\core\property\descriptors.py:331, in PropertyDescriptor.__set__(self, obj, value, setter)
    328     class_name = obj.__class__.__name__
    329     raise RuntimeError(f"{class_name}.{self.name} is a readonly property")
--> 331 value = self.property.prepare_value(obj, self.name, value)
    332 old = self._get(obj)
    333 self._set(obj, old, value, setter=setter)

File Y:\python_projects\pybrokerProjects\.venv\Lib\site-packages\bokeh\core\property\bases.py:365, in Property.prepare_value(self, owner, name, value, hint)
    363 else:
    364     obj_repr = owner if isinstance(owner, HasProps) else owner.__name__
--> 365     raise ValueError(f"failed to validate {obj_repr}.{name}: {error}")
    367 if isinstance(owner, HasProps):
    368     obj = owner

ValueError: failed to validate FigureOptions(...).x_range: expected an element of either Instance(Range), Either(Tuple(Float, Float), Tuple(Datetime, Datetime), Tuple(TimeDelta, TimeDelta)), Seq(String), Object(Series) or Object(GroupBy), got [Timestamp('2019-08-21 00:00:00'), Timestamp('2019-09-02 00:00:00'), Timestamp('2019-10-08 00:00:00'), Timestamp('2019-11-01 00:00:00'), Timestamp('2019-12-02 00:00:00'), Timestamp('2020-01-02 00:00:00'), Timestamp('2020-02-03 00:00:00'), Timestamp('2020-03-02 00:00:00'), Timestamp('2020-04-01 00:00:00'), Timestamp('2020-05-06 00:00:00'), Timestamp('2020-06-01 00:00:00'), Timestamp('2020-07-01 00:00:00'), Timestamp('2020-08-03 00:00:00'), Timestamp('2020-09-01 00:00:00'), Timestamp('2020-10-09 00:00:00'), Timestamp('2020-11-02 00:00:00'), Timestamp('2020-12-01 00:00:00'), Timestamp('2021-01-04 00:00:00'), Timestamp('2021-02-01 00:00:00'), Timestamp('2021-03-01 00:00:00'), Timestamp('2021-04-01 00:00:00'), Timestamp('2021-05-06 00:00:00'), Timestamp('2021-06-01 00:00:00'), Timestamp('2021-07-01 00:00:00'), Timestamp('2021-08-02 00:00:00'), Timestamp('2021-09-01 00:00:00'), Timestamp('2021-10-08 00:00:00'), Timestamp('2021-11-01 00:00:00'), Timestamp('2021-12-01 00:00:00'), Timestamp('2022-01-04 00:00:00'), Timestamp('2022-02-07 00:00:00'), Timestamp('2022-03-01 00:00:00'), Timestamp('2022-04-01 00:00:00'), Timestamp('2022-05-05 00:00:00'), Timestamp('2022-06-01 00:00:00'), Timestamp('2022-07-01 00:00:00'), Timestamp('2022-08-01 00:00:00'), Timestamp('2022-09-01 00:00:00'), Timestamp('2022-10-10 00:00:00'), Timestamp('2022-11-01 00:00:00'), Timestamp('2022-12-01 00:00:00'), Timestamp('2023-01-03 00:00:00'), Timestamp('2023-02-01 00:00:00'), Timestamp('2023-03-01 00:00:00'), Timestamp('2023-04-03 00:00:00'), Timestamp('2023-05-04 00:00:00'), Timestamp('2023-06-01 00:00:00'), Timestamp('2023-07-03 00:00:00'), Timestamp('2023-08-01 00:00:00'), Timestamp('2023-09-01 00:00:00'), Timestamp('2023-10-09 00:00:00'), Timestamp('2023-11-01 00:00:00'), Timestamp('2023-12-01 00:00:00'), Timestamp('2024-01-02 00:00:00'), Timestamp('2024-02-01 00:00:00'), Timestamp('2024-03-01 00:00:00'), Timestamp('2024-04-01 00:00:00'), Timestamp('2024-05-06 00:00:00'), Timestamp('2024-06-03 00:00:00'), Timestamp('2024-07-01 00:00:00')]

Please emulate the short example I posted, specifically, you need to:

  • set x_axis_type="datetime"
  • not set the x_range value at all

Setting x_range to an explicit, enumerated list of values is only for categorical cases, which is not what you want. For numerical cases (which includes datetimes) either don’t set at all to get the default auto-ranging behavior, or set to a tuple of numeric values (start, end) to manually configure where the axis starts and ends.

I tried to remove x_range, but it gave me a “blank” result, like

from bokeh.io import output_notebook
from bokeh.models import ColumnDataSource
from bokeh.palettes import Bokeh8
from bokeh.plotting import figure, show

output_notebook()
data = {'date': ['2019-08-21',
                 '2019-09-02',
                 '2019-10-08',
                 '2019-11-01',
                 '2019-12-02',
                 '2020-01-02',
                 '2020-02-03',
                 '2020-03-02',
                 '2020-04-01',
                 '2020-05-06',
                 '2020-06-01',
                 '2020-07-01',
                 '2020-08-03',
                 '2020-09-01',
                 '2020-10-09',
                 '2020-11-02',
                 '2020-12-01',
                 '2021-01-04',
                 '2021-02-01',
                 '2021-03-01',
                 '2021-04-01',
                 '2021-05-06',
                 '2021-06-01',
                 '2021-07-01',
                 '2021-08-02',
                 '2021-09-01',
                 '2021-10-08',
                 '2021-11-01',
                 '2021-12-01',
                 '2022-01-04',
                 '2022-02-07',
                 '2022-03-01',
                 '2022-04-01',
                 '2022-05-05',
                 '2022-06-01',
                 '2022-07-01',
                 '2022-08-01',
                 '2022-09-01',
                 '2022-10-10',
                 '2022-11-01',
                 '2022-12-01',
                 '2023-01-03',
                 '2023-02-01',
                 '2023-03-01',
                 '2023-04-03',
                 '2023-05-04',
                 '2023-06-01',
                 '2023-07-03',
                 '2023-08-01',
                 '2023-09-01',
                 '2023-10-09',
                 '2023-11-01',
                 '2023-12-01',
                 '2024-01-02',
                 '2024-02-01',
                 '2024-03-01',
                 '2024-04-01',
                 '2024-05-06',
                 '2024-06-03',
                 '2024-07-01'],
        'AAA': [38.548501355706556,
                47.88238931293438,
                68.1553420611671,
                61.50283320583812,
                33.696499165875956,
                48.53466303474638,
                51.65866576895849,
                35.94462300035041,
                63.34115752295874,
                38.053395874857316,
                29.833590569385297,
                35.81508411386094,
                40.35360896782375,
                55.01534082331641,
                80.67954996060007,
                58.58564232425918,
                54.47585528685438,
                58.13707733614629,
                51.357312548966846,
                70.16268840811519,
                61.20089201050857,
                49.03330677664287,
                52.17942347974438,
                83.66722527892423,
                38.105843161662726,
                75.64412679447241,
                74.30301115878919,
                61.780178806274,
                51.393361945047054,
                64.96906799198881,
                62.66255786417349,
                75.60701635334077,
                88.07239920879474,
                63.153916709650105,
                53.61965606935439,
                79.2525179604621,
                48.669474667731514,
                60.597704217990525,
                77.13653727931367,
                65.80902804392302,
                44.55475216447764,
                54.16497425930436,
                58.548028325588255,
                49.83730249356159,
                58.4205897753709,
                44.405341658643245,
                68.33270949809335,
                72.48823353967538,
                65.07616166557801,
                57.97860226333309,
                59.4732785338992,
                71.26454893388495,
                53.87553926642376,
                45.04700046049706,
                58.33088024170677,
                51.190029673478335,
                70.87724582614395,
                62.81680164723068,
                71.15831329606891,
                75.26475547736815],
        'BBB': [8.139767300098688,
                6.379246258316986,
                2.1397876745729327,
                6.313706149780265,
                15.098701765214418,
                7.7490382079431805,
                5.4726797054267635,
                10.405559742857772,
                2.986876875013502,
                4.669670292672878,
                7.534593039764434,
                2.787635835876732,
                12.118937055028901,
                11.702122297877466,
                2.8311461815551,
                7.501054558928145,
                3.6628575569337225,
                8.239654014974363,
                4.954066297839982,
                2.083447797206059,
                2.921473222095478,
                5.137534178496049,
                7.4582543454771635,
                2.487862346261772,
                3.531719837796348,
                4.045284649360511,
                1.7491675845137844,
                3.3466232150159514,
                4.840143341535361,
                4.9917899774011145,
                2.9899770578389937,
                1.4854231702920762,
                1.2255755891115165,
                4.217472534148868,
                2.243957753288968,
                0.4403110214968118,
                3.0835689089384903,
                1.358814085474778,
                1.32401918006161,
                1.0346393179145001,
                3.120564972772968,
                3.491203517758961,
                3.712850268944131,
                4.988604699054436,
                2.0553956517902305,
                4.202924464452326,
                5.646048112953468,
                3.134292131741094,
                2.3498068682642175,
                3.9067051131274053,
                3.468626646538365,
                3.0846532069075083,
                3.173765652430829,
                4.656468001242938,
                4.623212759956554,
                4.605090488799595,
                1.280748354179071,
                4.3397422194240445,
                4.245698908677754,
                3.8753027881689577],
        'CCC': [20.33619640167309,
                21.839053421519523,
                12.984387113073856,
                17.037439813468634,
                30.605229365542254,
                20.129919416080906,
                12.520100978849971,
                18.799694232825843,
                13.031657582957715,
                34.57799846816344,
                31.48451858867735,
                21.398982791901407,
                13.880594036442448,
                16.359987287525573,
                9.576664332282931,
                18.982371743610287,
                13.345619464746417,
                11.382398022766575,
                20.007435257759017,
                7.9718626130888595,
                14.16661151855533,
                24.714673934208427,
                14.467953604503977,
                8.031869997453756,
                14.341776606694575,
                3.9418839554092053,
                4.564235327892936,
                7.486700473316088,
                17.59347697991627,
                12.473576787207497,
                15.04802964216387,
                10.192898968388441,
                3.624050090172247,
                11.076907165314589,
                20.735698860060626,
                11.139008999260989,
                22.53908395429051,
                9.363077384109896,
                6.03765386981592,
                7.858175963833991,
                13.756148766416365,
                12.991557327103815,
                14.902961334387957,
                12.92833522977923,
                24.240163453474683,
                7.470141718699687,
                6.107220998859199,
                7.9960017545868425,
                11.332971437039006,
                17.353084244155685,
                21.790791062441144,
                11.267577893518558,
                26.875857820099007,
                28.33963149477056,
                12.295962219956854,
                8.008082923230933,
                11.498047450042755,
                11.76844418653792,
                10.034094521997764,
                9.1514947839658],
        'DDD': [31.070222323496406,
                22.39069553159074,
                15.20599254915021,
                11.433062868932623,
                16.63054694914773,
                17.73999088605368,
                11.536109040385249,
                5.94744735263699,
                8.215867704420134,
                4.581166834727733,
                13.661244663404798,
                18.11867720632882,
                18.918353106383204,
                2.148206358192932,
                2.129410525603654,
                5.175257751107644,
                10.375251539124092,
                14.424937621863728,
                11.772293710124755,
                8.517816424911617,
                10.006216206400245,
                10.416508463177399,
                12.89717856168254,
                2.406021221902175,
                20.90493365194458,
                6.47207286470695,
                6.725649596411921,
                10.611713116209543,
                13.269476983765664,
                6.829261074943621,
                3.838434899630019,
                6.18507459115281,
                2.123947386164871,
                7.517018945207147,
                8.06897785261463,
                3.1154100903667428,
                9.820284993268954,
                7.9154055109322465,
                4.907888529545782,
                9.080783383593868,
                16.237142357324924,
                12.105998348145983,
                11.276331206371859,
                14.566789325570584,
                8.095837208666254,
                31.809784213595332,
                4.449284501526217,
                4.60115379026931,
                10.00211500817693,
                9.970006959423198,
                7.297299021246675,
                6.308433390618397,
                6.519174687365989,
                7.464980124896396,
                6.143702102581181,
                11.470405217149835,
                10.742121711138521,
                8.28345856640664,
                6.051668845604065,
                3.0236137587073757],
        'EEE': [1.905312619025266,
                1.508615475638359,
                1.5144906020358848,
                3.7129579619803543,
                3.969022754219645,
                5.846388455175859,
                4.806725478330563,
                3.0997337417149606,
                2.3812000701467135,
                3.232288993513746,
                2.495291068737484,
                3.3049396374412248,
                1.4991101051932496,
                1.3972568989086704,
                1.300419913633468,
                4.4854324561116625,
                2.278505504418036,
                3.2668163251214404,
                3.378128590527836,
                2.295955054159599,
                4.279948757941389,
                4.361435962058709,
                2.9785614652010297,
                0.7904443052775995,
                9.881493651844071,
                2.1654982104236375,
                2.487593615611728,
                8.654151321228905,
                2.6928469132045016,
                4.399264158891095,
                8.041776030490281,
                2.4059682971238145,
                1.4876397054355845,
                5.547905672493609,
                7.290249721864706,
                3.048220024855806,
                4.246335920918918,
                9.382396127074584,
                5.046769737500849,
                6.715388617357094,
                5.688932277466472,
                8.66045301040564,
                3.317153951903546,
                5.36844564972132,
                1.8222360398536301,
                5.028298589246329,
                5.437604978634036,
                7.770611578553886,
                4.929525904497059,
                5.693375496236237,
                3.4575641074691306,
                1.6911828946664809,
                4.0572910223095,
                5.394409600446887,
                11.61615955606676,
                13.620242330855644,
                1.0446260006821124,
                2.1788413865634495,
                1.9883885592363564,
                2.835818161097098],
        'FFF': [0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                14.005719028048984,
                25.802941929614015,
                10.043240244503219,
                14.885479536064869,
                14.99076207003063,
                18.574680414590883,
                13.229396729128446,
                13.377086334178953,
                3.4828090863247776,
                5.270241165983078,
                10.341074161225704,
                3.4935202035093234,
                4.829537964020513,
                7.375165079199482,
                5.7836137148547895,
                4.903330675320816,
                7.83665860459272,
                1.8025234144639548,
                11.291889894154314,
                6.094607058597666,
                7.986408019845895,
                5.5232303323366505,
                8.177747791310704,
                4.366037129787624,
                5.206753693605719,
                1.87468061662862,
                1.6607404595406488,
                6.506089290369925,
                5.568879933344677,
                1.5083032544691546,
                8.600861432871042,
                5.082503906682552,
                2.3062030825683615,
                7.191355638638881,
                12.81952998752914,
                5.0769150176586715,
                6.625999371619594,
                8.89162925149309,
                2.7596235315099236,
                4.641602012410844,
                8.07035542708865,
                1.453794632680936,
                2.9886514432763236,
                2.0350371068789914,
                3.828881690130035,
                4.385823310507942,
                2.772365990386064,
                6.796166343935865,
                3.8825397757433975,
                9.12145468824788,
                2.4329337225366556,
                8.013712864420087,
                3.9894513471148487,
                3.431229155470149],
        'GGG': [0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                5.520836486697661,
                1.0555964756182752,
                3.7012256307610416,
                1.5930646233191965,
                1.641244569644223,
                1.4332100100957361,
                2.1819699387981877,
                0.8140534357164931,
                1.942343195903382,
                1.6365264670296171,
                2.18393469693455,
                2.597402735618861,
                2.0329460452204486,
                1.9710028797802668,
                2.21247081209762,
                2.2489380030734702,
                1.805647560780391,
                1.980689682815763,
                2.4725798094719904,
                1.4962286490883918,
                3.040390121980568,
                6.300098767735419,
                3.24092832119379,
                2.310629034738645,
                3.822929474012491,
                3.508898519622578,
                1.616675541184667,
                3.4188933508197654,
                2.6061543393343722,
                2.4419073429522444,
                1.9567764828450604,
                2.5559125724925607,
                3.3207676731684526,
                3.0631888168454005,
                0.6835589382754452,
                1.9977803698961467,
                2.7260055609848433,
                2.301343974210285,
                3.1075433439884845,
                1.9846946782377872,
                2.1242769352769364,
                2.5989991294171886,
                2.5323845213002922,
                2.417785875222468]}
df = pd.DataFrame(data)
df['date'] = pd.to_datetime(df['date'])
df.set_index("date", inplace=True)
source = ColumnDataSource(df)
p = figure(
    x_axis_type="datetime",
    tooltips=[("date", "@date"), ("name", "$name"), ("value", "@$name")])
p.vbar_stack(df.columns.to_list(), x='date', color=Bokeh8[:len(df.columns)], source=data, legend_label=df.columns.to_list())
show(p)

I’m new to here, the short example you mentioned in previous post, I didn’t find the link, would you provide? Much appreciate.

I just re-read things and I am not actually sure I understand what you are trying to achieve. Is that actual issue just the axis crowding that you mentioned? Then you can do this with your original code:

df = pd.DataFrame(data)
df.set_index("date", inplace=True)

p = figure(x_range=df.index.tolist(),
           # x_axis_type="datetime",
           tooltips=[("date", "@date"), ("name", "$name"), ("value", "@$name")], sizing_mode="stretch_width")

# NEW: this had a mistake, need source=df here
p.vbar_stack(df.columns.to_list(), x='date', color=Bokeh8[:len(df.columns)], source=df, legend_label=df.columns.to_list())

# NEW: rotate the axis labels to not crowd
p.xaxis.major_label_orientation = 0.8

show(p)

That generates this output with legible labels:

But you also mention something about zooming and in retrospect I am not sure what your expectations here are. What details? Do you mean you want the axis tick values to change based on zoom level. Then you will have to use datetime values and axis as I mentioned above, like this:

df = pd.DataFrame(data)
df['date'] = pd.to_datetime(df['date'])
df.set_index("date", inplace=True)

p = figure(x_axis_type="datetime",
           tooltips=[("date", "@date"), ("name", "$name"), ("value", "@$name")], sizing_mode="stretch_width")
p.vbar_stack(df.columns.to_list(), x='date', color=Bokeh8[:len(df.columns)], source=df, legend_label=df.columns.to_list())

p.xaxis.major_label_orientation = 0.8
show(p)

That generates this output which has an actual datetime axis that changes when zoomed:

Note that the on this scale the default bar with is very thin. The units of a datetgime axis a milliseconds since epoch so you will need to set the bar widths to be a meaningful on this scale. You could either use a large number of millesconds, or a python timedelta e.g.

width=datetime.timedelta(weeks=1),

With that it looks like the dates are not evenly spaced?

What to do about this is up to your needs, you could make the bars have different widths, or all the same but different spacing as above. Or maybe you want the first (categorical) version above, but then you will have to give up the axis zooming behavior, since categorical values have not scale relation betweeen them. It not actually clear what your actual requirements are.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.