In Matplotlib, you can create a legend as a separate picture by first creating the legend and then saving it as an image file. Here's how you can do it:
import matplotlib.pyplot as plt # Create a plot plt.plot([1, 2, 3], [4, 5, 6], label='Line A') plt.plot([1, 2, 3], [6, 5, 4], label='Line B') # Create a legend plt.legend() # Save the legend as a separate image legend_fig = plt.figure() legend_ax = legend_fig.add_subplot(111) legend_ax.legend(*plt.gca().get_legend_handles_labels()) legend_ax.axis('off') legend_fig.savefig('legend.png', bbox_inches='tight') # Close the legend figure plt.close(legend_fig) # Show the main plot plt.show() In this example:
We create a basic plot with two lines and labels using plt.plot().
We create a legend using plt.legend().
We create a separate figure and axis for the legend using legend_fig and legend_ax.
We copy the legend from the main plot to the legend axis using legend_ax.legend(*plt.gca().get_legend_handles_labels()).
We turn off the axis using legend_ax.axis('off').
We save the legend as a separate image using legend_fig.savefig('legend.png', bbox_inches='tight'). You can specify the desired filename and format.
We close the legend figure to release resources using plt.close(legend_fig).
Finally, we display the main plot using plt.show().
After running this code, you'll have a separate image file named 'legend.png' that contains the legend. You can customize the legend's appearance and positioning as needed.
Matplotlib separate legend image example:
import matplotlib.pyplot as plt from matplotlib.legend_handler import HandlerLine2D # Plotting data x = range(10) y1 = [i**2 for i in x] y2 = [i**1.5 for i in x] fig, ax = plt.subplots() ax.plot(x, y1, label='y = x^2') ax.plot(x, y2, label='y = x^1.5') # Getting legend as a separate image handles, labels = ax.get_legend_handles_labels() ax.legend(handles, labels, handler_map={str: HandlerLine2D(numpoints=1)}) legend = ax.legend_.legendHandles[0].figure # Displaying the legend image plt.show() Extract Matplotlib legend to image:
import matplotlib.pyplot as plt # Plotting data x = range(10) y1 = [i**2 for i in x] y2 = [i**1.5 for i in x] fig, ax = plt.subplots() ax.plot(x, y1, label='y = x^2') ax.plot(x, y2, label='y = x^1.5') # Saving legend as a separate image ax.legend() legend = ax.legend_.legendHandles[0].figure legend.savefig('legend.png') Matplotlib save legend as image:
import matplotlib.pyplot as plt # Plotting data x = range(10) y1 = [i**2 for i in x] y2 = [i**1.5 for i in x] fig, ax = plt.subplots() ax.plot(x, y1, label='y = x^2') ax.plot(x, y2, label='y = x^1.5') # Saving legend as an image ax.legend() legend = ax.legend_.legendHandles[0].figure legend.savefig('legend_image.png') Separate legend image Matplotlib:
import matplotlib.pyplot as plt # Plotting data x = range(10) y1 = [i**2 for i in x] y2 = [i**1.5 for i in x] fig, ax = plt.subplots() ax.plot(x, y1, label='y = x^2') ax.plot(x, y2, label='y = x^1.5') # Extracting legend as a separate image ax.legend() legend = ax.legend_.legendHandles[0].figure plt.figure() plt.imshow(legend) plt.axis('off') plt.show() Get legend as separate image in Matplotlib:
import matplotlib.pyplot as plt # Plotting data x = range(10) y1 = [i**2 for i in x] y2 = [i**1.5 for i in x] fig, ax = plt.subplots() ax.plot(x, y1, label='y = x^2') ax.plot(x, y2, label='y = x^1.5') # Getting legend as a separate image ax.legend() legend = ax.legend_.legendHandles[0].figure plt.imshow(legend) plt.axis('off') plt.show() Extract legend image from Matplotlib plot:
import matplotlib.pyplot as plt # Plotting data x = range(10) y1 = [i**2 for i in x] y2 = [i**1.5 for i in x] fig, ax = plt.subplots() ax.plot(x, y1, label='y = x^2') ax.plot(x, y2, label='y = x^1.5') # Extracting legend from the plot as an image ax.legend() legend = ax.legend_.legendHandles[0].figure plt.imshow(legend) plt.axis('off') plt.show() Save legend as image Matplotlib:
import matplotlib.pyplot as plt # Plotting data x = range(10) y1 = [i**2 for i in x] y2 = [i**1.5 for i in x] fig, ax = plt.subplots() ax.plot(x, y1, label='y = x^2') ax.plot(x, y2, label='y = x^1.5') # Saving legend as an image file ax.legend() legend = ax.legend_.legendHandles[0].figure legend.savefig('legend_image.png') Matplotlib extract legend to image:
import matplotlib.pyplot as plt # Plotting data x = range(10) y1 = [i**2 for i in x] y2 = [i**1.5 for i in x] fig, ax = plt.subplots() ax.plot(x, y1, label='y = x^2') ax.plot(x, y2, label='y = x^1.5') # Extracting legend to an image file ax.legend() legend = ax.legend_.legendHandles[0].figure legend.savefig('legend_image.png') Get Matplotlib legend as image:
import matplotlib.pyplot as plt # Plotting data x = range(10) y1 = [i**2 for i in x] y2 = [i**1.5 for i in x] fig, ax = plt.subplots() ax.plot(x, y1, label='y = x^2') ax.plot(x, y2, label='y = x^1.5') # Getting the legend as an image ax.legend() legend = ax.legend_.legendHandles[0].figure plt.imshow(legend) plt.axis('off') plt.show() Extract Matplotlib legend as image:
import matplotlib.pyplot as plt # Plotting data x = range(10) y1 = [i**2 for i in x] y2 = [i**1.5 for i in x] fig, ax = plt.subplots() ax.plot(x, y1, label='y = x^2') ax.plot(x, y2, label='y = x^1.5') # Extracting the legend to an image ax.legend() legend = ax.legend_.legendHandles[0].figure plt.imshow(legend) plt.axis('off') plt.show() django-generic-views production-environment swifty-json google-cloud-functions dpkt x-xsrf-token php-5.2 presentmodalviewcontroller mime-types office-interop