画饼图及条形图 带数字及百分比

import matplotlib.patches as patches
df = data_new.groupby('qcut_daily_order_value').size().reset_index(name='counts')
#
X = ['(-3, 0]', '(0, 3]', '(3, 6]', '(6, 9]', '(9, 12]', '(12, 15]']
fig, ax = plt.subplots(figsize=(15,8)
                       ,facecolor='white'
                       ,dpi=80)
ax.vlines(x=df.index
          , ymin=0
          ,ymax=df.counts
          ,color='firebrick'
          ,alpha=1
          ,linewidth=30)

for i,count in enumerate(df.counts):
    #第一个参数 横坐标对应位置  第二个参数 纵坐标对应位置  第三个参数 要表示的文字
    ax.text(i,count+50000, '{:.2f}% ({})' .format(((count/np.sum(df.counts))*100),count) ,
            horizontalalignment='center')
    
ax.set_title('test',fontdict={'size':22})
ax.set_ylabel('test',fontdict={'size':16})

ax.set(ylabel='y',ylim=(0,4000000))
plt.xticks(df.index #显示在横坐标上的位置
           , df.qcut_daily_order_value.astype(str)#显示在横坐标上的名字
           ,rotation=50  #是否进行旋转
           ,horizontalalignment='right'
           ,fontsize=12)

画饼图及条形图 带数字及百分比_第1张图片

#根据分箱后的结果,画出每个箱中数据所占的百分比
# Prepare Data

df = data_new.groupby('ccccccccc').size().reset_index(name='counts')
# Draw Plot
fig, ax = plt.subplots(figsize=(12, 10), subplot_kw=dict(aspect="equal"), dpi= 80)
data = df['counts']
categories = df['ccccccccc']
explode = np.zeros(df.shape[0])
def func(pct, allvals):
    absolute = int(pct/100.*np.sum(allvals))
    return "{:.1f}% ({:d} )".format(pct, absolute)
wedges, texts, autotexts = ax.pie(data, 
                                  autopct=lambda pct: func(pct, data),
                                  textprops=dict(color="w"), 
                                  colors=plt.cm.Dark2.colors,
                                 startangle=200
                                 ,explode=explode)
# Decoration

ax.legend(wedges, categories, title="bins", loc="center left", bbox_to_anchor=(1, 0, 0.5, 1))
plt.setp(autotexts, size=10, weight=700)
ax.set_title(" test")
# plt.show()

画饼图及条形图 带数字及百分比_第2张图片

你可能感兴趣的:(可视化)