数据预处理

df['issue_d'] = pd.to_datetime(df['issue_d']) df['year'] = df['issue_d'].apply(lambda x: x.year) df['month'] = df['issue_d'].apply(lambda x: x.month)

计算lending_rate3m的平均值

avg_lending_rate3m = df.groupby(['year', 'month'])['lending_rate3m'].mean().reset_index()

统计不同job的数量

job_count = df.groupby(['year', 'month', 'job'])['id'].count().reset_index() job_count['job'] = job_count['job'].astype(str)

创建绘图空间

p = figure(title='Lending Rate 3M and Job Analysis', x_axis_label='Time', y_axis_label='Lending Rate 3M')

添加平均lending_rate3m折线图

p.line(x=np.arange(len(avg_lending_rate3m)), y=avg_lending_rate3m['lending_rate3m'], line_width=2, color='navy')

添加job数量堆叠柱状图

colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink', 'brown', 'gray'] for i, job in enumerate(job_count['job'].unique()): p.vbar(x=np.arange(len(job_count)), top=job_count[job_count['job'] == job]['id'], width=0.9, color=colors[i], legend_label=job)

布局设置

p.legend.location = 'top_left' p.xaxis.ticker = np.arange(len(avg_lending_rate3m)) p.xaxis.major_label_overrides = {i: f"{avg_lending_rate3m['year'][i]}-{avg_lending_rate3m['month'][i]}" for i in range(len(avg_lending_rate3m))} p.xaxis.major_label_orientation = np.pi / 4 p.y_range.start = 0

输出图像

output_file('lending_rate3m_and_job_analysis.html') show(p

使用python的bokeh库对lending_rate3m的平均值和job进行可视化分析布局合理from bokehplotting import figure showoutput_fileimport numpy as npimport pandas as pdimport pandasdf = pdread_csvrUsersfuchuanruoDesktop可视化作业实践traincsv

原文地址: https://www.cveoy.top/t/topic/fbyX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录