码字小白菜努力中 2022-12-15 21:42 采纳率: 88%
浏览 5
已结题

关于#matplotlib#的问题,如何解决?

不同城市分布的数量想要画出一个图。


import xlrd
import matplotlib.pyplot as plt

if __name__ == "__main__":
    # 读取表格
    data = xlrd.open_workbook("../data_spark/city.xls")
    # 获取表格的sheets
    table = data.sheets()[0]
    # 行
    rows = table.nrows
    print("xls行数:", rows)
    # 列
    cols = table.ncols
    print("xls列数:", cols)
    # 获取第一行数据
    row1data = table.row_values(0)
    print(row1data)  # 输出标题行

    # 存储所有表格信息
    tables = []
    for rows in range(1, table.nrows):
        dict_ = {"职位名": "job_id",
                 "工作城市": "work_city",
                 "学历要求": "stu",
                 "最低年薪资": "salary_min",
                 "最高年薪资": "salary_max",
                 "平均年薪资": "salary_avg"
                 }
        dict_["job_id"] = table.cell_value(rows, 0)
        dict_["work_city"] = table.cell_value(rows, 1)
        dict_["stu"] = table.cell_value(rows, 2)
        dict_["salary_min"] = table.cell_value(rows, 3)
        dict_["salary_max"] = table.cell_value(rows, 4)
        dict_["salary_avg"] = table.cell_value(rows, 5)
        tables.append(dict_)

    def statistical_data(tables):
        res_dict = {}
        for data in tables:
            city = data.get('work_city')
            if city not in res_dict:
                res_dict[city] = 1  # 初始数量1
            else:
                res_dict[city] += 1  # 相同key对应的数量+1
        return res_dict
    result = statistical_data(tables)
    print(result)

    for i in tables:
        data1 = data[i['work_city']]
        _x = data1.index
        _y = result.values

        # 画图
        plt.figure(figsize=(20, 8), dpi=80)

        plt.bar(range(len(_x)), _y)

        plt.xticks(range(len(_x)), _x)

        plt.show()

img

img

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 12月23日
    • 创建了问题 12月15日

    悬赏问题

    • ¥30 为什么会失败呢,该如何调整
    • ¥50 如何在不能联网影子模式下的电脑解决usb锁
    • ¥20 服务器redhat5.8网络问题
    • ¥15 如何利用c++ MFC绘制复杂网络多层图
    • ¥20 要做柴油机燃烧室优化 需要保持压缩比不变 请问怎么用AVL fire ESE软件里面的 compensation volume 来使用补偿体积来保持压缩比不变
    • ¥15 python螺旋图像
    • ¥15 算能的sail库的运用
    • ¥15 'Content-Type': 'application/x-www-form-urlencoded' 请教 这种post请求参数,该如何填写??重点是下面那个冒号啊
    • ¥15 找代写python里的jango设计在线书店
    • ¥15 请教如何关于Msg文件解析