Python Bokeh交互式可视化: 房贷与信用卡违约关系分析
# 导入必要的库
from bokeh.plotting import figure, show, output_file
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral4
from bokeh.transform import factor_cmap
import pandas as pd
# 加载数据
df = pd.read_csv(r'/Users/fuchuanruo/Desktop/可视化作业/实践/train.csv')
test = pd.read_csv(r'/Users/fuchuanruo/Desktop/可视化作业/实践/test.csv')
# 数据预处理
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='房贷与信用卡违约关系',
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='房贷与信用卡违约关系',
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/jkqj 著作权归作者所有。请勿转载和采集!