plt 图例图标大小
时间: 2023-07-18 18:08:22 浏览: 55
在 `matplotlib` 中,可以通过调整 `legend` 对象的 `fontsize` 属性来设置图例的字体大小,例如:
```python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6], label='Line 1')
plt.plot([1, 2, 3], [2, 4, 6], label='Line 2')
plt.legend(fontsize=12)
plt.show()
```
如果要调整图例图标的大小,可以使用 `legend` 对象的 `handles` 属性获取每个图例对象,然后使用 `set_sizes` 方法设置图标大小,例如:
```python
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
line1, = plt.plot([1, 2, 3], [4, 5, 6], label='Line 1')
line2, = plt.plot([1, 2, 3], [2, 4, 6], label='Line 2')
handles, labels = plt.gca().get_legend_handles_labels()
legend = plt.legend(handles=handles, labels=labels)
for handle in legend.legendHandles:
if isinstance(handle, Line2D):
handle.set_sizes([100])
plt.show()
```
上面的代码中,我们先使用 `get_legend_handles_labels` 方法获取所有图例对象,然后使用 `legend` 对象的 `handles` 属性将它们添加到图例中。接着,我们遍历所有图例对象,如果是 `Line2D` 类型的对象,就使用 `set_sizes` 方法将图标大小设置为100。