Key points:
Argsparser receives array arguments
Matplotlib displays Chinese questions
Numpy.random. Rand randomly generates colors
import argparse import matplotlib.pylab as plt from matplotlib.patches import Wedge,Rectangle import numpy as np import matplotlib as mpl fig,ax=plt.subplots(subplot_kw={"aspect":"equal"}) mpl.rcParams['font.family'] = ['sans-serif'] mpl.rcParams['font.sans-serif'] = ['SimHei'] parser=argparse.ArgumentParser() parser.add_argument('--items',type=str,nargs="+",help="the items to be painted,expected list") parser.add_argument('--amount',type=float,nargs="+",help="the amount of the items, expected as list") args=parser.parse_args() amount=0 for i in range(len(args.items)): Amount =amount+args. Amount [I] start_angle=45 end_angle=45 wedge=[] Rectangle =[] start_re=0.1 for I in range(len(args.items)): color=np.random.rand(4) end_angle=end_angle+360*args.amount[i]/amount ,1.9 newWedge = Wedge ((2), 1, start_angle, end_angle, facecolor = color, edgecolor = (0,0,0,1)) rectangle. Append (a rectangle (3.6, Ax. text(4.2, start_re, "%s:%3.1f%%" %(args. Items [I],(args. Amount [I] / amount * 100))) start_re += 0.4 wedge. Append (newWedge) start_angle=end_anglefor re in rectangle: ax.add_patch(re) for wed in wedge: Ax, ax. Add_patch (wed) axis ([0, 8, 0.5, 4]) PLT. The show ()Copy the code
Effect:
Py --items Apple, pear, grape, banana, orange, watermelon, mango, pomegranate --amount 1 2 3 4 5 6 7 8Copy the code