使用python的bokeh库对housing是否有房贷和default信用卡是否违约条形图判断房贷和违约比例进行可视化分析布局合理from bokehplotting import figure showoutput_fileimport numpy as npimport pandas as pdimport pandasdf = pdread_csvrUsersfuchuanruoDeskt
计算房贷和违约比例
housing_count = df['housing'].value_counts() default_count = df['default'].value_counts() housing_ratio = round(housing_count[1] / len(df), 2) default_ratio = round(default_count[1] / len(df), 2)
创建条形图
p = figure(title="Housing and Default Ratio", x_range=["Housing", "Default"], y_range=(0, 1), plot_width=400, plot_height=400) p.vbar(x=["Housing"], top=[housing_ratio], width=0.5, color='#FFC107') p.vbar(x=["Default"], top=[default_ratio], width=0.5, color='#2196F3')
设置样式
p.title.text_font_size = '20pt' p.xaxis.axis_label_text_font_size = '16pt' p.yaxis.axis_label_text_font_size = '16pt' p.xaxis.major_label_text_font_size = '14pt' p.yaxis.major_label_text_font_size = '14pt'
输出文件
output_file("housing_default_ratio.html")
显示图形
show(p
原文地址: https://www.cveoy.top/t/topic/fdz8 著作权归作者所有。请勿转载和采集!