题目:

以99男+99女的样本数据,绘制匹配折线图

要求:

  1. 生成样本数据,模拟匹配实验
  2. 生成绘制数据表格
  3. bokhe制图

    1. 这里设置图例,并且可交互(消隐模式)

      提示:

  4. bokeh制图时,y轴为男性,x轴为女性

  5. 绘制数据表格中,需要把男女性的数字编号提取出来,这样图表横纵轴好识别
  6. bokhe绘制折线图示意:p.line([0,女性数字编号,女性数字编号],[男性数字编号,男性数字编号,0])

导入库

  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import os
  5. import time
  6. #倒入时间模块
  7. import warnings
  8. warnings.filterwarnings('ignore')
  9. #不发出警告
  10. from bokeh.plotting import figure, show, output_file
  11. from bokeh.models import ColumnDataSource, HoverTool
  12. #导入bokeh绘图模块

生成样本数据,模拟实验

根据之前项目中实现的10000个男性与10000个女性样本的实验过程与结果,在此模拟生成99个男性与女性的样本进行试验

  1. sample1_m2 = create_sample(99, 'm')
  2. sample1_f2 = create_sample(99, 'f')
  3. sample1_m2['strategy'] = np.random.choice([1, 2, 3], 99)
  4. #创建样本数据
  5. test_m2 = sample1_m2.copy()
  6. test_f2 = sample1_f2.copy()
  7. #复制实验数据,创建实验副本
  8. n = 1
  9. #设置一个实验次数变量
  10. starttime = time.time()
  11. #设置起始时间
  12. success_roundn = different_strategy(test_m2, test_f2, n)
  13. match_success2 = success_roundn
  14. test_m2 = test_m2.drop(success_roundn['m'].tolist())
  15. test_f2 = test_f2.drop(success_roundn['f'].tolist())
  16. print('成功进行第%i轮试验,本轮实验成功匹配%i对,总共成功匹配%i对,还剩下%i位男性和%i为女性'%
  17. (n, len(success_roundn), len(match_success2), len(test_m2),len(test_f2)))
  18. #第一轮测试
  19. while len(success_roundn) != 0:
  20. n += 1
  21. success_roundn = different_strategy(test_m2,test_f2,n)
  22. #得到该轮成功匹配数据
  23. match_success2 = pd.concat([match_success2,success_roundn])
  24. # 将成功匹配数据汇总
  25. test_m2 = test_m2.drop(success_roundn['m'].tolist())
  26. test_f2 = test_f2.drop(success_roundn['f'].tolist())
  27. # 输出下一轮实验数据
  28. print('成功进行第%i轮实验,本轮实验成功匹配%i对,总共成功匹配%i对,还剩下%i位男性和%i位女性' %
  29. (n,len(success_roundn),len(match_success2),len(test_m2),len(test_f2)))
  30. endtime = time.time()
  31. #记录结束时间
  32. print('--------------------')
  33. print('本次实验总共进行了%i轮,配对成功%i对\n---------------'%(n, len(match_success2)))
  34. print('实验总共耗时%.2f秒'%(endtime - starttime))

生成绘制数据表格

为了接下里我们作图的需要,我们需要生成图像的点数据,并且设置每一循环对应的线条颜色,然后筛选出我们作图所需要的数据字段

  1. graphdata1 = match_success2.copy()
  2. graphdata1 = pd.merge(graphdata1,sample1_m2,left_on = 'm',right_index = True)
  3. graphdata1 = pd.merge(graphdata1,sample1_f2,left_on = 'f',right_index = True)
  4. # 合并数据,得到成功配对的男女各项分值
  5. graphdata1['x'] = '0,' + graphdata1['f'].str[1:] + ',' + graphdata1['f'].str[1:]
  6. graphdata1['x'] = graphdata1['x'].str.split(',')
  7. graphdata1['y'] = graphdata1['m'].str[1:] + ',' + graphdata1['m'].str[1:] + ',0'
  8. graphdata1['y'] = graphdata1['y'].str.split(',')
  9. # 筛选出id的数字编号,制作x,y字段
  10. from bokeh.palettes import brewer
  11. # 导入调色模块
  12. round_num = graphdata1['round_n'].max()
  13. color = brewer['Blues'][round_num+1] # 这里+1是为了得到一个色带更宽的调色盘,避免最后一个颜色太浅
  14. graphdata1['color'] = ''
  15. for rn in graphdata1['round_n'].value_counts().index:
  16. graphdata1['color'][graphdata1['round_n'] == rn] = color[rn-1]
  17. # 设置颜色
  18. graphdata1 = graphdata1[['m','f','strategy_type','round_n','score_x','score_y','x','y','color']]
  19. # 筛选字段

Bokeh绘图(pic1.html

  1. output_file('C:\\Users\\RaingEye\\Desktop\\数据分析项目实战\\婚恋配对实验\\pic1.html')
  2. p = figure(plot_width=500, plot_height=500,title="配对实验过程模拟示意" ,tools= 'reset,wheel_zoom,pan') # 构建绘图空间
  3. for datai in graphdata1.values:
  4. p.line(datai[-3],datai[-2],line_width=1, line_alpha = 0.8, line_color = datai[-1],line_dash = [10,4],legend= 'round %i' % datai[3])
  5. # 绘制折线
  6. p.circle(datai[-3],datai[-2],size = 3,color = datai[-1],legend= 'round %i' % datai[3])
  7. # 绘制点
  8. p.ygrid.grid_line_dash = [6, 4]
  9. p.xgrid.grid_line_dash = [6, 4]
  10. p.legend.location = "top_right"
  11. p.legend.click_policy="hide"
  12. # 设置其他参数
  13. show(p)

图片.png