使用python的bokeh库对housing是否有房子和default是否违约关系可视化分析布局合理from bokehplotting import figure showoutput_fileimport numpy as npimport pandas as pdimport pandasdf = pdread_csvrUsersfuchuanruoDesktop可视化作业实践trainc
计算是否有房子和是否违约的比例
has_house_default = df.groupby(['has_house', 'default']).size().reset_index(name='count') has_house_default['percent'] = has_house_default['count'] / has_house_default['count'].sum()
创建绘图空间
p = figure(title="Housing and Default Relationship", plot_width=600, plot_height=400)
绘制饼图
p.wedge(x=0.4, y=0.5, radius=0.4, start_angle=np.pi, end_angle=0, color="#c9d9d3", legend_label="No Default", source=has_house_default[has_house_default['default'] == 0]) p.wedge(x=0.6, y=0.5, radius=0.4, start_angle=np.pi, end_angle=0, color="#718dbf", legend_label="Default", source=has_house_default[has_house_default['default'] == 1])
设置图例位置
p.legend.location = "top_right"
设置标题和轴标签
p.title.text_font_size = "16pt" p.xaxis.axis_label = "Has House" p.yaxis.axis_label = "Default"
输出图像
output_file("housing_default.html") show(p
原文地址: https://www.cveoy.top/t/topic/fbBJ 著作权归作者所有。请勿转载和采集!