Set matplotlib legend markersize to a constant












2















I'm making a diagram using matplotlib, and it has plt.Circles and plt.axvlines to represent different shapes. I need a legend to describe these shapes, but the problem is the legend marker (the image part), changes size depending on the input, which looks awful. How do I set the size to a constant?



fig = plt.figure(figsize=(6.4, 6), dpi=200, frameon=False)
ax = fig.gca()
# 3 Circles, they produce different sized legend markers
ax.add_patch(plt.Circle((0,0), radius=1, alpha=0.9, zorder=0, label="Circle"))
ax.add_patch(plt.Circle((-1,0), radius=0.05, color="y", label="Point on Circle"))
ax.add_patch(plt.Circle((1, 0), radius=0.05, color="k", label="Opposite Point on Circle"))
# A vertical line which produces a huge legend marker
ax.axvline(0, ymin=0.5-0.313, ymax=0.5+0.313, linewidth=12, zorder=1, c="g", label="Vertical Line")
ax.legend(loc=2)
ax.set_xlim(-2,1.2) # The figsize and limits are meant to preserve the circle's shape
ax.set_ylim(-1.5, 1.5)
fig.show()


I've seen solutions including legend.legendHandles[0]._size or various assortments of that, and it doesn't seem to change the size regardless of the value I set










