Bokeh vbar_stack bars don't re-sort after filtering the data source












0














I have a vbar_stack chart that I am updating with CheckboxGroup selections. When I change the accounts being selected, the data does change, but the chart doesn't re-sort. How can I make the accounts re-sort in descending order?



Right now, I am using this code for the CheckboxGroup onchange command. The custom_make_dataset function does re-sort the filtered dataset.



def custom_make_dataset(df, accounts):

remove_accounts = [i for i in df["acc"].unique() if i not in accounts]

new_df = df
.groupby(["acc","product"])["cost"]
.sum()
.reset_index()
.pivot(columns="acc", index="product", values="cost")

for account in remove_accounts:
new_df[account] = 0

new_df["Total"] = new_df.sum(axis=1)
new_df.sort_values(by="Total", ascending=False, inplace=True)
del new_df["Total"]

return ColumnDataSource(new_df)

def update_checkbox(attr, old, new):
accounts_to_plot = [account_selection.labels[i] for i in account_selection.active]
new_src = custom_make_dataset(df,accounts_to_plot)
source.data.update(new_src.data)

account_selection = CheckboxGroup(name="Customer Accounts", labels=initial_accounts, active = [i for i,j in enumerate(initial_accounts)])
account_selection.on_change('active', update_checkbox)


Full Selection
After 1 Account Selection










