Αποτελέσματα Αναζήτησης
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 · Add a Legend to a Matplotlib. In this example, a simple quadratic function \ ( y = x^2 \) is plotted against the x-values [1, 2, 3, 4, 5]. A legend labeled “single element” is added to the plot, clarifying the plotted data. Python.
Parameters: handleslist of (Artist or tuple of Artist), optional. A list of Artists (lines, patches) to be added to the legend.
import matplotlib.pyplot as plt 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.
First we'll show off how to make a legend for specific lines. import matplotlib.pyplot as plt import numpy as np import matplotlib.collections as mcol from matplotlib.legend_handler import HandlerLineCollection, HandlerTuple from matplotlib.lines import Line2D t1 = np.arange(0.0, 2.0, 0.1) t2 = np.arange(0.0, 2.0, 0.01) fig, ax = plt.subplots
fig, ax = plt. subplots line1, = ax. plot ([1, 2, 3], label = "Line 1", linestyle = '--') line2, = ax. plot ([3, 2, 1], label = "Line 2", linewidth = 4) # Create a legend for the first line. first_legend = ax. legend (handles = [line1], loc = 'upper right') # Add the legend manually to the Axes. ax. add_artist (first_legend) # Create another ...
Basic Usage of Multiple Columns in a Matplotlib Legend. To use multiple columns in a Matplotlib legend, we can utilize the ncol parameter in the legend () function. This parameter allows us to specify the number of columns we want in our legend. Here’s an example demonstrating how to create a two-column legend: