李克特五点计分图示

李克特量表常用的选项形式是五点计分,对这些题项进行可视化,以清晰展现每道题上所有受测者选择的情况。这样的方案有很多种。我先给出基于堆积条形图的方案。
该方案需要使用到Python的matplotlib库。

def likert_type_pic(df,col_ls):
    '''
    输入一个DataFrame,以及likert五点计分题对应的列名称,
    就可以输出一个含有多个五点计分的横向堆积条形图,
    每个条的yticks对应的是其变量名称,堆积条上有五个类别的百分比信息,
    基于matplotlib.pyplot numpy 和pandas绘制而成。
    '''
    df_use = df[col_ls]
    df_counts = pd.DataFrame(np.zeros((5,len(col_ls))),columns=col_ls,index=[1,2,3,4,5])
    for col in col_ls:
        median = df[col].value_counts()
        name = df_raw[col].name
        df_counts[name] = median # 将每个变量每种分值的个案数都记录下来
    print(df_counts)
    
    # 开始绘图

    fig,ax = plt.subplots(1,figsize=(14,4))
    label_ls = ['非常不赞同','比较不赞同','不确定','比较赞同','非常赞同']
#     color_ls = ['#b3cde0','#6497b1','#005b96','#03396c','#011f4b']
    color_ls = ['#fa3c4c','#0084ff','#44bec7','#ffc300','#005b96']
#     画堆积图
    a1 = np.array(df_counts.loc[1,:])
    a2 = np.array(df_counts.loc[2,:])
    a3 = np.array(df_counts.loc[3,:])
    a4 = np.array(df_counts.loc[4,:])
    a5 = np.array(df_counts.loc[5,:])
    long = [i for i in range(1,len(col_ls)+1)]
    ax.barh(long,a1,color=[color_ls[0]]*len(col_ls),label=label_ls[0],height=0.6)
    ax.barh(long,a2,color=[color_ls[1]]*len(col_ls),label=label_ls[1],left=a1,height=0.6)
    ax.barh(long,a3,color=[color_ls[2]]*len(col_ls),label=label_ls[2],left=a1+a2,height=0.6)
    ax.barh(long,a4,color=[color_ls[3]]*len(col_ls),label=label_ls[3],left=a1+a2+a3,height=0.6)
    ax.barh(long,a5,color=[color_ls[4]]*len(col_ls),label=label_ls[4],left=a1+a2+a3+a4,height=0.6)
    
    
    for col in [i for i in range(len(col_ls))]: # 为每一个数量添加文字,文字添加在那一条的中间位置,应该是前几个值总和加上当下值的一半
        plt.text(np.array(df_counts.iloc[0,col])/2,col+1, '{0}'.format(np.array(df_counts.iloc[0,col])), ha='center', va='center', fontsize=14,color='w')
        x1 = np.array(df_counts.iloc[0,col])
        plt.text(x1 + np.array(df_counts.iloc[1,col])/2,col+1, '{0}'.format(np.array(df_counts.iloc[1,col])), ha='center', va='center', fontsize=14,color='w')
        x2 = x1 + np.array(df_counts.iloc[1,col])
        plt.text(x2 + np.array(df_counts.iloc[2,col])/2,col+1, '{0}'.format(np.array(df_counts.iloc[2,col])), ha='center', va='center', fontsize=14,color='w')
        x3 = x2 + np.array(df_counts.iloc[2,col])
        plt.text(x3 + np.array(df_counts.iloc[3,col])/2,col+1, '{0}'.format(np.array(df_counts.iloc[3,col])), ha='center', va='center', fontsize=14,color='w')
        x4 = x3 + np.array(df_counts.iloc[3,col])
        plt.text(x4 + np.array(df_counts.iloc[4,col])/2,col+1, '{0}'.format(np.array(df_counts.iloc[4,col])), ha='center', va='center', fontsize=14,color='w')
    
    y_ls = []
    for j in col_ls:
        x = j[:-3]
        y_ls.append(x)
    
    plt.yticks([i for i in range(1,len(col_ls)+1)],y_ls,fontsize=16)
    all_num = df_use.shape[0]
    plt.xticks([all_num],[''])
    ax.spines.clear()
    plt.ylim((-0.5,len(col_ls)+0.5))
    plt.legend(loc='lower center',ncol=5,mode=None,facecolor='inherit',edgecolor='w',fontsize=12)
    
    plt.show()
    return None

实现的效果如下:


likert五点计分图示
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容