Color coded table plot based on the values in a pandas dataframe










1















I have a dataframe df containing string values



a b c d



b c d a



I would like to produce a pdf plot based on the data in the df, with 4 cols and 2 rows, where each cell in the table plot has a color depending on the value in the df, a=blue, b=red, c=yellow, d=green.



Like this



Thanks in advance!










share|improve this question
























  • By "table" you mean a plt.table or a subplot grid? I think you would benefit from describing much more in detail the desired output and also what other resources have not helped you achieve what you want.

    – ImportanceOfBeingErnest
    Nov 13 '18 at 13:20











  • @ImportanceOfBeingErnest I added a picture of what I am looking for :)

    – peter_parker
    Nov 13 '18 at 13:36











  • I would probably map the characters to numbers first, then plot an imshow plot of the data. You will need a custom colormap with the 4 colors in it.

    – ImportanceOfBeingErnest
    Nov 13 '18 at 13:40















1















I have a dataframe df containing string values



a b c d



b c d a



I would like to produce a pdf plot based on the data in the df, with 4 cols and 2 rows, where each cell in the table plot has a color depending on the value in the df, a=blue, b=red, c=yellow, d=green.



Like this



Thanks in advance!










share|improve this question
























  • By "table" you mean a plt.table or a subplot grid? I think you would benefit from describing much more in detail the desired output and also what other resources have not helped you achieve what you want.

    – ImportanceOfBeingErnest
    Nov 13 '18 at 13:20











  • @ImportanceOfBeingErnest I added a picture of what I am looking for :)

    – peter_parker
    Nov 13 '18 at 13:36











  • I would probably map the characters to numbers first, then plot an imshow plot of the data. You will need a custom colormap with the 4 colors in it.

    – ImportanceOfBeingErnest
    Nov 13 '18 at 13:40













1












1








1








I have a dataframe df containing string values



a b c d



b c d a



I would like to produce a pdf plot based on the data in the df, with 4 cols and 2 rows, where each cell in the table plot has a color depending on the value in the df, a=blue, b=red, c=yellow, d=green.



Like this



Thanks in advance!










share|improve this question
















I have a dataframe df containing string values



a b c d



b c d a



I would like to produce a pdf plot based on the data in the df, with 4 cols and 2 rows, where each cell in the table plot has a color depending on the value in the df, a=blue, b=red, c=yellow, d=green.



Like this



Thanks in advance!







python pandas dataframe matplotlib






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 18:08









Joe

6,00921429




6,00921429










asked Nov 13 '18 at 12:55









peter_parkerpeter_parker

174




174












  • By "table" you mean a plt.table or a subplot grid? I think you would benefit from describing much more in detail the desired output and also what other resources have not helped you achieve what you want.

    – ImportanceOfBeingErnest
    Nov 13 '18 at 13:20











  • @ImportanceOfBeingErnest I added a picture of what I am looking for :)

    – peter_parker
    Nov 13 '18 at 13:36











  • I would probably map the characters to numbers first, then plot an imshow plot of the data. You will need a custom colormap with the 4 colors in it.

    – ImportanceOfBeingErnest
    Nov 13 '18 at 13:40

















  • By "table" you mean a plt.table or a subplot grid? I think you would benefit from describing much more in detail the desired output and also what other resources have not helped you achieve what you want.

    – ImportanceOfBeingErnest
    Nov 13 '18 at 13:20











  • @ImportanceOfBeingErnest I added a picture of what I am looking for :)

    – peter_parker
    Nov 13 '18 at 13:36











  • I would probably map the characters to numbers first, then plot an imshow plot of the data. You will need a custom colormap with the 4 colors in it.

    – ImportanceOfBeingErnest
    Nov 13 '18 at 13:40
















By "table" you mean a plt.table or a subplot grid? I think you would benefit from describing much more in detail the desired output and also what other resources have not helped you achieve what you want.

– ImportanceOfBeingErnest
Nov 13 '18 at 13:20





By "table" you mean a plt.table or a subplot grid? I think you would benefit from describing much more in detail the desired output and also what other resources have not helped you achieve what you want.

– ImportanceOfBeingErnest
Nov 13 '18 at 13:20













@ImportanceOfBeingErnest I added a picture of what I am looking for :)

– peter_parker
Nov 13 '18 at 13:36





@ImportanceOfBeingErnest I added a picture of what I am looking for :)

– peter_parker
Nov 13 '18 at 13:36













I would probably map the characters to numbers first, then plot an imshow plot of the data. You will need a custom colormap with the 4 colors in it.

– ImportanceOfBeingErnest
Nov 13 '18 at 13:40





I would probably map the characters to numbers first, then plot an imshow plot of the data. You will need a custom colormap with the 4 colors in it.

– ImportanceOfBeingErnest
Nov 13 '18 at 13:40












1 Answer
1






active

oldest

votes


















1














You can do in this way:



from matplotlib import colors as c

color_map = 'a':1,'b':2,'c':3, 'd':4
cMap = c.ListedColormap(['g','b','y','r'])
df = df.replace(color_map)
fig, ax = plt.subplots()
ax.pcolor(df,cmap=cMap)
plt.show()


enter image description here



And If you want to remove the ticks, add plt.xticks() and plt.yticks()






share|improve this answer
























    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%2f53281495%2fcolor-coded-table-plot-based-on-the-values-in-a-pandas-dataframe%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can do in this way:



    from matplotlib import colors as c

    color_map = 'a':1,'b':2,'c':3, 'd':4
    cMap = c.ListedColormap(['g','b','y','r'])
    df = df.replace(color_map)
    fig, ax = plt.subplots()
    ax.pcolor(df,cmap=cMap)
    plt.show()


    enter image description here



    And If you want to remove the ticks, add plt.xticks() and plt.yticks()






    share|improve this answer





























      1














      You can do in this way:



      from matplotlib import colors as c

      color_map = 'a':1,'b':2,'c':3, 'd':4
      cMap = c.ListedColormap(['g','b','y','r'])
      df = df.replace(color_map)
      fig, ax = plt.subplots()
      ax.pcolor(df,cmap=cMap)
      plt.show()


      enter image description here



      And If you want to remove the ticks, add plt.xticks() and plt.yticks()






      share|improve this answer



























        1












        1








        1







        You can do in this way:



        from matplotlib import colors as c

        color_map = 'a':1,'b':2,'c':3, 'd':4
        cMap = c.ListedColormap(['g','b','y','r'])
        df = df.replace(color_map)
        fig, ax = plt.subplots()
        ax.pcolor(df,cmap=cMap)
        plt.show()


        enter image description here



        And If you want to remove the ticks, add plt.xticks() and plt.yticks()






        share|improve this answer















        You can do in this way:



        from matplotlib import colors as c

        color_map = 'a':1,'b':2,'c':3, 'd':4
        cMap = c.ListedColormap(['g','b','y','r'])
        df = df.replace(color_map)
        fig, ax = plt.subplots()
        ax.pcolor(df,cmap=cMap)
        plt.show()


        enter image description here



        And If you want to remove the ticks, add plt.xticks() and plt.yticks()







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 14:43

























        answered Nov 13 '18 at 14:27









        JoeJoe

        6,00921429




        6,00921429





























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281495%2fcolor-coded-table-plot-based-on-the-values-in-a-pandas-dataframe%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

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus