plt.legend(labels,loc="upper left")
时间: 2023-10-28 11:04:47 浏览: 109
关于matplotlib-legend 位置属性 loc 使用说明
5星 · 资源好评率100%
This line of code is used for adding a legend to a plot in matplotlib library of Python.
`labels` is a list of strings that represent the labels for each plotted line or point in the plot.
`loc` is a string that represents the location of the legend on the plot. It can take values like "upper left", "upper right", "lower left", "lower right", "center", and so on.
For example, if we have plotted two lines with labels "Line 1" and "Line 2", and we want to add a legend to the plot at the upper left corner, we can use the following code:
```
import matplotlib.pyplot as plt
# Plotting two lines
plt.plot(x1, y1, label="Line 1")
plt.plot(x2, y2, label="Line 2")
# Adding legend to the plot
plt.legend(labels=["Line 1", "Line 2"], loc="upper left")
# Displaying the plot
plt.show()
```
阅读全文