share|improve this question



























    2















    I'm making a diagram using matplotlib, and it has plt.Circles and plt.axvlines to represent different shapes. I need a legend to describe these shapes, but the problem is the legend marker (the image part), changes size depending on the input, which looks awful. How do I set the size to a constant?



    fig = plt.figure(figsize=(6.4, 6), dpi=200, frameon=False)
    ax = fig.gca()
    # 3 Circles, they produce different sized legend markers
    ax.add_patch(plt.Circle((0,0), radius=1, alpha=0.9, zorder=0, label="Circle"))
    ax.add_patch(plt.Circle((-1,0), radius=0.05, color="y", label="Point on Circle"))
    ax.add_patch(plt.Circle((1, 0), radius=0.05, color="k", label="Opposite Point on Circle"))
    # A vertical line which produces a huge legend marker
    ax.axvline(0, ymin=0.5-0.313, ymax=0.5+0.313, linewidth=12, zorder=1, c="g", label="Vertical Line")
    ax.legend(loc=2)
    ax.set_xlim(-2,1.2) # The figsize and limits are meant to preserve the circle's shape
    ax.set_ylim(-1.5, 1.5)
    fig.show()


    I've seen solutions including legend.legendHandles[0]._size or various assortments of that, and it doesn't seem to change the size regardless of the value I set










    share|improve this question

























      2












      2








      2








      I'm making a diagram using matplotlib, and it has plt.Circles and plt.axvlines to represent different shapes. I need a legend to describe these shapes, but the problem is the legend marker (the image part), changes size depending on the input, which looks awful. How do I set the size to a constant?



      fig = plt.figure(figsize=(6.4, 6), dpi=200, frameon=False)
      ax = fig.gca()
      # 3 Circles, they produce different sized legend markers
      ax.add_patch(plt.Circle((0,0), radius=1, alpha=0.9, zorder=0, label="Circle"))
      ax.add_patch(plt.Circle((-1,0), radius=0.05, color="y", label="Point on Circle"))
      ax.add_patch(plt.Circle((1, 0), radius=0.05, color="k", label="Opposite Point on Circle"))
      # A vertical line which produces a huge legend marker
      ax.axvline(0, ymin=0.5-0.313, ymax=0.5+0.313, linewidth=12, zorder=1, c="g", label="Vertical Line")
      ax.legend(loc=2)
      ax.set_xlim(-2,1.2) # The figsize and limits are meant to preserve the circle's shape
      ax.set_ylim(-1.5, 1.5)
      fig.show()


      I've seen solutions including legend.legendHandles[0]._size or various assortments of that, and it doesn't seem to change the size regardless of the value I set










      share|improve this question














      I'm making a diagram using matplotlib, and it has plt.Circles and plt.axvlines to represent different shapes. I need a legend to describe these shapes, but the problem is the legend marker (the image part), changes size depending on the input, which looks awful. How do I set the size to a constant?



      fig = plt.figure(figsize=(6.4, 6), dpi=200, frameon=False)
      ax = fig.gca()
      # 3 Circles, they produce different sized legend markers
      ax.add_patch(plt.Circle((0,0), radius=1, alpha=0.9, zorder=0, label="Circle"))
      ax.add_patch(plt.Circle((-1,0), radius=0.05, color="y", label="Point on Circle"))
      ax.add_patch(plt.Circle((1, 0), radius=0.05, color="k", label="Opposite Point on Circle"))
      # A vertical line which produces a huge legend marker
      ax.axvline(0, ymin=0.5-0.313, ymax=0.5+0.313, linewidth=12, zorder=1, c="g", label="Vertical Line")
      ax.legend(loc=2)
      ax.set_xlim(-2,1.2) # The figsize and limits are meant to preserve the circle's shape
      ax.set_ylim(-1.5, 1.5)
      fig.show()


      I've seen solutions including legend.legendHandles[0]._size or various assortments of that, and it doesn't seem to change the size regardless of the value I set







      python-3.x matplotlib legend






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 23:11









      Dylan GatlinDylan Gatlin

      112




      112
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The legend markers for the circles are different in size because the first circle has no edgecolor, while the two other ones have an edgecolor set via color. You may instead set the facecolor of the circle. Alternatively, you can set the linewidth of all 3 circles equal.



          The legend marker for the line is so huge because it simply copies the attribute from the line in the plot. If you want to use a different linewidth, you can update it via the respective legend handler.



          import matplotlib.pyplot as plt
          from matplotlib.legend_handler import HandlerLine2D

          def update_prop(handle, orig):
          handle.update_from(orig)
          handle.set_linewidth(2)

          fig, ax = plt.subplots(figsize=(6.4, 6), dpi=200, frameon=False)

          # 3 Circles, set the facecolor instead of edge- and face-color
          ax.add_patch(plt.Circle((0,0), radius=1, alpha=0.9, zorder=0, label="Circle"))
          ax.add_patch(plt.Circle((-1,0), radius=0.05, facecolor="y", label="Point on Circle"))
          ax.add_patch(plt.Circle((1, 0), radius=0.05, facecolor="k", label="Opposite Point on Circle"))

          # Line, update the linewidth via
          ax.axvline(0, ymin=0.5-0.313, ymax=0.5+0.313, linewidth=12, zorder=1, c="g", label="Vertical Line")
          ax.legend(loc=2, handler_map={plt.Line2D:HandlerLine2D(update_func=update_prop)})


          ax.set_xlim(-2,1.2)
          ax.set_ylim(-1.5, 1.5)
          plt.show()


          enter image description here






          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%2f53463177%2fset-matplotlib-legend-markersize-to-a-constant%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














            The legend markers for the circles are different in size because the first circle has no edgecolor, while the two other ones have an edgecolor set via color. You may instead set the facecolor of the circle. Alternatively, you can set the linewidth of all 3 circles equal.



            The legend marker for the line is so huge because it simply copies the attribute from the line in the plot. If you want to use a different linewidth, you can update it via the respective legend handler.



            import matplotlib.pyplot as plt
            from matplotlib.legend_handler import HandlerLine2D

            def update_prop(handle, orig):
            handle.update_from(orig)
            handle.set_linewidth(2)

            fig, ax = plt.subplots(figsize=(6.4, 6), dpi=200, frameon=False)

            # 3 Circles, set the facecolor instead of edge- and face-color
            ax.add_patch(plt.Circle((0,0), radius=1, alpha=0.9, zorder=0, label="Circle"))
            ax.add_patch(plt.Circle((-1,0), radius=0.05, facecolor="y", label="Point on Circle"))
            ax.add_patch(plt.Circle((1, 0), radius=0.05, facecolor="k", label="Opposite Point on Circle"))

            # Line, update the linewidth via
            ax.axvline(0, ymin=0.5-0.313, ymax=0.5+0.313, linewidth=12, zorder=1, c="g", label="Vertical Line")
            ax.legend(loc=2, handler_map={plt.Line2D:HandlerLine2D(update_func=update_prop)})


            ax.set_xlim(-2,1.2)
            ax.set_ylim(-1.5, 1.5)
            plt.show()


            enter image description here






            share|improve this answer




























              1














              The legend markers for the circles are different in size because the first circle has no edgecolor, while the two other ones have an edgecolor set via color. You may instead set the facecolor of the circle. Alternatively, you can set the linewidth of all 3 circles equal.



              The legend marker for the line is so huge because it simply copies the attribute from the line in the plot. If you want to use a different linewidth, you can update it via the respective legend handler.



              import matplotlib.pyplot as plt
              from matplotlib.legend_handler import HandlerLine2D

              def update_prop(handle, orig):
              handle.update_from(orig)
              handle.set_linewidth(2)

              fig, ax = plt.subplots(figsize=(6.4, 6), dpi=200, frameon=False)

              # 3 Circles, set the facecolor instead of edge- and face-color
              ax.add_patch(plt.Circle((0,0), radius=1, alpha=0.9, zorder=0, label="Circle"))
              ax.add_patch(plt.Circle((-1,0), radius=0.05, facecolor="y", label="Point on Circle"))
              ax.add_patch(plt.Circle((1, 0), radius=0.05, facecolor="k", label="Opposite Point on Circle"))

              # Line, update the linewidth via
              ax.axvline(0, ymin=0.5-0.313, ymax=0.5+0.313, linewidth=12, zorder=1, c="g", label="Vertical Line")
              ax.legend(loc=2, handler_map={plt.Line2D:HandlerLine2D(update_func=update_prop)})


              ax.set_xlim(-2,1.2)
              ax.set_ylim(-1.5, 1.5)
              plt.show()


              enter image description here






              share|improve this answer


























                1












                1








                1







                The legend markers for the circles are different in size because the first circle has no edgecolor, while the two other ones have an edgecolor set via color. You may instead set the facecolor of the circle. Alternatively, you can set the linewidth of all 3 circles equal.



                The legend marker for the line is so huge because it simply copies the attribute from the line in the plot. If you want to use a different linewidth, you can update it via the respective legend handler.



                import matplotlib.pyplot as plt
                from matplotlib.legend_handler import HandlerLine2D

                def update_prop(handle, orig):
                handle.update_from(orig)
                handle.set_linewidth(2)

                fig, ax = plt.subplots(figsize=(6.4, 6), dpi=200, frameon=False)

                # 3 Circles, set the facecolor instead of edge- and face-color
                ax.add_patch(plt.Circle((0,0), radius=1, alpha=0.9, zorder=0, label="Circle"))
                ax.add_patch(plt.Circle((-1,0), radius=0.05, facecolor="y", label="Point on Circle"))
                ax.add_patch(plt.Circle((1, 0), radius=0.05, facecolor="k", label="Opposite Point on Circle"))

                # Line, update the linewidth via
                ax.axvline(0, ymin=0.5-0.313, ymax=0.5+0.313, linewidth=12, zorder=1, c="g", label="Vertical Line")
                ax.legend(loc=2, handler_map={plt.Line2D:HandlerLine2D(update_func=update_prop)})


                ax.set_xlim(-2,1.2)
                ax.set_ylim(-1.5, 1.5)
                plt.show()


                enter image description here






                share|improve this answer













                The legend markers for the circles are different in size because the first circle has no edgecolor, while the two other ones have an edgecolor set via color. You may instead set the facecolor of the circle. Alternatively, you can set the linewidth of all 3 circles equal.



                The legend marker for the line is so huge because it simply copies the attribute from the line in the plot. If you want to use a different linewidth, you can update it via the respective legend handler.



                import matplotlib.pyplot as plt
                from matplotlib.legend_handler import HandlerLine2D

                def update_prop(handle, orig):
                handle.update_from(orig)
                handle.set_linewidth(2)

                fig, ax = plt.subplots(figsize=(6.4, 6), dpi=200, frameon=False)

                # 3 Circles, set the facecolor instead of edge- and face-color
                ax.add_patch(plt.Circle((0,0), radius=1, alpha=0.9, zorder=0, label="Circle"))
                ax.add_patch(plt.Circle((-1,0), radius=0.05, facecolor="y", label="Point on Circle"))
                ax.add_patch(plt.Circle((1, 0), radius=0.05, facecolor="k", label="Opposite Point on Circle"))

                # Line, update the linewidth via
                ax.axvline(0, ymin=0.5-0.313, ymax=0.5+0.313, linewidth=12, zorder=1, c="g", label="Vertical Line")
                ax.legend(loc=2, handler_map={plt.Line2D:HandlerLine2D(update_func=update_prop)})


                ax.set_xlim(-2,1.2)
                ax.set_ylim(-1.5, 1.5)
                plt.show()


                enter image description here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 24 '18 at 23:44









                ImportanceOfBeingErnestImportanceOfBeingErnest

                136k13154232




                136k13154232
































                    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%2f53463177%2fset-matplotlib-legend-markersize-to-a-constant%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

                    To store a contact into the json file from server.js file using a class in NodeJS

                    Redirect URL with Chrome Remote Debugging Android Devices

                    Dieringhausen