matplotlib图例的参数代码
时间: 2023-07-12 19:51:05 浏览: 50
好的,下面是一些常用的matplotlib图例参数及其示例代码:
1. `loc`:指定图例的位置,可选值包括`'best'`、`'upper right'`、`'upper left'`、`'lower left'`、`'lower right'`、`'right'`、`'center left'`、`'center right'`、`'lower center'`、`'upper center'`、`'center'`。例如:`plt.legend(loc='upper right')`。
2. `ncol`:指定图例中的列数。例如:`plt.legend(ncol=2)`。
3. `title`:指定图例的标题。例如:`plt.legend(title='Legend')`。
4. `fontsize`:指定图例文字的字体大小。例如:`plt.legend(fontsize='medium')`。
5. `frameon`:指定是否显示图例边框。例如:`plt.legend(frameon=False)`。
6. `borderaxespad`:指定图例边框与图像的距离。例如:`plt.legend(borderaxespad=1.0)`。
7. `fancybox`:指定图例边框是否使用圆角。例如:`plt.legend(fancybox=True)`。
8. `shadow`:指定图例边框是否显示阴影。例如:`plt.legend(shadow=True)`。
9. `mode`:指定图例中的图标显示方式,可选值包括`'expand'`、`'extend'`、`'none'`。例如:`plt.legend(mode='expand')`。
示例代码:
```python
import matplotlib.pyplot as plt
# 数据
x = [1, 2, 3, 4, 5]
y1 = [1, 2, 3, 4, 5]
y2 = [1, 4, 9, 16, 25]
# 图像
plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
# 图例
legend = plt.legend(loc='upper left', ncol=2, title='Legend', fontsize='medium', frameon=False, borderaxespad=1.0, fancybox=True, shadow=True, mode='expand')
# 展示图像
plt.show()
```
希望这些参数能够帮到你!