share|improve this question



























    0














    I have a vbar_stack chart that I am updating with CheckboxGroup selections. When I change the accounts being selected, the data does change, but the chart doesn't re-sort. How can I make the accounts re-sort in descending order?



    Right now, I am using this code for the CheckboxGroup onchange command. The custom_make_dataset function does re-sort the filtered dataset.



    def custom_make_dataset(df, accounts):

    remove_accounts = [i for i in df["acc"].unique() if i not in accounts]

    new_df = df
    .groupby(["acc","product"])["cost"]
    .sum()
    .reset_index()
    .pivot(columns="acc", index="product", values="cost")

    for account in remove_accounts:
    new_df[account] = 0

    new_df["Total"] = new_df.sum(axis=1)
    new_df.sort_values(by="Total", ascending=False, inplace=True)
    del new_df["Total"]

    return ColumnDataSource(new_df)

    def update_checkbox(attr, old, new):
    accounts_to_plot = [account_selection.labels[i] for i in account_selection.active]
    new_src = custom_make_dataset(df,accounts_to_plot)
    source.data.update(new_src.data)

    account_selection = CheckboxGroup(name="Customer Accounts", labels=initial_accounts, active = [i for i,j in enumerate(initial_accounts)])
    account_selection.on_change('active', update_checkbox)


    Full Selection
    After 1 Account Selection










    share|improve this question

























      0












      0








      0







      I have a vbar_stack chart that I am updating with CheckboxGroup selections. When I change the accounts being selected, the data does change, but the chart doesn't re-sort. How can I make the accounts re-sort in descending order?



      Right now, I am using this code for the CheckboxGroup onchange command. The custom_make_dataset function does re-sort the filtered dataset.



      def custom_make_dataset(df, accounts):

      remove_accounts = [i for i in df["acc"].unique() if i not in accounts]

      new_df = df
      .groupby(["acc","product"])["cost"]
      .sum()
      .reset_index()
      .pivot(columns="acc", index="product", values="cost")

      for account in remove_accounts:
      new_df[account] = 0

      new_df["Total"] = new_df.sum(axis=1)
      new_df.sort_values(by="Total", ascending=False, inplace=True)
      del new_df["Total"]

      return ColumnDataSource(new_df)

      def update_checkbox(attr, old, new):
      accounts_to_plot = [account_selection.labels[i] for i in account_selection.active]
      new_src = custom_make_dataset(df,accounts_to_plot)
      source.data.update(new_src.data)

      account_selection = CheckboxGroup(name="Customer Accounts", labels=initial_accounts, active = [i for i,j in enumerate(initial_accounts)])
      account_selection.on_change('active', update_checkbox)


      Full Selection
      After 1 Account Selection










      share|improve this question













      I have a vbar_stack chart that I am updating with CheckboxGroup selections. When I change the accounts being selected, the data does change, but the chart doesn't re-sort. How can I make the accounts re-sort in descending order?



      Right now, I am using this code for the CheckboxGroup onchange command. The custom_make_dataset function does re-sort the filtered dataset.



      def custom_make_dataset(df, accounts):

      remove_accounts = [i for i in df["acc"].unique() if i not in accounts]

      new_df = df
      .groupby(["acc","product"])["cost"]
      .sum()
      .reset_index()
      .pivot(columns="acc", index="product", values="cost")

      for account in remove_accounts:
      new_df[account] = 0

      new_df["Total"] = new_df.sum(axis=1)
      new_df.sort_values(by="Total", ascending=False, inplace=True)
      del new_df["Total"]

      return ColumnDataSource(new_df)

      def update_checkbox(attr, old, new):
      accounts_to_plot = [account_selection.labels[i] for i in account_selection.active]
      new_src = custom_make_dataset(df,accounts_to_plot)
      source.data.update(new_src.data)

      account_selection = CheckboxGroup(name="Customer Accounts", labels=initial_accounts, active = [i for i,j in enumerate(initial_accounts)])
      account_selection.on_change('active', update_checkbox)


      Full Selection
      After 1 Account Selection







      python widget bokeh






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 0:12









      Keri

      111




      111
























          2 Answers
          2






          active

          oldest

          votes


















          1














          I figured out the problem. While I was updating the data, I wasn't updating the x-axis itself. You can put something like this in the onchange command to update the plot x-axis.



          p.x_range.factors = (new_src.data['product'])






          share|improve this answer





























            0














            Maybe bokeh is using the dataframe index to determine on which x position the values are placed.



            Try resetting your new_df index before returning the new ColumnDataSource.
            new_df.reset_index(drop=True, inplace=True)






            share|improve this answer





















            • That didn't work. I also checked the ColumnDataSource using ColumnDataSource(new_df).data and the values are sorted correctly in the data store.
              – Keri
              Nov 14 '18 at 16:42













            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291327%2fbokeh-vbar-stack-bars-dont-re-sort-after-filtering-the-data-source%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            I figured out the problem. While I was updating the data, I wasn't updating the x-axis itself. You can put something like this in the onchange command to update the plot x-axis.



            p.x_range.factors = (new_src.data['product'])






            share|improve this answer


























              1














              I figured out the problem. While I was updating the data, I wasn't updating the x-axis itself. You can put something like this in the onchange command to update the plot x-axis.



              p.x_range.factors = (new_src.data['product'])






              share|improve this answer
























                1












                1








                1






                I figured out the problem. While I was updating the data, I wasn't updating the x-axis itself. You can put something like this in the onchange command to update the plot x-axis.



                p.x_range.factors = (new_src.data['product'])






                share|improve this answer












                I figured out the problem. While I was updating the data, I wasn't updating the x-axis itself. You can put something like this in the onchange command to update the plot x-axis.



                p.x_range.factors = (new_src.data['product'])







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 7:56









                Keri

                111




                111

























                    0














                    Maybe bokeh is using the dataframe index to determine on which x position the values are placed.



                    Try resetting your new_df index before returning the new ColumnDataSource.
                    new_df.reset_index(drop=True, inplace=True)






                    share|improve this answer





















                    • That didn't work. I also checked the ColumnDataSource using ColumnDataSource(new_df).data and the values are sorted correctly in the data store.
                      – Keri
                      Nov 14 '18 at 16:42


















                    0














                    Maybe bokeh is using the dataframe index to determine on which x position the values are placed.



                    Try resetting your new_df index before returning the new ColumnDataSource.
                    new_df.reset_index(drop=True, inplace=True)






                    share|improve this answer





















                    • That didn't work. I also checked the ColumnDataSource using ColumnDataSource(new_df).data and the values are sorted correctly in the data store.
                      – Keri
                      Nov 14 '18 at 16:42
















                    0












                    0








                    0






                    Maybe bokeh is using the dataframe index to determine on which x position the values are placed.



                    Try resetting your new_df index before returning the new ColumnDataSource.
                    new_df.reset_index(drop=True, inplace=True)






                    share|improve this answer












                    Maybe bokeh is using the dataframe index to determine on which x position the values are placed.



                    Try resetting your new_df index before returning the new ColumnDataSource.
                    new_df.reset_index(drop=True, inplace=True)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 '18 at 9:25









                    Pallie

                    3107




                    3107












                    • That didn't work. I also checked the ColumnDataSource using ColumnDataSource(new_df).data and the values are sorted correctly in the data store.
                      – Keri
                      Nov 14 '18 at 16:42




















                    • That didn't work. I also checked the ColumnDataSource using ColumnDataSource(new_df).data and the values are sorted correctly in the data store.
                      – Keri
                      Nov 14 '18 at 16:42


















                    That didn't work. I also checked the ColumnDataSource using ColumnDataSource(new_df).data and the values are sorted correctly in the data store.
                    – Keri
                    Nov 14 '18 at 16:42






                    That didn't work. I also checked the ColumnDataSource using ColumnDataSource(new_df).data and the values are sorted correctly in the data store.
                    – Keri
                    Nov 14 '18 at 16:42




















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291327%2fbokeh-vbar-stack-bars-dont-re-sort-after-filtering-the-data-source%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)