Setting a fixed size for points in legend in Matplotlib

Setting a fixed size for points in legend in Matplotlib

In Matplotlib, the size of points in the legend is automatically determined based on the markers used in the plot. However, if you want to set a fixed size for points in the legend, you can achieve this by creating a custom legend handler. Here's how you can do it:

import matplotlib.pyplot as plt from matplotlib.legend_handler import HandlerTuple # Create a custom legend handler with fixed marker size class FixedSizeHandlerTuple(HandlerTuple): def create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans): # Call the parent class's create_artists method artists = super().create_artists(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans) for artist in artists: if isinstance(artist, plt.Line2D): artist.set_markersize(10) # Set the desired fixed marker size return artists # Create a scatter plot with a legend x = [1, 2, 3] y = [4, 5, 6] plt.scatter(x, y, label='Data points') # Add legend with the custom handler plt.legend(handler_map={plt.scatter: FixedSizeHandlerTuple()}) # Show the plot plt.show() 

In this example, the FixedSizeHandlerTuple class is a custom legend handler that inherits from HandlerTuple. It overrides the create_artists method to set a fixed marker size for scatter plot markers in the legend.

When you create your scatter plot, you can then add the legend using the custom handler FixedSizeHandlerTuple to ensure that the points in the legend have a fixed marker size.

Adjust the artist.set_markersize() value to the desired size for the points in the legend.

Examples

    import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [3, 2, 1] y2 = [1, 2, 3] plt.plot(x, y1, 'o', label='Small points') plt.plot(x, y2, 'o', markersize=15, label='Large points') # Use handler_map to set a fixed size for legend markers legend = plt.legend(handler_map={plt.Line2D: plt.legend.LegendHandlerMarkers(markersize=10)}) plt.show() 
      import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [3, 2, 1] y2 = [1, 2, 3] plt.plot(x, y1, 'o', markersize=5, label='Small points') plt.plot(x, y2, 'o', markersize=15, label='Large points') # Adjust the size of legend markers plt.legend(handler_map={plt.Line2D: plt.legend.LegendHandlerMarkers(markersize=8)}) plt.show() 
        import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [2, 3, 1] y2 = [3, 1, 2] plt.plot(x, y1, 'o', markersize=20, label='Large data points') plt.plot(x, y2, 'o', markersize=5, label='Small data points') # Fix the size of legend markers plt.legend(handler_map={plt.Line2D: plt.legend.LegendHandlerMarkers(markersize=10)}) plt.show() 
          import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [3, 6, 9] y2 = [1, 4, 7] plt.plot(x, y1, 'o', markersize=30, label='Huge points') plt.plot(x, y2, 'o', markersize=10, label='Small points') # Control the size of legend markers plt.legend(handler_map={plt.Line2D: plt.legend.LegendHandlerMarkers(markersize=12)}) plt.show() 
            import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [3, 2, 1] y2 = [4, 5, 6] plt.plot(x, y1, 'o', markersize=5, label='Tiny') plt.plot(x, y2, 'o', markersize=25, label='Huge') # Fix the size of legend markers plt.legend(handler_map={plt.Line2D: plt.legend.LegendHandlerMarkers(markersize=10)}) plt.show() 
              import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [3, 1, 2] y2 = [2, 4, 6] plt.plot(x, y1, 'o', markersize=10, label='Small points') plt.plot(x, y2, 'o', markersize=30, label='Big points') # Set a constant size for legend markers plt.legend(handler_map={plt.Line2D: plt.legend.LegendHandlerMarkers(markersize=12)}) plt.show() 
                import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [2, 3, 4] y2 = [1, 2, 3] plt.plot(x, y1, 'o', markersize=25, label='Larger plot markers') plt.plot(x, y2, 'o', markersize=5, label='Smaller plot markers') # Independent legend markersize plt.legend(handler_map={plt.Line2D: plt.legend.LegendHandlerMarkers(markersize=10)}) plt.show() 
                  import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [1, 3, 5] y2 = [2, 4, 6] plt.plot(x, y1, 'o', markersize=40, label='Large') plt.plot(x, y2, 'o', markersize=15, label='Medium') # Keep the same legend marker size for all plt.legend(handler_map={plt.Line2D: plt.legend.LegendHandlerMarkers(markersize=10)}) plt.show() 
                    import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [10, 20, 30] y2 = [5, 15, 25] plt.plot(x, y1, 'o', markersize=20, label='Big') plt.plot(x, y2, 'o', markersize=10, label='Small') # Uniform legend marker size plt.legend(handler_map={plt.Line2D: plt.legend.LegendHandlerMarkers(markersize=10)}) plt.show() 
                    1. "How to fix the size of legend points in Matplotlib?"

                    More Tags

                    ujs text-to-speech r-plotly virtual-memory jquery-ui-dialog discount navigator mockito mlab asp.net-web-api

                    More Python Questions

                    More Mortgage and Real Estate Calculators

                    More Physical chemistry Calculators

                    More Biochemistry Calculators

                    More Retirement Calculators