使用python的bokeh库对housing是否有房贷和default信用卡是否违约进行可视化分析布局合理from bokehplotting import figure showoutput_fileimport numpy as npimport pandas as pdimport pandasdf = pdread_csvrUsersfuchuanruoDesktop可视化作业实践tra
对housing和default进行可视化分析
from bokeh.layouts import gridplot from bokeh.models import ColumnDataSource from bokeh.palettes import Spectral4 from bokeh.transform import factor_cmap
数据预处理
df['housing'] = df['housing'].replace({'no': 'No', 'yes': 'Yes'}) df['default'] = df['default'].replace({'no': 'No', 'yes': 'Yes'})
创建ColumnDataSource
source = ColumnDataSource(data=dict(x=df['housing'], y=df['default']))
创建第一个图表
p1 = figure(x_range=['No', 'Yes'], plot_height=350, title="Housing and Default", toolbar_location=None, tools="") p1.vbar(x='x', top='y', width=0.9, source=source, legend_field="x", line_color='white', fill_color=factor_cmap('x', palette=Spectral4, factors=['No', 'Yes']))
设置第一个图表属性
p1.xgrid.grid_line_color = None p1.legend.orientation = "horizontal" p1.legend.location = "top_center"
创建第二个图表
p2 = figure(plot_width=400, plot_height=400, title="Housing and Default", toolbar_location=None, tools="") p2.scatter(x='x', y='y', source=source, color=factor_cmap('x', palette=Spectral4, factors=['No', 'Yes']), legend_field="x", alpha=0.5, size=8)
设置第二个图表属性
p2.legend.location = "top_left"
将两个图表放在一起
layout = gridplot([[p1, p2]])
输出到HTML文件
output_file("housing_and_default.html")
显示图表
show(layout
原文地址: https://www.cveoy.top/t/topic/fdwR 著作权归作者所有。请勿转载和采集!