Housing and Default Relationship Visualization using Python Bokeh
from bokeh.plotting import figure, show, output_file import numpy as np import pandas as pd import pandas df = pd.read_csv(r'/Users/fuchuanruo/Desktop/可视化作业/实践/train.csv') test = pd.read_csv(r'/Users/fuchuanruo/Desktop/可视化作业/实践/test.csv')
计算housing和default关系
housing_default = df.groupby('housing')['default'].value_counts(normalize=True).unstack() housing_default
创建绘图空间
p = figure(x_range=housing_default.index.tolist(), plot_height=350, title='Housing vs Default', toolbar_location=None, tools="")
添加柱状图
p.vbar_stack(housing_default.columns.tolist(), x='housing', width=0.9, color=['#c9d9d3', '#718dbf'], source=housing_default, legend_label=housing_default.columns.tolist())
设置图例位置
p.legend.location = 'top_right' p.legend.orientation = 'vertical'
设置x轴标签
p.xaxis.axis_label = 'Housing'
设置y轴标签
p.yaxis.axis_label = 'Percentage'
输出图像
output_file('housing_default.html') show(p)
原文地址: https://www.cveoy.top/t/topic/jjX0 著作权归作者所有。请勿转载和采集!