python中matplotlib的图例位置

在matplotlib中,一般图例默认是在图表内部的,如果要放置到图例外面,需要对坐标进行指定,

注意右上角是(1,1)。使用

bbox_to_anchor=(1,1),来指定锚点,设定loc='upper_left'.

比如:
 

import matplotlib.pyplot as plt

Benz = [3367, 4120, 5539]               # Benz线条
BMW = [4000, 3590, 4423]                # BMW线条
Lexus = [5200, 4930, 5350]              # Lexus线条

seq = [2021, 2022, 2023]                # 年度
plt.xticks(seq)                         # 设定x轴刻度
plt.plot(seq, Benz, '-*', label='Benz')
plt.plot(seq, BMW, '-o', label='BMW')
plt.plot(seq, Lexus, '-^', label='Lexus')
plt.legend(loc=6, bbox_to_anchor=(1,1))
plt.tight_layout(pad=7)
plt.title("Sales Report", fontsize=24)
plt.xlabel("Year", fontsize=14)
plt.ylabel("Number of Sales", fontsize=14)
plt.tick_params(axis='both', labelsize=12, color='red')
plt.show()
plt.tight_layout(pad=7),这里,设定在图表和figere1之间设定留白

你可能感兴趣的:(python,mathplotlib)