以下为python语言采用bokeh库对字符型的job和数字类型的age可视化分析x轴字符不重叠鼠标在柱形图上时可以显示具体数量from bokehplotting import figure showoutput_fileimport numpy as npimport pandas as pdimport pandasdf = pdread_csvrUsersfuchuanruoDesktop
对job进行可视化分析
job_counts = df['job'].value_counts() job_counts_dict = dict(job_counts) job_counts_list = sorted(job_counts_dict.items(), key=lambda x:x[1], reverse=True) job_counts_df = pd.DataFrame(job_counts_list, columns=['job', 'count'])
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
p = figure(x_range=job_counts_df['job'], plot_width=800, plot_height=400, tools=TOOLS, title="Job Counts") p.vbar(x='job', top='count', width=0.9, source=job_counts_df, line_color='white')
p.xgrid.grid_line_color = None p.y_range.start = 0 p.xaxis.major_label_orientation = 1.2 p.hover.tooltips = [("Count", "@count")]
output_file("job_counts.html") show(p)
对age进行可视化分析
age_counts = df['age'].value_counts() age_counts_dict = dict(age_counts) age_counts_list = sorted(age_counts_dict.items(), key=lambda x:x[0]) age_counts_df = pd.DataFrame(age_counts_list, columns=['age', 'count'])
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
p = figure(plot_width=800, plot_height=400, tools=TOOLS, title="Age Counts") p.vbar(x='age', top='count', width=0.9, source=age_counts_df, line_color='white')
p.xgrid.grid_line_color = None p.y_range.start = 0 p.xaxis.major_label_orientation = 1.2 p.hover.tooltips = [("Count", "@count")]
output_file("age_counts.html") show(p
原文地址: http://www.cveoy.top/t/topic/e9Zz 著作权归作者所有。请勿转载和采集!