plt.legend(['y = x', 'y = 2x', 'y = 3x', 'y = 4x'], loc='upper left')
时间: 2024-01-27 11:05:21 浏览: 8
This code creates a legend for a plot with four lines. The legend labels the lines as "y = x", "y = 2x", "y = 3x", and "y = 4x". The location of the legend is specified as "upper left".
相关问题
plt.legend(loc=1)
plt.legend(loc=1)是用于在matplotlib中设置图例位置的函数。其中,loc参数用于指定图例的位置,具体取值如下:
- 0:最佳位置(自动选择最佳位置)
- 1:右上角
- 2:左上角
- 3:左下角
- 4:右下角
- 5:右侧中间
- 6:左侧中间
- 7:底部中间
- 8:顶部中间
- 9:中心
以下是一个示例代码,演示了如何使用plt.legend(loc=1)来设置图例位置为右上角:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x, y, label='y = x^2')
plt.legend(loc=1)
plt.show()
```
运行以上代码,将会绘制一个折线图,并将图例放置在右上角。
plt.legend(labels,loc="upper left")
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()
```