可以使用plt.legend()函数来设置图例的样式。

下面是一些可以设置的参数:

  • loc:图例的位置,可以是一个字符串,也可以是一个二元组,用于指定坐标轴上的位置。常见的字符串有'best'、'upper right'、'upper left'、'lower right'、'lower left'、'right'、'center left'、'center right'、'lower center'、'upper center'、'center'。
  • fontsize:图例的字体大小。
  • frameon:是否显示图例的边框。
  • ncol:图例的列数。
  • title:图例的标题。

例如:

import matplotlib.pyplot as plt

x = [1, 2, 3]
y1 = [1, 2, 3]
y2 = [2, 4, 6]

plt.plot(x, y1, label='y1')
plt.plot(x, y2, label='y2')

plt.legend(loc='upper right', fontsize=12, frameon=False, ncol=2, title='Legend Title')

plt.show()

这段代码会在图例的右上角显示图例,字体大小为12,无边框,分为2列,标题为'Legend Title'

matplotlib中设置图例的样式

原文地址: https://www.cveoy.top/t/topic/hudU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录