收百科
当前位置: 首页 生活百科

图列位置靠上怎么设置 图例位置靠右

时间:2023-09-03 作者: 小编 阅读量: 1 栏目名: 生活百科

要将图例位置设置为靠上和靠右,可以使用matplotlib库中的legend()函数,并设置相关参数。首先,要将图例位置靠上,可以将参数loc设置为"upper"。

要将图例位置设置为靠上和靠右,可以使用matplotlib库中的legend()函数,并设置相关参数。

首先,要将图例位置靠上,可以将参数loc设置为"upper"。例如,legend(loc="upper")。

其次,要将图例位置靠右,可以将参数bbox_to_anchor设置为(1, 1)。例如,legend(loc="upper", bbox_to_anchor=(1, 1))。

以下是一个完整的示例代码:

```python

import matplotlib.pyplot as plt

# 创建示例数据

x = [1, 2, 3, 4, 5]

y = [1, 4, 9, 16, 25]

# 绘制折线图

plt.plot(x, y, label="Line")

# 设置图例位置为靠上和靠右

plt.legend(loc="upper", bbox_to_anchor=(1, 1))

# 显示图形

plt.show()

```

运行此代码将绘制出一个折线图,并将图例位置设置为靠上和靠右。