Python Bokeh可视化:婚姻状况与工作类型占比分析
数据预处理
marital_job = df.groupby(['marital', 'job']).size().reset_index(name='counts') marital_job['percent'] = marital_job['counts'] / df.shape[0] * 100
创建绘图空间
p = figure(title='Marital Status and Job Distribution', x_range=marital_job['job'].unique(), plot_width=800, plot_height=400)
绘制堆叠柱状图
colors = ['#c9d9d3', '#718dbf', '#e84d60', '#ddb7b1', '#9e9ac8', '#b5cf6b', '#f9a65a', '#bab0ac'] marital_status = marital_job['marital'].unique() for i, status in enumerate(marital_status): subset = marital_job[marital_job['marital'] == status] p.vbar(x=subset['job'], top=subset['percent'], width=0.9, color=colors[i], legend_label=status)
设置坐标轴标签等
p.xaxis.major_label_orientation = 1.2 p.xaxis.axis_label = 'Job' p.yaxis.axis_label = 'Percentage' p.legend.location = 'top_left'
输出图像
output_file('marital_job.html') show(p)
原文地址: https://www.cveoy.top/t/topic/jjXK 著作权归作者所有。请勿转载和采集!