python的matplotlib里的legend用法
时间: 2024-05-01 14:10:30 浏览: 127
Matplotlib中的legend用于为图表添加标签和说明。通常,它将一些关键字参数传递给plot()函数,以确定要添加到图例中的标签。例如,我们可以使用以下代码添加图例:
import matplotlib.pyplot as plt
# 创建一些数据
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
# 画出数据
plt.plot(x, y, label='线条1')
# 添加图例
plt.legend()
在这个例子中,我们将“线条1”的标签传递给plot()函数,并使用legend()函数将它添加到图例中。还可以将多个标签传递给plot()函数,并使用逗号分隔它们。例如:
plt.plot(x, y, label='线条1')
plt.plot(x, [i+1 for i in y], label='线条2')
plt.plot(x, [i+2 for i in y], label='线条3')
plt.legend()
这将创建一个图例,其中包含三个标签:“线条1”,“线条2”和“线条3”。可以将图例放置在不同的位置,例如右下角或左上角。这可以通过传递关键字参数给legend()函数来完成。例如:
plt.legend(loc='upper left')
这将把图例放置在左上角。可以使用不同的loc参数(如“upper right”,“center”等)来将图例放置在不同的位置。
相关问题
python matplotlib 自定义legend
### 如何在Python中使用Matplotlib自定义图例
为了更好地理解如何定制图例外观,在Python中的`matplotlib`库提供了多种方法来调整图例的位置、字体大小和其他属性。以下是几种常见的自定义方式。
#### 设置图例位置
可以通过参数`loc`指定图例放置的具体位置,该参数接受字符串或整数作为输入值表示不同的预设方位。例如:
```python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line')
plt.legend(loc='upper left') # 将图例放在左上角
plt.show()
```
#### 修改图例框样式
可以利用`fancybox`, `shadow`等布尔型参数改变图例边框的效果;而通过设置`framealpha`可调节透明度[^1]。
```python
plt.plot([1, 2, 3], label='Fancy Legend Box')
plt.legend(fancybox=True, shadow=True, framealpha=0.5)
plt.show()
```
#### 调整图例内文字特性
对于想要修改图例内部文本样式的场景,则能借助于`prop`字典传递给`legend()`函数实现对字体家族(`family`)、粗细程度(`weight`)以及字号(`size`)等方面的控制。
```python
from matplotlib.font_manager import FontProperties
font = FontProperties()
font.set_family('serif')
font.set_weight('bold')
font.set_size('large')
plt.plot([1, 2, 3], label='Styled Text in Legend')
plt.legend(prop=font)
plt.show()
```
#### 控制标记尺寸与线条宽度
当涉及到散点图或者带有不同线宽的折线图时,还可以单独设定图例里代表数据系列的小图标大小(即`markerscale`)和对应线条厚度(即`handlelength`),从而达到更美观一致的整体效果[^4]。
```python
plt.scatter([1, 2, 3], [4, 5, 6], s=[70, 80, 90], c=['red'], marker='o', label='Scatter Points')
line, = plt.plot([1, 2, 3], 'b-', lw=4, label='Thick Line')
plt.legend(markerscale=2., handlelength=0.8)
plt.show()
```
python Matplotlib
### 使用 Python Matplotlib 进行数据可视化
#### 安装 Matplotlib 库
为了使用 Matplotlib,需先确保已安装该库。可以通过 pip 工具来完成安装:
```bash
pip install matplotlib
```
#### 导入必要的模块并创建基础折线图
一旦安装完毕,就可以通过 `import` 命令引入所需的包,并利用简单的例子展示如何绘制一条正弦曲线[^1]。
```python
import matplotlib.pyplot as plt
import numpy as np
# 创建数据集
x_values = np.linspace(0, 10, 100)
y_values = np.sin(x_values)
# 绘制图形
plt.figure(figsize=(8, 4)) # 设置画布大小
plt.plot(x_values, y_values, label='sin(x)', color="blue", linestyle="--")
# 添加标签和标题
plt.title('Sine Wave Example') # 图表标题
plt.xlabel('X Axis Label (radians)')# X轴名称
plt.ylabel('Y Axis Label (amplitude)')# Y轴名称
# 显示网格线
plt.grid(True)
# 展示图例
plt.legend()
# 输出图像到屏幕
plt.show()
```
这段代码展示了如何定义一组用于表示横坐标 (`x`) 和纵坐标 (`y`) 的数值序列;接着调用 `plot()` 函数生成对应的线条;最后设置了一些额外属性如颜色、样式以及添加了图例说明等[^2]。
#### 将图表保存为图片文件以便于Web集成
除了直接显示外,还可以把生成的图表导出成静态图片形式,方便嵌入网页或其他文档中。这一步骤可通过 `savefig()` 方法实现,指定目标路径及格式即可[^3]。
```python
plt.savefig("output/sinewave_example.svg", format='svg', dpi=1200)
```
上述命令会将当前绘图窗口的内容另存为名为 "sinewave_example.svg" 的矢量图形文件,质量较高适合印刷出版物或高质量网络发布需求。
阅读全文
相关推荐
















