How to make sure that both x and y axes of plot are of equal sizes?












2















I want to make x and y axes be of equal lengths (i.e the plot minus the legend should be square ). I wish to plot the legend outside (I have already been able to put legend outside the box). The span of x axis in the data (x_max - x_min) is not the same as the span of y axis in the data (y_max - y_min).



This is the relevant part of the code that I have at the moment:



plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=15 )
plt.axis('equal')
plt.tight_layout()


The following link is an example of an output plot that I am getting : plot



How can I do this?










share|improve this question





























    2















    I want to make x and y axes be of equal lengths (i.e the plot minus the legend should be square ). I wish to plot the legend outside (I have already been able to put legend outside the box). The span of x axis in the data (x_max - x_min) is not the same as the span of y axis in the data (y_max - y_min).



    This is the relevant part of the code that I have at the moment:



    plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=15 )
    plt.axis('equal')
    plt.tight_layout()


    The following link is an example of an output plot that I am getting : plot



    How can I do this?










    share|improve this question



























      2












      2








      2


      1






      I want to make x and y axes be of equal lengths (i.e the plot minus the legend should be square ). I wish to plot the legend outside (I have already been able to put legend outside the box). The span of x axis in the data (x_max - x_min) is not the same as the span of y axis in the data (y_max - y_min).



      This is the relevant part of the code that I have at the moment:



      plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=15 )
      plt.axis('equal')
      plt.tight_layout()


      The following link is an example of an output plot that I am getting : plot



      How can I do this?










      share|improve this question
















      I want to make x and y axes be of equal lengths (i.e the plot minus the legend should be square ). I wish to plot the legend outside (I have already been able to put legend outside the box). The span of x axis in the data (x_max - x_min) is not the same as the span of y axis in the data (y_max - y_min).



      This is the relevant part of the code that I have at the moment:



      plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=15 )
      plt.axis('equal')
      plt.tight_layout()


      The following link is an example of an output plot that I am getting : plot



      How can I do this?







      python python-2.7 matplotlib






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 7 at 23:11







      user10853036

















      asked Jan 7 at 20:51









      user10853036user10853036

      134




      134
























          3 Answers
          3






          active

          oldest

          votes


















          4














          Would plt.axis('scaled') be what you're after? That would produce a square plot, if the data limits are of equal difference.



          If they are not, you could get a square plot by setting the aspect of the axes to the ratio of xlimits and ylimits.



          import numpy as np
          import matplotlib.pyplot as plt

          fig, (ax1, ax2) = plt.subplots(1,2)

          ax1.plot([-2.5, 2.5], [-4,13], "s-")
          ax1.axis("scaled")

          ax2.plot([-2.5, 2.5], [-4,13], "s-")
          ax2.set_aspect(np.diff(ax2.get_xlim())/np.diff(ax2.get_ylim()))

          plt.show()


          enter image description here






          share|improve this answer
























          • Thanks for your answer. Will it matter if the legend is outside of the plot? I will like to have the part of the plot which is without the legend be a square (I will like to have the legend besides this square plot). In my case, the data limits are not of equal difference.

            – user10853036
            Jan 7 at 21:51











          • It shouldn't matter if there is another subplot, or a legend next to the plot for this to work. I'd say try it out and see if you're happy with the result, else come back and explain any further problem.

            – ImportanceOfBeingErnest
            Jan 7 at 21:53











          • Thanks for your answer. I tried it. It works, but there is this one issue - the sides of the plot get cut (this is with me using plt.tight_layout(). Here is the plot that I am getting after using the above suggestions. ( i.stack.imgur.com/sn6zj.png ) How can I not have the sides of the figure cut?

            – user10853036
            Jan 7 at 22:05











          • That shouldn't happen. Did you call tight_layout before setting the aspect?

            – ImportanceOfBeingErnest
            Jan 7 at 22:23











          • I called tight_layout after setting the aspect. I don't have subplots, so I tried this method which is based on your suggestion. This is what I am doing in the relevant section of the code: 1st line: axes = plt.gca() , 2nd line: axes.set_aspect( np.diff(axes.get_xlim())/np.diff(axes.get_ylim()) ) , 3rd line : plt.tight_layout() , 4th line: plt.savefig('filename.png')

            – user10853036
            Jan 7 at 22:26





















          1














          One option you have to is manually set the limits, assuming that you know the size of your dataset.



          axes = plt.gca()
          axes.set_xlim([xmin,xmax])
          axes.set_ylim([ymin,ymax])


          A better option would be to iterate through your data to find the maximum x- and y-coordinates, take the greater of those two numbers, add a little bit more to that value to act as a buffer, and set xmax and ymax to that new value. You can use a similar method to set xmin and ymin: instead of finding the maximums, find the minimums.



          To put the legend outside of the plot, I would look at this question: How to put the legend out of the plot






          share|improve this answer
























          • I have already been able to put legend outside the box (The figure shows as much). The span of x axis in the data (x_max - x_min) is not the same as the span of y axis in the data (y_max - y_min). The only thing I want is to make x and y axes be of equal lengths (i.e the plot minus the legend should be square ).

            – user10853036
            Jan 7 at 21:03











          • Unless I'm misunderstanding something, I think you should be able to do what I described above. Let me know if it doesn't work

            – Nicholas Schmeller
            Jan 7 at 21:13











          • Nicholas: Thanks for your help. I really appreciate it. Unfortunately, it doesn't seem to work. I tried your suggestion. This is what I tried: plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=15 ) axes = plt.gca() xmin = np.min( XX[ : , 0 ] ) xmax = np.max( XX[ : , 0 ] ) ymin = np.min( XX[ : , 1 ] ) ymax = np.max( XX[ : , 1 ] ) axes.set_xlim([xmin,xmax]) axes.set_ylim([ymin,ymax])

            – user10853036
            Jan 7 at 21:41





















          0














          Something that hasn't been mentioned yet but which can give you a square output is to simply set the size of the figure using figsize=(x,y) in constructing the figure. For example,



          import matplotlib.pyplot as plt
          import numpy as np
          import random

          x = np.arange(0, 10, 0.2)
          y = np.zeros((len(x)))
          for ii in range(len(x)):
          y[ii] = random.random()*20

          fig = plt.figure(figsize=(5,5), dpi=150)
          plt.scatter(x, y, label="Data", marker="+")
          plt.title("Title")
          plt.legend(bbox_to_anchor=(1.01, 0.5), loc="center left", borderaxespad=0.)
          plt.show()




          Important note This method will not produce a square plot if plt.tight_layout() is also used - doing so will always squeeze the ticklabels and anything else that is added (title, legend, axis labels, etc) into the region created by figsize thereby reducing the aspect ratio below 1:1.






          share|improve this answer


























          • Did you notice that this does not actually produce a square axes? Only because the figure is square, doesn't mean the axes is as well.

            – ImportanceOfBeingErnest
            Jan 8 at 14:45











          • @ImportanceOfBeingErnest yes I do realize that, but it is within 30 pixels even for figures up to 1200x1200 so I thought it was appropriate to mention, given that the question seemed to be concerning the look of the plot not whether it is square down to the pixel when measured. Certainly setting the aspect ratio explicitly as in your answer is more robust, though it does require an axis handle on which to use set_aspect where this does not (the figure size can be set with rcParams instead of in constructing the figure).

            – William Miller
            Jan 8 at 16:55






          • 1





            If you don't have a handle in pyplot you may always do plt.gca().set_something.

            – ImportanceOfBeingErnest
            Jan 8 at 17:24











          • @ImportanceOfBeingErnest That’s a good note, I did not actually know that. I do think that setting the aspect ratio explicitly as in your answer is more elegant

            – William Miller
            Jan 8 at 20:33











          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%2f54081694%2fhow-to-make-sure-that-both-x-and-y-axes-of-plot-are-of-equal-sizes%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          Would plt.axis('scaled') be what you're after? That would produce a square plot, if the data limits are of equal difference.



          If they are not, you could get a square plot by setting the aspect of the axes to the ratio of xlimits and ylimits.



          import numpy as np
          import matplotlib.pyplot as plt

          fig, (ax1, ax2) = plt.subplots(1,2)

          ax1.plot([-2.5, 2.5], [-4,13], "s-")
          ax1.axis("scaled")

          ax2.plot([-2.5, 2.5], [-4,13], "s-")
          ax2.set_aspect(np.diff(ax2.get_xlim())/np.diff(ax2.get_ylim()))

          plt.show()


          enter image description here






          share|improve this answer
























          • Thanks for your answer. Will it matter if the legend is outside of the plot? I will like to have the part of the plot which is without the legend be a square (I will like to have the legend besides this square plot). In my case, the data limits are not of equal difference.

            – user10853036
            Jan 7 at 21:51











          • It shouldn't matter if there is another subplot, or a legend next to the plot for this to work. I'd say try it out and see if you're happy with the result, else come back and explain any further problem.

            – ImportanceOfBeingErnest
            Jan 7 at 21:53











          • Thanks for your answer. I tried it. It works, but there is this one issue - the sides of the plot get cut (this is with me using plt.tight_layout(). Here is the plot that I am getting after using the above suggestions. ( i.stack.imgur.com/sn6zj.png ) How can I not have the sides of the figure cut?

            – user10853036
            Jan 7 at 22:05











          • That shouldn't happen. Did you call tight_layout before setting the aspect?

            – ImportanceOfBeingErnest
            Jan 7 at 22:23











          • I called tight_layout after setting the aspect. I don't have subplots, so I tried this method which is based on your suggestion. This is what I am doing in the relevant section of the code: 1st line: axes = plt.gca() , 2nd line: axes.set_aspect( np.diff(axes.get_xlim())/np.diff(axes.get_ylim()) ) , 3rd line : plt.tight_layout() , 4th line: plt.savefig('filename.png')

            – user10853036
            Jan 7 at 22:26


















          4














          Would plt.axis('scaled') be what you're after? That would produce a square plot, if the data limits are of equal difference.



          If they are not, you could get a square plot by setting the aspect of the axes to the ratio of xlimits and ylimits.



          import numpy as np
          import matplotlib.pyplot as plt

          fig, (ax1, ax2) = plt.subplots(1,2)

          ax1.plot([-2.5, 2.5], [-4,13], "s-")
          ax1.axis("scaled")

          ax2.plot([-2.5, 2.5], [-4,13], "s-")
          ax2.set_aspect(np.diff(ax2.get_xlim())/np.diff(ax2.get_ylim()))

          plt.show()


          enter image description here






          share|improve this answer
























          • Thanks for your answer. Will it matter if the legend is outside of the plot? I will like to have the part of the plot which is without the legend be a square (I will like to have the legend besides this square plot). In my case, the data limits are not of equal difference.

            – user10853036
            Jan 7 at 21:51











          • It shouldn't matter if there is another subplot, or a legend next to the plot for this to work. I'd say try it out and see if you're happy with the result, else come back and explain any further problem.

            – ImportanceOfBeingErnest
            Jan 7 at 21:53











          • Thanks for your answer. I tried it. It works, but there is this one issue - the sides of the plot get cut (this is with me using plt.tight_layout(). Here is the plot that I am getting after using the above suggestions. ( i.stack.imgur.com/sn6zj.png ) How can I not have the sides of the figure cut?

            – user10853036
            Jan 7 at 22:05











          • That shouldn't happen. Did you call tight_layout before setting the aspect?

            – ImportanceOfBeingErnest
            Jan 7 at 22:23











          • I called tight_layout after setting the aspect. I don't have subplots, so I tried this method which is based on your suggestion. This is what I am doing in the relevant section of the code: 1st line: axes = plt.gca() , 2nd line: axes.set_aspect( np.diff(axes.get_xlim())/np.diff(axes.get_ylim()) ) , 3rd line : plt.tight_layout() , 4th line: plt.savefig('filename.png')

            – user10853036
            Jan 7 at 22:26
















          4












          4








          4







          Would plt.axis('scaled') be what you're after? That would produce a square plot, if the data limits are of equal difference.



          If they are not, you could get a square plot by setting the aspect of the axes to the ratio of xlimits and ylimits.



          import numpy as np
          import matplotlib.pyplot as plt

          fig, (ax1, ax2) = plt.subplots(1,2)

          ax1.plot([-2.5, 2.5], [-4,13], "s-")
          ax1.axis("scaled")

          ax2.plot([-2.5, 2.5], [-4,13], "s-")
          ax2.set_aspect(np.diff(ax2.get_xlim())/np.diff(ax2.get_ylim()))

          plt.show()


          enter image description here






          share|improve this answer













          Would plt.axis('scaled') be what you're after? That would produce a square plot, if the data limits are of equal difference.



          If they are not, you could get a square plot by setting the aspect of the axes to the ratio of xlimits and ylimits.



          import numpy as np
          import matplotlib.pyplot as plt

          fig, (ax1, ax2) = plt.subplots(1,2)

          ax1.plot([-2.5, 2.5], [-4,13], "s-")
          ax1.axis("scaled")

          ax2.plot([-2.5, 2.5], [-4,13], "s-")
          ax2.set_aspect(np.diff(ax2.get_xlim())/np.diff(ax2.get_ylim()))

          plt.show()


          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 7 at 21:45









          ImportanceOfBeingErnestImportanceOfBeingErnest

          130k13139215




          130k13139215













          • Thanks for your answer. Will it matter if the legend is outside of the plot? I will like to have the part of the plot which is without the legend be a square (I will like to have the legend besides this square plot). In my case, the data limits are not of equal difference.

            – user10853036
            Jan 7 at 21:51











          • It shouldn't matter if there is another subplot, or a legend next to the plot for this to work. I'd say try it out and see if you're happy with the result, else come back and explain any further problem.

            – ImportanceOfBeingErnest
            Jan 7 at 21:53











          • Thanks for your answer. I tried it. It works, but there is this one issue - the sides of the plot get cut (this is with me using plt.tight_layout(). Here is the plot that I am getting after using the above suggestions. ( i.stack.imgur.com/sn6zj.png ) How can I not have the sides of the figure cut?

            – user10853036
            Jan 7 at 22:05











          • That shouldn't happen. Did you call tight_layout before setting the aspect?

            – ImportanceOfBeingErnest
            Jan 7 at 22:23











          • I called tight_layout after setting the aspect. I don't have subplots, so I tried this method which is based on your suggestion. This is what I am doing in the relevant section of the code: 1st line: axes = plt.gca() , 2nd line: axes.set_aspect( np.diff(axes.get_xlim())/np.diff(axes.get_ylim()) ) , 3rd line : plt.tight_layout() , 4th line: plt.savefig('filename.png')

            – user10853036
            Jan 7 at 22:26





















          • Thanks for your answer. Will it matter if the legend is outside of the plot? I will like to have the part of the plot which is without the legend be a square (I will like to have the legend besides this square plot). In my case, the data limits are not of equal difference.

            – user10853036
            Jan 7 at 21:51











          • It shouldn't matter if there is another subplot, or a legend next to the plot for this to work. I'd say try it out and see if you're happy with the result, else come back and explain any further problem.

            – ImportanceOfBeingErnest
            Jan 7 at 21:53











          • Thanks for your answer. I tried it. It works, but there is this one issue - the sides of the plot get cut (this is with me using plt.tight_layout(). Here is the plot that I am getting after using the above suggestions. ( i.stack.imgur.com/sn6zj.png ) How can I not have the sides of the figure cut?

            – user10853036
            Jan 7 at 22:05











          • That shouldn't happen. Did you call tight_layout before setting the aspect?

            – ImportanceOfBeingErnest
            Jan 7 at 22:23











          • I called tight_layout after setting the aspect. I don't have subplots, so I tried this method which is based on your suggestion. This is what I am doing in the relevant section of the code: 1st line: axes = plt.gca() , 2nd line: axes.set_aspect( np.diff(axes.get_xlim())/np.diff(axes.get_ylim()) ) , 3rd line : plt.tight_layout() , 4th line: plt.savefig('filename.png')

            – user10853036
            Jan 7 at 22:26



















          Thanks for your answer. Will it matter if the legend is outside of the plot? I will like to have the part of the plot which is without the legend be a square (I will like to have the legend besides this square plot). In my case, the data limits are not of equal difference.

          – user10853036
          Jan 7 at 21:51





          Thanks for your answer. Will it matter if the legend is outside of the plot? I will like to have the part of the plot which is without the legend be a square (I will like to have the legend besides this square plot). In my case, the data limits are not of equal difference.

          – user10853036
          Jan 7 at 21:51













          It shouldn't matter if there is another subplot, or a legend next to the plot for this to work. I'd say try it out and see if you're happy with the result, else come back and explain any further problem.

          – ImportanceOfBeingErnest
          Jan 7 at 21:53





          It shouldn't matter if there is another subplot, or a legend next to the plot for this to work. I'd say try it out and see if you're happy with the result, else come back and explain any further problem.

          – ImportanceOfBeingErnest
          Jan 7 at 21:53













          Thanks for your answer. I tried it. It works, but there is this one issue - the sides of the plot get cut (this is with me using plt.tight_layout(). Here is the plot that I am getting after using the above suggestions. ( i.stack.imgur.com/sn6zj.png ) How can I not have the sides of the figure cut?

          – user10853036
          Jan 7 at 22:05





          Thanks for your answer. I tried it. It works, but there is this one issue - the sides of the plot get cut (this is with me using plt.tight_layout(). Here is the plot that I am getting after using the above suggestions. ( i.stack.imgur.com/sn6zj.png ) How can I not have the sides of the figure cut?

          – user10853036
          Jan 7 at 22:05













          That shouldn't happen. Did you call tight_layout before setting the aspect?

          – ImportanceOfBeingErnest
          Jan 7 at 22:23





          That shouldn't happen. Did you call tight_layout before setting the aspect?

          – ImportanceOfBeingErnest
          Jan 7 at 22:23













          I called tight_layout after setting the aspect. I don't have subplots, so I tried this method which is based on your suggestion. This is what I am doing in the relevant section of the code: 1st line: axes = plt.gca() , 2nd line: axes.set_aspect( np.diff(axes.get_xlim())/np.diff(axes.get_ylim()) ) , 3rd line : plt.tight_layout() , 4th line: plt.savefig('filename.png')

          – user10853036
          Jan 7 at 22:26







          I called tight_layout after setting the aspect. I don't have subplots, so I tried this method which is based on your suggestion. This is what I am doing in the relevant section of the code: 1st line: axes = plt.gca() , 2nd line: axes.set_aspect( np.diff(axes.get_xlim())/np.diff(axes.get_ylim()) ) , 3rd line : plt.tight_layout() , 4th line: plt.savefig('filename.png')

          – user10853036
          Jan 7 at 22:26















          1














          One option you have to is manually set the limits, assuming that you know the size of your dataset.



          axes = plt.gca()
          axes.set_xlim([xmin,xmax])
          axes.set_ylim([ymin,ymax])


          A better option would be to iterate through your data to find the maximum x- and y-coordinates, take the greater of those two numbers, add a little bit more to that value to act as a buffer, and set xmax and ymax to that new value. You can use a similar method to set xmin and ymin: instead of finding the maximums, find the minimums.



          To put the legend outside of the plot, I would look at this question: How to put the legend out of the plot






          share|improve this answer
























          • I have already been able to put legend outside the box (The figure shows as much). The span of x axis in the data (x_max - x_min) is not the same as the span of y axis in the data (y_max - y_min). The only thing I want is to make x and y axes be of equal lengths (i.e the plot minus the legend should be square ).

            – user10853036
            Jan 7 at 21:03











          • Unless I'm misunderstanding something, I think you should be able to do what I described above. Let me know if it doesn't work

            – Nicholas Schmeller
            Jan 7 at 21:13











          • Nicholas: Thanks for your help. I really appreciate it. Unfortunately, it doesn't seem to work. I tried your suggestion. This is what I tried: plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=15 ) axes = plt.gca() xmin = np.min( XX[ : , 0 ] ) xmax = np.max( XX[ : , 0 ] ) ymin = np.min( XX[ : , 1 ] ) ymax = np.max( XX[ : , 1 ] ) axes.set_xlim([xmin,xmax]) axes.set_ylim([ymin,ymax])

            – user10853036
            Jan 7 at 21:41


















          1














          One option you have to is manually set the limits, assuming that you know the size of your dataset.



          axes = plt.gca()
          axes.set_xlim([xmin,xmax])
          axes.set_ylim([ymin,ymax])


          A better option would be to iterate through your data to find the maximum x- and y-coordinates, take the greater of those two numbers, add a little bit more to that value to act as a buffer, and set xmax and ymax to that new value. You can use a similar method to set xmin and ymin: instead of finding the maximums, find the minimums.



          To put the legend outside of the plot, I would look at this question: How to put the legend out of the plot






          share|improve this answer
























          • I have already been able to put legend outside the box (The figure shows as much). The span of x axis in the data (x_max - x_min) is not the same as the span of y axis in the data (y_max - y_min). The only thing I want is to make x and y axes be of equal lengths (i.e the plot minus the legend should be square ).

            – user10853036
            Jan 7 at 21:03











          • Unless I'm misunderstanding something, I think you should be able to do what I described above. Let me know if it doesn't work

            – Nicholas Schmeller
            Jan 7 at 21:13











          • Nicholas: Thanks for your help. I really appreciate it. Unfortunately, it doesn't seem to work. I tried your suggestion. This is what I tried: plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=15 ) axes = plt.gca() xmin = np.min( XX[ : , 0 ] ) xmax = np.max( XX[ : , 0 ] ) ymin = np.min( XX[ : , 1 ] ) ymax = np.max( XX[ : , 1 ] ) axes.set_xlim([xmin,xmax]) axes.set_ylim([ymin,ymax])

            – user10853036
            Jan 7 at 21:41
















          1












          1








          1







          One option you have to is manually set the limits, assuming that you know the size of your dataset.



          axes = plt.gca()
          axes.set_xlim([xmin,xmax])
          axes.set_ylim([ymin,ymax])


          A better option would be to iterate through your data to find the maximum x- and y-coordinates, take the greater of those two numbers, add a little bit more to that value to act as a buffer, and set xmax and ymax to that new value. You can use a similar method to set xmin and ymin: instead of finding the maximums, find the minimums.



          To put the legend outside of the plot, I would look at this question: How to put the legend out of the plot






          share|improve this answer













          One option you have to is manually set the limits, assuming that you know the size of your dataset.



          axes = plt.gca()
          axes.set_xlim([xmin,xmax])
          axes.set_ylim([ymin,ymax])


          A better option would be to iterate through your data to find the maximum x- and y-coordinates, take the greater of those two numbers, add a little bit more to that value to act as a buffer, and set xmax and ymax to that new value. You can use a similar method to set xmin and ymin: instead of finding the maximums, find the minimums.



          To put the legend outside of the plot, I would look at this question: How to put the legend out of the plot







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 7 at 21:00









          Nicholas SchmellerNicholas Schmeller

          2126




          2126













          • I have already been able to put legend outside the box (The figure shows as much). The span of x axis in the data (x_max - x_min) is not the same as the span of y axis in the data (y_max - y_min). The only thing I want is to make x and y axes be of equal lengths (i.e the plot minus the legend should be square ).

            – user10853036
            Jan 7 at 21:03











          • Unless I'm misunderstanding something, I think you should be able to do what I described above. Let me know if it doesn't work

            – Nicholas Schmeller
            Jan 7 at 21:13











          • Nicholas: Thanks for your help. I really appreciate it. Unfortunately, it doesn't seem to work. I tried your suggestion. This is what I tried: plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=15 ) axes = plt.gca() xmin = np.min( XX[ : , 0 ] ) xmax = np.max( XX[ : , 0 ] ) ymin = np.min( XX[ : , 1 ] ) ymax = np.max( XX[ : , 1 ] ) axes.set_xlim([xmin,xmax]) axes.set_ylim([ymin,ymax])

            – user10853036
            Jan 7 at 21:41





















          • I have already been able to put legend outside the box (The figure shows as much). The span of x axis in the data (x_max - x_min) is not the same as the span of y axis in the data (y_max - y_min). The only thing I want is to make x and y axes be of equal lengths (i.e the plot minus the legend should be square ).

            – user10853036
            Jan 7 at 21:03











          • Unless I'm misunderstanding something, I think you should be able to do what I described above. Let me know if it doesn't work

            – Nicholas Schmeller
            Jan 7 at 21:13











          • Nicholas: Thanks for your help. I really appreciate it. Unfortunately, it doesn't seem to work. I tried your suggestion. This is what I tried: plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=15 ) axes = plt.gca() xmin = np.min( XX[ : , 0 ] ) xmax = np.max( XX[ : , 0 ] ) ymin = np.min( XX[ : , 1 ] ) ymax = np.max( XX[ : , 1 ] ) axes.set_xlim([xmin,xmax]) axes.set_ylim([ymin,ymax])

            – user10853036
            Jan 7 at 21:41



















          I have already been able to put legend outside the box (The figure shows as much). The span of x axis in the data (x_max - x_min) is not the same as the span of y axis in the data (y_max - y_min). The only thing I want is to make x and y axes be of equal lengths (i.e the plot minus the legend should be square ).

          – user10853036
          Jan 7 at 21:03





          I have already been able to put legend outside the box (The figure shows as much). The span of x axis in the data (x_max - x_min) is not the same as the span of y axis in the data (y_max - y_min). The only thing I want is to make x and y axes be of equal lengths (i.e the plot minus the legend should be square ).

          – user10853036
          Jan 7 at 21:03













          Unless I'm misunderstanding something, I think you should be able to do what I described above. Let me know if it doesn't work

          – Nicholas Schmeller
          Jan 7 at 21:13





          Unless I'm misunderstanding something, I think you should be able to do what I described above. Let me know if it doesn't work

          – Nicholas Schmeller
          Jan 7 at 21:13













          Nicholas: Thanks for your help. I really appreciate it. Unfortunately, it doesn't seem to work. I tried your suggestion. This is what I tried: plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=15 ) axes = plt.gca() xmin = np.min( XX[ : , 0 ] ) xmax = np.max( XX[ : , 0 ] ) ymin = np.min( XX[ : , 1 ] ) ymax = np.max( XX[ : , 1 ] ) axes.set_xlim([xmin,xmax]) axes.set_ylim([ymin,ymax])

          – user10853036
          Jan 7 at 21:41







          Nicholas: Thanks for your help. I really appreciate it. Unfortunately, it doesn't seem to work. I tried your suggestion. This is what I tried: plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=15 ) axes = plt.gca() xmin = np.min( XX[ : , 0 ] ) xmax = np.max( XX[ : , 0 ] ) ymin = np.min( XX[ : , 1 ] ) ymax = np.max( XX[ : , 1 ] ) axes.set_xlim([xmin,xmax]) axes.set_ylim([ymin,ymax])

          – user10853036
          Jan 7 at 21:41













          0














          Something that hasn't been mentioned yet but which can give you a square output is to simply set the size of the figure using figsize=(x,y) in constructing the figure. For example,



          import matplotlib.pyplot as plt
          import numpy as np
          import random

          x = np.arange(0, 10, 0.2)
          y = np.zeros((len(x)))
          for ii in range(len(x)):
          y[ii] = random.random()*20

          fig = plt.figure(figsize=(5,5), dpi=150)
          plt.scatter(x, y, label="Data", marker="+")
          plt.title("Title")
          plt.legend(bbox_to_anchor=(1.01, 0.5), loc="center left", borderaxespad=0.)
          plt.show()




          Important note This method will not produce a square plot if plt.tight_layout() is also used - doing so will always squeeze the ticklabels and anything else that is added (title, legend, axis labels, etc) into the region created by figsize thereby reducing the aspect ratio below 1:1.






          share|improve this answer


























          • Did you notice that this does not actually produce a square axes? Only because the figure is square, doesn't mean the axes is as well.

            – ImportanceOfBeingErnest
            Jan 8 at 14:45











          • @ImportanceOfBeingErnest yes I do realize that, but it is within 30 pixels even for figures up to 1200x1200 so I thought it was appropriate to mention, given that the question seemed to be concerning the look of the plot not whether it is square down to the pixel when measured. Certainly setting the aspect ratio explicitly as in your answer is more robust, though it does require an axis handle on which to use set_aspect where this does not (the figure size can be set with rcParams instead of in constructing the figure).

            – William Miller
            Jan 8 at 16:55






          • 1





            If you don't have a handle in pyplot you may always do plt.gca().set_something.

            – ImportanceOfBeingErnest
            Jan 8 at 17:24











          • @ImportanceOfBeingErnest That’s a good note, I did not actually know that. I do think that setting the aspect ratio explicitly as in your answer is more elegant

            – William Miller
            Jan 8 at 20:33
















          0














          Something that hasn't been mentioned yet but which can give you a square output is to simply set the size of the figure using figsize=(x,y) in constructing the figure. For example,



          import matplotlib.pyplot as plt
          import numpy as np
          import random

          x = np.arange(0, 10, 0.2)
          y = np.zeros((len(x)))
          for ii in range(len(x)):
          y[ii] = random.random()*20

          fig = plt.figure(figsize=(5,5), dpi=150)
          plt.scatter(x, y, label="Data", marker="+")
          plt.title("Title")
          plt.legend(bbox_to_anchor=(1.01, 0.5), loc="center left", borderaxespad=0.)
          plt.show()




          Important note This method will not produce a square plot if plt.tight_layout() is also used - doing so will always squeeze the ticklabels and anything else that is added (title, legend, axis labels, etc) into the region created by figsize thereby reducing the aspect ratio below 1:1.






          share|improve this answer


























          • Did you notice that this does not actually produce a square axes? Only because the figure is square, doesn't mean the axes is as well.

            – ImportanceOfBeingErnest
            Jan 8 at 14:45











          • @ImportanceOfBeingErnest yes I do realize that, but it is within 30 pixels even for figures up to 1200x1200 so I thought it was appropriate to mention, given that the question seemed to be concerning the look of the plot not whether it is square down to the pixel when measured. Certainly setting the aspect ratio explicitly as in your answer is more robust, though it does require an axis handle on which to use set_aspect where this does not (the figure size can be set with rcParams instead of in constructing the figure).

            – William Miller
            Jan 8 at 16:55






          • 1





            If you don't have a handle in pyplot you may always do plt.gca().set_something.

            – ImportanceOfBeingErnest
            Jan 8 at 17:24











          • @ImportanceOfBeingErnest That’s a good note, I did not actually know that. I do think that setting the aspect ratio explicitly as in your answer is more elegant

            – William Miller
            Jan 8 at 20:33














          0












          0








          0







          Something that hasn't been mentioned yet but which can give you a square output is to simply set the size of the figure using figsize=(x,y) in constructing the figure. For example,



          import matplotlib.pyplot as plt
          import numpy as np
          import random

          x = np.arange(0, 10, 0.2)
          y = np.zeros((len(x)))
          for ii in range(len(x)):
          y[ii] = random.random()*20

          fig = plt.figure(figsize=(5,5), dpi=150)
          plt.scatter(x, y, label="Data", marker="+")
          plt.title("Title")
          plt.legend(bbox_to_anchor=(1.01, 0.5), loc="center left", borderaxespad=0.)
          plt.show()




          Important note This method will not produce a square plot if plt.tight_layout() is also used - doing so will always squeeze the ticklabels and anything else that is added (title, legend, axis labels, etc) into the region created by figsize thereby reducing the aspect ratio below 1:1.






          share|improve this answer















          Something that hasn't been mentioned yet but which can give you a square output is to simply set the size of the figure using figsize=(x,y) in constructing the figure. For example,



          import matplotlib.pyplot as plt
          import numpy as np
          import random

          x = np.arange(0, 10, 0.2)
          y = np.zeros((len(x)))
          for ii in range(len(x)):
          y[ii] = random.random()*20

          fig = plt.figure(figsize=(5,5), dpi=150)
          plt.scatter(x, y, label="Data", marker="+")
          plt.title("Title")
          plt.legend(bbox_to_anchor=(1.01, 0.5), loc="center left", borderaxespad=0.)
          plt.show()




          Important note This method will not produce a square plot if plt.tight_layout() is also used - doing so will always squeeze the ticklabels and anything else that is added (title, legend, axis labels, etc) into the region created by figsize thereby reducing the aspect ratio below 1:1.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 8 at 16:48

























          answered Jan 8 at 0:38









          William MillerWilliam Miller

          1




          1













          • Did you notice that this does not actually produce a square axes? Only because the figure is square, doesn't mean the axes is as well.

            – ImportanceOfBeingErnest
            Jan 8 at 14:45











          • @ImportanceOfBeingErnest yes I do realize that, but it is within 30 pixels even for figures up to 1200x1200 so I thought it was appropriate to mention, given that the question seemed to be concerning the look of the plot not whether it is square down to the pixel when measured. Certainly setting the aspect ratio explicitly as in your answer is more robust, though it does require an axis handle on which to use set_aspect where this does not (the figure size can be set with rcParams instead of in constructing the figure).

            – William Miller
            Jan 8 at 16:55






          • 1





            If you don't have a handle in pyplot you may always do plt.gca().set_something.

            – ImportanceOfBeingErnest
            Jan 8 at 17:24











          • @ImportanceOfBeingErnest That’s a good note, I did not actually know that. I do think that setting the aspect ratio explicitly as in your answer is more elegant

            – William Miller
            Jan 8 at 20:33



















          • Did you notice that this does not actually produce a square axes? Only because the figure is square, doesn't mean the axes is as well.

            – ImportanceOfBeingErnest
            Jan 8 at 14:45











          • @ImportanceOfBeingErnest yes I do realize that, but it is within 30 pixels even for figures up to 1200x1200 so I thought it was appropriate to mention, given that the question seemed to be concerning the look of the plot not whether it is square down to the pixel when measured. Certainly setting the aspect ratio explicitly as in your answer is more robust, though it does require an axis handle on which to use set_aspect where this does not (the figure size can be set with rcParams instead of in constructing the figure).

            – William Miller
            Jan 8 at 16:55






          • 1





            If you don't have a handle in pyplot you may always do plt.gca().set_something.

            – ImportanceOfBeingErnest
            Jan 8 at 17:24











          • @ImportanceOfBeingErnest That’s a good note, I did not actually know that. I do think that setting the aspect ratio explicitly as in your answer is more elegant

            – William Miller
            Jan 8 at 20:33

















          Did you notice that this does not actually produce a square axes? Only because the figure is square, doesn't mean the axes is as well.

          – ImportanceOfBeingErnest
          Jan 8 at 14:45





          Did you notice that this does not actually produce a square axes? Only because the figure is square, doesn't mean the axes is as well.

          – ImportanceOfBeingErnest
          Jan 8 at 14:45













          @ImportanceOfBeingErnest yes I do realize that, but it is within 30 pixels even for figures up to 1200x1200 so I thought it was appropriate to mention, given that the question seemed to be concerning the look of the plot not whether it is square down to the pixel when measured. Certainly setting the aspect ratio explicitly as in your answer is more robust, though it does require an axis handle on which to use set_aspect where this does not (the figure size can be set with rcParams instead of in constructing the figure).

          – William Miller
          Jan 8 at 16:55





          @ImportanceOfBeingErnest yes I do realize that, but it is within 30 pixels even for figures up to 1200x1200 so I thought it was appropriate to mention, given that the question seemed to be concerning the look of the plot not whether it is square down to the pixel when measured. Certainly setting the aspect ratio explicitly as in your answer is more robust, though it does require an axis handle on which to use set_aspect where this does not (the figure size can be set with rcParams instead of in constructing the figure).

          – William Miller
          Jan 8 at 16:55




          1




          1





          If you don't have a handle in pyplot you may always do plt.gca().set_something.

          – ImportanceOfBeingErnest
          Jan 8 at 17:24





          If you don't have a handle in pyplot you may always do plt.gca().set_something.

          – ImportanceOfBeingErnest
          Jan 8 at 17:24













          @ImportanceOfBeingErnest That’s a good note, I did not actually know that. I do think that setting the aspect ratio explicitly as in your answer is more elegant

          – William Miller
          Jan 8 at 20:33





          @ImportanceOfBeingErnest That’s a good note, I did not actually know that. I do think that setting the aspect ratio explicitly as in your answer is more elegant

          – William Miller
          Jan 8 at 20:33


















          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%2f54081694%2fhow-to-make-sure-that-both-x-and-y-axes-of-plot-are-of-equal-sizes%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

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage