When using matplotlib to draw icons, the use of legend plt.legend()

plt.legend(loc=3, bbox_to_anchor=(-0.12, -0.12))

The legend sets the position via loc, bbox_to_anchor

Parameter 1: loc()

This parameter is used to determine an approximate position in the picture
upper right (1)
upper left (2)
lower left (3)
lower right (4)
center left (6)
center right (7)
lower center (8)
upper center (9 )

Parameter 2: bbox_to_anchor(num1, num2)

Used to fine-tune the position of the legend.
num1 is used to control the left and right movement of the legend. The larger the value, the more it moves to the right;
num2 is used to control the up and down movement of the legend. The larger the value, the more it moves upward.

the case

1. No parameters

"""
    绘制饼图
"""
import numpy as np
import matplotlib.pyplot as plt

# 构建窗口
plt.figure('Computer language', facecolor='lightgray')
# 设置标题
plt.title("Computer language's popularity", fontdict={
    
    'fontsize': 24, 'color': 'r'})
# 绘制饼图
x = np.array([23, 30, 13, 24, 10])
explode = [0.05, 0.01, 0.01, 0.01, 0.01]
language_class = ['python', 'java', 'hadoop', 'C++', 'C']
color = ['r', 'blue', 'yellow', 'orange', 'white']
plt.pie(x=x, explode=explode, labels=language_class,
        colors=color, autopct='%1.1f%%', shadow=True)
# 设置图例
plt.legend()
plt.axis('equal')
# 展示
plt.show()

Show results:
insert image description here

2. Add parameters

plt.legend(loc=3, bbox_to_anchor=(-0.12, -0.12))

Show results:
insert image description here

Guess you like

Origin blog.csdn.net/m0_51489557/article/details/130065239