|
| 1 | +# @Time : 2020/5/14 11:02 |
| 2 | +# @Author : Libuda |
| 3 | +# @FileName: main.py |
| 4 | +# @Software: PyCharm |
| 5 | + |
| 6 | +import pandas as pd |
| 7 | +import random |
| 8 | +import matplotlib.pyplot as plt |
| 9 | +import matplotlib |
| 10 | + |
| 11 | + |
| 12 | +def guessnum(Number): |
| 13 | + """ |
| 14 | + 传入数字返回猜到这个数字用的次数 |
| 15 | + :param Number: |
| 16 | + :return: |
| 17 | + """ |
| 18 | + n = 0 |
| 19 | + Min = 0 |
| 20 | + Max = 10 |
| 21 | + # Number = random.randint(0, 100) # 随机生成0-100的整数 |
| 22 | + flag = True |
| 23 | + Num = random.randint(0, 10) |
| 24 | + |
| 25 | + while flag: |
| 26 | + if Num < Number: |
| 27 | + Min = Num |
| 28 | + b = Max - Min |
| 29 | + c = b // 2 |
| 30 | + Num += c |
| 31 | + n += 1 |
| 32 | + elif Num > Number: |
| 33 | + Max = Num |
| 34 | + b = Max - Min |
| 35 | + c = b // 2 |
| 36 | + Num -= c |
| 37 | + n += 1 |
| 38 | + else: |
| 39 | + flag = False |
| 40 | + |
| 41 | + return Number, n |
| 42 | + |
| 43 | + |
| 44 | +def res_to_csv(Number, guss_count): |
| 45 | + """ |
| 46 | + 将数字及其猜测次数保存到csv |
| 47 | + :param Number: |
| 48 | + :param guss_count: |
| 49 | + :return: |
| 50 | + """ |
| 51 | + excel_path = "res.xls" |
| 52 | + old_data = pd.read_excel(excel_path) |
| 53 | + df = pd.DataFrame(old_data) |
| 54 | + result = [] |
| 55 | + res = {} |
| 56 | + res["Number"] = str(Number) |
| 57 | + res["guss_count"] = str(guss_count) |
| 58 | + result.append(res) |
| 59 | + |
| 60 | + # 最恶心的地方就是必须将其返回 |
| 61 | + df = df.append(result) |
| 62 | + df.to_excel(excel_path, sheet_name='biubiu', index=0) |
| 63 | + |
| 64 | + |
| 65 | +def show_res(): |
| 66 | + # 设置matplotlib正常显示中文和负号 |
| 67 | + matplotlib.rcParams['font.sans-serif'] = ['SimHei'] |
| 68 | + matplotlib.rcParams['axes.unicode_minus'] = False |
| 69 | + data = pd.read_excel("res.xls") |
| 70 | + Number = data["Number"] |
| 71 | + guss_count = data['guss_count'] |
| 72 | + # num_list2 = [15, 30, 40, 20] # 纵坐标值2 |
| 73 | + # x = range(len(num_list1)) |
| 74 | + """ |
| 75 | + 绘制条形图 |
| 76 | + left:长条形中点横坐标 |
| 77 | + height:长条形高度 |
| 78 | + width:长条形宽度,默认值0.8 |
| 79 | + label:为后面设置legend准备 |
| 80 | + """ |
| 81 | + plt.bar(left=Number, height=guss_count, width=0.4, alpha=0.8, color='red') |
| 82 | + plt.ylim(0, 10) # y轴取值范围 |
| 83 | + plt.ylabel("猜测次数") |
| 84 | + plt.xticks([index + 0.2 for index in Number], Number) |
| 85 | + # plt.xticks([index for index in x], label_list) |
| 86 | + plt.xlabel("预想值") |
| 87 | + plt.title("AI猜数可视化") |
| 88 | + plt.legend() # 设置题注 |
| 89 | + # 编辑文本 |
| 90 | + |
| 91 | + for a, b in zip(Number, guss_count): |
| 92 | + print(a, b) |
| 93 | + plt.text(a, b + 0.1, '%.0f' % b, ha='center', va='bottom', fontsize=7) |
| 94 | + |
| 95 | + # for rect in rects1: |
| 96 | + # height = rect.get_height() |
| 97 | + # plt.text(rect.get_x() + rect.get_width() / 2, height + 1,height, ha="center", va="bottom") |
| 98 | + plt.show() |
| 99 | + |
| 100 | + |
| 101 | +def main(): |
| 102 | + # 0-10为预先想好的数字 |
| 103 | + for i in range(1, 10): |
| 104 | + print(i) |
| 105 | + number, guss_count = guessnum(i) |
| 106 | + print("数字%s,猜了%s次" % (number, guss_count)) |
| 107 | + res_to_csv(number, guss_count) |
| 108 | + # print("——————————") |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == '__main__': |
| 112 | + # 注意 猜测同样的数字需将excel情况 否则可视化显示有问题 |
| 113 | + |
| 114 | + # 1.猜测数字并写入excel |
| 115 | + # main() |
| 116 | + |
| 117 | + # 2.可视化 |
| 118 | + show_res() |
0 commit comments