python matplotlib notes: pie chart

1. Pie chart parameters:

matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, 
autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, 
startangle=0, radius=1, counterclock=True, wedgeprops=None, 
textprops=None, center=(0, 0), frame=False, rotatelabels=False, *,
normalize=True, data=None)

explode: Set each part to highlight
the label: list type, set the label data
colors: array-like, the color of each pie piece
autopct: Set the text inside the circle, usually a percentage format: fmt % pct
labeldistance: Set the distance between the label text and the center of the circle, 1.1 Represents 1.1 times the radius
shadow: Sets whether there is a shadow
startangle: Starting angle, the default starts from 0 and turns counterclockwise
pctdistance: Sets the distance between the text in the circle and the center of the circle

2. Sample:

# 导入包
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
import tushare as ts  
# 导入数据
ts.set_token("****************************")  
pro = ts.pro_api()
#查询当前所有正常上市交易的股票列表
data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
data

List of listed companies

# 修改字体(默认字体可能会存在无法显示的情况)
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
# 按照公司所在地区分类
cate_df = data.groupby("area").count()
categories = cate_df.index
count_num = cate_df.name
plt.figure(figsize=(12, 10), dpi=120)
plt.pie(count_num, labels=categories, autopct="%1.2f%%",
        textprops={'fontsize': 24}, labeldistance=1.25)
plt.title("各省市上市公司数量占比", fontsize=24)
# 保存图片
plt.savefig("./image/饼状图-各省市上市公司数量占比01.png")
plt.show()

Pie chart-proportion of the number of listed companies in each province and city01
You can see that there is overlap in a lot of data. You can optimize the picture by adjusting the font size and the distance between the text and the center of the circle;

# 修改字体
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
# 按照公司所在地区分类
cate_df = data.groupby("area").count()
# 按照百分比把饼凸显出来
cate_df["precents"] = cate_df["name"]* 2.5 / cate_df["name"].sum()
cate_df = cate_df.reset_index()
categories = cate_df.area
count_num = cate_df.name
explode = cate_df.precents
plt.figure(figsize=(10, 10), dpi=100)
plt.pie(count_num,explode, labels=categories, autopct="%1.1f%%",
        textprops={
    
    'fontsize': 16}, labeldistance=1.1,pctdistance=0.9,
        # 偏移角度防止把标题上的字挡住
        startangle=230)
# plt.legend(fontsize=16)
plt.title("各省市上市公司数量占比", fontsize=26)
# 保存图形
plt.savefig("./image/饼状图-各省市上市公司数量占比02.png")
plt.show()

Optimized pie chart 02
Well, it looks uglier, more like COVID-19.

Therefore, for drawing pie charts, the suggestions are:
1. It is best not to exceed 10 categories (customized). If there are more, you can try to merge small categories into other categories;
2. The position of the words and the prominent part of the picture need to be adjusted according to the data. If documents are not automatically generated or pictures are generated in a process, it may be more flexible to use Excel for a single picture;

Guess you like

Origin blog.csdn.net/weixin_39747882/article/details/128436004