Αποτελέσματα Αναζήτησης
A simple plot for sine and cosine curves with a legend. Used matplotlib.pyplot. import math import matplotlib.pyplot as plt x=[] for i in range(-314,314): x.append(i/100) ysin=[math.sin(i) for i in x] ycos=[math.cos(i) for i in x] plt.plot(x,ysin,label='sin(x)') #specify label for the corresponding curve plt.plot(x,ycos,label='cos(x)') plt ...
1 Αυγ 2024 · The legend helps to identify different data series by using labels specified in the label parameter of the plot functions. import matplotlib.pyplot as plt # Plot data with labels plt.plot([1, 2, 3], [4, 5, 6], label='Line 1') plt.plot([1, 2, 3], [6, 5, 4], label='Line 2') # Add a legend plt.legend() # Show the plot plt.show()
To make a legend for all artists on an Axes, call this function with an iterable of strings, one for each legend item. For example: ax.plot([1, 2, 3]) ax.plot([5, 6, 7]) ax.legend(['First line', 'Second line']) Parameters: handleslist of (Artist or tuple of Artist), optional. A list of Artists (lines, patches) to be added to the legend.
from matplotlib.lines import Line2D. colors = ['black', 'red', 'green'] lines = [Line2D([0], [0], color=c, linewidth=3, linestyle='--') for c in colors] labels = ['black data', 'red data', 'green data'] plt.legend(lines, labels) plt.show() For even more options, take a look at this matplotlib gallery sample.
There are many ways to create and customize legends in Matplotlib. Below we'll show a few examples for how to do so. First we'll show off how to make a legend for specific lines.
22 Οκτ 2021 · Learn the easiest way to add a legend to your plot using the .legend() method. Learn more on Matplotlib and how to master this powerful library.
This post explains how to customize the legend on a chart with matplotlib. It provides many examples covering the most common use cases like controling the legend location, adding a legend title or customizing the legend markers and labels.