import matplotlib.pyplot as plt
plt.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=None, data=None)
常用参数:
x
即每个扇形的占比的序列或数组explode
如果不是None,则是一个len(x)长度的数组,指定每一块的突出程度;突出显示,设置每一块分割出来的间隙大小labels
为每个扇形提供标签的字符串序列colors
为每个扇形提供颜色的字符串序列autopct
如果它是一个格式字符串,标签将是fmt % pct。如果它是一个函数,它将被调用。shadow
阴影startangle
从x轴逆时针旋转,饼的旋转角度pctdistance, default: 0.6
每个饼片的中心与由autopct生成的文本的开头之间距离与半径的比率,大于1的话会显示在圆外labeldistance, default: 1.1
饼状图标签绘制时的径向距离(我认为这个也与8类似是个比率)。如果设置为None,则不绘制标签,而是存储在图例()中使用。
import matplotlib.pyplot as plt
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' # 定义标签
sizes = [15, 30, 45, 10] # 每一块的比例
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] # 每一块的颜色
explode = (0, 0.1, 0, 0) # 突出显示,这里仅仅突出显示第二块(即'Hogs')
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90)
plt.axis('equal') # 显示为圆(避免比例压缩为椭圆)
plt.show()
selected = links.loc[links.Types == '酒精饮料']
alcohol = selected['id'].sum()
selected['count'] = selected.apply(lambda x : x['id']/alcohol,axis=1)
selected.rename(columns={'count':'percent'},inplace=True)
data = selected['percent']
labels = selected['Goods']
plt.figure(figsize = (10,10)) # 设置画布大小
explode = (0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.08,0.3,0.1,0.3) # 设置每一块分割出的间隙大小
plt.pie(data,explode = explode,labels = labels,autopct = '%1.2f%%',
pctdistance = 1.1,labeldistance = 1.2)
plt.title("酒精饮料内部各商品的销量占比") # 设置标题
plt.axis('equal')
如果没有explode
参数,每一块都没有分割出来
alcohol = selected['id'].sum()
selected['count'] = selected.apply(lambda x : x['id']/alcohol,axis=1)
selected.rename(columns={'count':'percent'},inplace=True)
data = selected['percent']
labels = selected['Goods']
plt.figure(figsize = (10,10)) # 设置画布大小
# explode = (0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.08,0.3,0.1,0.3) # 设置每一块分割出的间隙大小
plt.pie(data,labels = labels,autopct = '%1.2f%%',
pctdistance = 1.1,labeldistance = 1.2)
plt.title("酒精饮料内部各商品的销量占比") # 设置标题
plt.axis('equal')
如果没有pctdistance
参数,则格式字符串会默认显示在圆内
alcohol = selected['id'].sum()
selected['count'] = selected.apply(lambda x : x['id']/alcohol,axis=1)
selected.rename(columns={'count':'percent'},inplace=True)
data = selected['percent']
labels = selected['Goods']
plt.figure(figsize = (10,10)) # 设置画布大小
# explode = (0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.08,0.3,0.1,0.3) # 设置每一块分割出的间隙大小
plt.pie(data,labels = labels,autopct = '%1.2f%%',
labeldistance = 1.2)
plt.title("酒精饮料内部各商品的销量占比") # 设置标题
plt.axis('equal')
对于很好的类别,格式字符串会重叠,所以这个参数也比较重要。
如果对您有帮助,麻烦点赞关注,这真的对我很重要!!!如果需要互关,请评论留言!