How to set limits of Y-axes in countplot?












0












$begingroup$


df in my program happens to be a dataframe with these columns :



df.columns
'''output : Index(['lat', 'lng', 'desc', 'zip', 'title', 'timeStamp', 'twp', 'addr', 'e',
'reason'],
dtype='object')'''


When I execute this piece of code:



sns.countplot(x = df['reason'], data=df)
# output is the plot below


countplot 1



but if i slightly tweak my code like this :



p = df['reason'].value_counts()
k = pd.DataFrame({'causes':p.index,'freq':p.values})
sns.countplot(x = k['causes'], data = k)


countplot 2



So essentially I just stored the 'reasons' column values and its frequencies as a series in p and then converted them to another dataframe k but this new countplot doesn't have the right range of Y-axis for the given values.



My doubts happen to be :




  • Can we set of Y-axis in the second countplot in its appropriate limits

  • Why the does second countplot differ from the first one when i just separated the specific column i wanted to graph and plotted it separately ?










share|improve this question







New contributor




Arnav Das is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    0












    $begingroup$


    df in my program happens to be a dataframe with these columns :



    df.columns
    '''output : Index(['lat', 'lng', 'desc', 'zip', 'title', 'timeStamp', 'twp', 'addr', 'e',
    'reason'],
    dtype='object')'''


    When I execute this piece of code:



    sns.countplot(x = df['reason'], data=df)
    # output is the plot below


    countplot 1



    but if i slightly tweak my code like this :



    p = df['reason'].value_counts()
    k = pd.DataFrame({'causes':p.index,'freq':p.values})
    sns.countplot(x = k['causes'], data = k)


    countplot 2



    So essentially I just stored the 'reasons' column values and its frequencies as a series in p and then converted them to another dataframe k but this new countplot doesn't have the right range of Y-axis for the given values.



    My doubts happen to be :




    • Can we set of Y-axis in the second countplot in its appropriate limits

    • Why the does second countplot differ from the first one when i just separated the specific column i wanted to graph and plotted it separately ?










    share|improve this question







    New contributor




    Arnav Das is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      0












      0








      0





      $begingroup$


      df in my program happens to be a dataframe with these columns :



      df.columns
      '''output : Index(['lat', 'lng', 'desc', 'zip', 'title', 'timeStamp', 'twp', 'addr', 'e',
      'reason'],
      dtype='object')'''


      When I execute this piece of code:



      sns.countplot(x = df['reason'], data=df)
      # output is the plot below


      countplot 1



      but if i slightly tweak my code like this :



      p = df['reason'].value_counts()
      k = pd.DataFrame({'causes':p.index,'freq':p.values})
      sns.countplot(x = k['causes'], data = k)


      countplot 2



      So essentially I just stored the 'reasons' column values and its frequencies as a series in p and then converted them to another dataframe k but this new countplot doesn't have the right range of Y-axis for the given values.



      My doubts happen to be :




      • Can we set of Y-axis in the second countplot in its appropriate limits

      • Why the does second countplot differ from the first one when i just separated the specific column i wanted to graph and plotted it separately ?










      share|improve this question







      New contributor




      Arnav Das is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      df in my program happens to be a dataframe with these columns :



      df.columns
      '''output : Index(['lat', 'lng', 'desc', 'zip', 'title', 'timeStamp', 'twp', 'addr', 'e',
      'reason'],
      dtype='object')'''


      When I execute this piece of code:



      sns.countplot(x = df['reason'], data=df)
      # output is the plot below


      countplot 1



      but if i slightly tweak my code like this :



      p = df['reason'].value_counts()
      k = pd.DataFrame({'causes':p.index,'freq':p.values})
      sns.countplot(x = k['causes'], data = k)


      countplot 2



      So essentially I just stored the 'reasons' column values and its frequencies as a series in p and then converted them to another dataframe k but this new countplot doesn't have the right range of Y-axis for the given values.



      My doubts happen to be :




      • Can we set of Y-axis in the second countplot in its appropriate limits

      • Why the does second countplot differ from the first one when i just separated the specific column i wanted to graph and plotted it separately ?







      python dataframe matplotlib seaborn






      share|improve this question







      New contributor




      Arnav Das is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Arnav Das is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Arnav Das is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      Arnav DasArnav Das

      1033




      1033




      New contributor




      Arnav Das is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Arnav Das is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Arnav Das is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          1












          $begingroup$

          Countplot from seaborn will not work as you expect. When you calculate the frequencies, you want to plot the values in p.values as they appear. Countplot will take a dataframe where labels are not aggregated and then count each one of them, as it did in the first case.



          So countplot will be appropriate for the case where your dataframe looks like:



          index | reason |
          0 EMS
          1 EMS
          2 Traffic
          3 Fire
          4 Fire
          5 EMS
          6 Traffic
          ...


          In the second case you already have your frequencies:



          index | reason |
          EMS 10
          Traffic 21
          Fire 15


          Then count plot will just count the lines and it will be one for each, that is why your plot looks like that.



          To solve your problem you could just plot using .plot from pandas:



          df['reason'].value_counts(normalize=True).plot(kind='bar')


          Where the parameter normalize=True will show normalized frequencies instead of raw count values.






          share|improve this answer









          $endgroup$













          • $begingroup$
            wow I had never thought that way, so countplot counts the lines only
            $endgroup$
            – Arnav Das
            2 days ago






          • 1




            $begingroup$
            Yes, thats right. So in the second case you should use barplot only.
            $endgroup$
            – Victor Oliveira
            2 days ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "557"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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
          });


          }
          });






          Arnav Das is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f47036%2fhow-to-set-limits-of-y-axes-in-countplot%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












          $begingroup$

          Countplot from seaborn will not work as you expect. When you calculate the frequencies, you want to plot the values in p.values as they appear. Countplot will take a dataframe where labels are not aggregated and then count each one of them, as it did in the first case.



          So countplot will be appropriate for the case where your dataframe looks like:



          index | reason |
          0 EMS
          1 EMS
          2 Traffic
          3 Fire
          4 Fire
          5 EMS
          6 Traffic
          ...


          In the second case you already have your frequencies:



          index | reason |
          EMS 10
          Traffic 21
          Fire 15


          Then count plot will just count the lines and it will be one for each, that is why your plot looks like that.



          To solve your problem you could just plot using .plot from pandas:



          df['reason'].value_counts(normalize=True).plot(kind='bar')


          Where the parameter normalize=True will show normalized frequencies instead of raw count values.






          share|improve this answer









          $endgroup$













          • $begingroup$
            wow I had never thought that way, so countplot counts the lines only
            $endgroup$
            – Arnav Das
            2 days ago






          • 1




            $begingroup$
            Yes, thats right. So in the second case you should use barplot only.
            $endgroup$
            – Victor Oliveira
            2 days ago
















          1












          $begingroup$

          Countplot from seaborn will not work as you expect. When you calculate the frequencies, you want to plot the values in p.values as they appear. Countplot will take a dataframe where labels are not aggregated and then count each one of them, as it did in the first case.



          So countplot will be appropriate for the case where your dataframe looks like:



          index | reason |
          0 EMS
          1 EMS
          2 Traffic
          3 Fire
          4 Fire
          5 EMS
          6 Traffic
          ...


          In the second case you already have your frequencies:



          index | reason |
          EMS 10
          Traffic 21
          Fire 15


          Then count plot will just count the lines and it will be one for each, that is why your plot looks like that.



          To solve your problem you could just plot using .plot from pandas:



          df['reason'].value_counts(normalize=True).plot(kind='bar')


          Where the parameter normalize=True will show normalized frequencies instead of raw count values.






          share|improve this answer









          $endgroup$













          • $begingroup$
            wow I had never thought that way, so countplot counts the lines only
            $endgroup$
            – Arnav Das
            2 days ago






          • 1




            $begingroup$
            Yes, thats right. So in the second case you should use barplot only.
            $endgroup$
            – Victor Oliveira
            2 days ago














          1












          1








          1





          $begingroup$

          Countplot from seaborn will not work as you expect. When you calculate the frequencies, you want to plot the values in p.values as they appear. Countplot will take a dataframe where labels are not aggregated and then count each one of them, as it did in the first case.



          So countplot will be appropriate for the case where your dataframe looks like:



          index | reason |
          0 EMS
          1 EMS
          2 Traffic
          3 Fire
          4 Fire
          5 EMS
          6 Traffic
          ...


          In the second case you already have your frequencies:



          index | reason |
          EMS 10
          Traffic 21
          Fire 15


          Then count plot will just count the lines and it will be one for each, that is why your plot looks like that.



          To solve your problem you could just plot using .plot from pandas:



          df['reason'].value_counts(normalize=True).plot(kind='bar')


          Where the parameter normalize=True will show normalized frequencies instead of raw count values.






          share|improve this answer









          $endgroup$



          Countplot from seaborn will not work as you expect. When you calculate the frequencies, you want to plot the values in p.values as they appear. Countplot will take a dataframe where labels are not aggregated and then count each one of them, as it did in the first case.



          So countplot will be appropriate for the case where your dataframe looks like:



          index | reason |
          0 EMS
          1 EMS
          2 Traffic
          3 Fire
          4 Fire
          5 EMS
          6 Traffic
          ...


          In the second case you already have your frequencies:



          index | reason |
          EMS 10
          Traffic 21
          Fire 15


          Then count plot will just count the lines and it will be one for each, that is why your plot looks like that.



          To solve your problem you could just plot using .plot from pandas:



          df['reason'].value_counts(normalize=True).plot(kind='bar')


          Where the parameter normalize=True will show normalized frequencies instead of raw count values.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          Victor OliveiraVictor Oliveira

          1807




          1807












          • $begingroup$
            wow I had never thought that way, so countplot counts the lines only
            $endgroup$
            – Arnav Das
            2 days ago






          • 1




            $begingroup$
            Yes, thats right. So in the second case you should use barplot only.
            $endgroup$
            – Victor Oliveira
            2 days ago


















          • $begingroup$
            wow I had never thought that way, so countplot counts the lines only
            $endgroup$
            – Arnav Das
            2 days ago






          • 1




            $begingroup$
            Yes, thats right. So in the second case you should use barplot only.
            $endgroup$
            – Victor Oliveira
            2 days ago
















          $begingroup$
          wow I had never thought that way, so countplot counts the lines only
          $endgroup$
          – Arnav Das
          2 days ago




          $begingroup$
          wow I had never thought that way, so countplot counts the lines only
          $endgroup$
          – Arnav Das
          2 days ago




          1




          1




          $begingroup$
          Yes, thats right. So in the second case you should use barplot only.
          $endgroup$
          – Victor Oliveira
          2 days ago




          $begingroup$
          Yes, thats right. So in the second case you should use barplot only.
          $endgroup$
          – Victor Oliveira
          2 days ago










          Arnav Das is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Arnav Das is a new contributor. Be nice, and check out our Code of Conduct.













          Arnav Das is a new contributor. Be nice, and check out our Code of Conduct.












          Arnav Das is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Data Science Stack Exchange!


          • 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.


          Use MathJax to format equations. MathJax reference.


          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%2fdatascience.stackexchange.com%2fquestions%2f47036%2fhow-to-set-limits-of-y-axes-in-countplot%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

          Callistus I

          Tabula Rosettana

          How to label and detect the document text images