使用GPT生成python图表

首先,生成一脚本,读取到所需的excel表格

python 复制代码
import xlrd

data = xlrd.open_workbook('xxxx.xls')  # 打开xls文件
table = data.sheet_by_index(0)  # 通过索引获取表格

# 初始化奖项字典
awards_dict = {
    "一等奖": 0,
    "二等奖": 0,
    "三等奖": 0
}

# 初始化科目字典
subjects_dict = {}

# 遍历表格的每一行,跳过表头
for i in range(1, table.nrows):
    row = table.row_values(i)
    # 是xx大学才进行处理
    if row[2] == 'xx大学':
        # 转为小写,避免首字母不一样带来的误判
        subject = row[4].lower()
        # 获得的奖项
        award = row[5]
        # 加入字典
        if subject not in subjects_dict:
            subjects_dict[subject] = awards_dict.copy()
        subjects_dict[subject][award] += 1

# 将结果写入文件
with open("result.txt", "w") as f:
    for subject, awards in subjects_dict.items():
        f.write(f"{subject}\n")
        for award, count in awards.items():
            f.write(f"{award}: {count}\n")
        f.write("\n")

matplotlib

我们要GPT根据上面的代码,用直方图展示xx大学在各个类别中的获奖情况,也就是去对这个文本文件进行数据可视化,于是chatGPT就生成了以下的代码

python 复制代码
import xlrd
import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['font.sans-serif'] = ['SimHei']    # 设置中文字体,不然显示不了中文
data = xlrd.open_workbook('xxxx.xls')  # 打开xls文件
table = data.sheet_by_index(0)  # 通过索引获取表格

# 初始化奖项字典
awards_dict = {
    "一等奖": 0,
    "二等奖": 0,
    "三等奖": 0
}

# 初始化科目字典
subjects_dict = {}

# 遍历表格的每一行,跳过表头
for i in range(1, table.nrows):
    row = table.row_values(i)
    if row[2] == 'xx大学':
        # 转为小写,避免首字母不一样带来的误判
        subject = row[4].lower()
        award = row[5]
        if subject not in subjects_dict:
            subjects_dict[subject] = awards_dict.copy()
        subjects_dict[subject][award] += 1

# 直方图展示xx大学在各个类别中的获奖情况
x_labels = subjects_dict.keys()
x = np.arange(len(x_labels))  # 使用numpy的arange生成连续的横坐标值

# 提取各个奖项的数量
y_first = [subjects_dict[subject]['一等奖'] for subject in x_labels]
y_second = [subjects_dict[subject]['二等奖'] for subject in x_labels]
y_third = [subjects_dict[subject]['三等奖'] for subject in x_labels]

# 设置颜色
color_first = 'skyblue'
color_second = 'lightgreen'
color_third = 'lightcoral'

# 绘制直方图
plt.bar(x, y_third, width=0.2, align='center', label='三等奖', color=color_third)
plt.bar(x + 0.2, y_second, width=0.2, align='center', label='二等奖', color=color_second)
plt.bar(x - 0.2, y_first, width=0.2, align='center', label='一等奖', color=color_first)

plt.xlabel('类别')
plt.ylabel('获奖数量')
plt.title('xx大学在各个类别中的获奖情况')
plt.xticks(x, x_labels)
plt.legend()

# 调整图例位置和边框样式
plt.legend(loc='upper right', frameon=False)

# 设置图形背景色
plt.gca().set_facecolor('whitesmoke')

# 调整图形布局
plt.tight_layout()

plt.show()

pyecharts

pyecharts 是一个用于生成 Echarts 图表的类库。 Echarts 是百度开源的一个数据可视化 JS 库。

python 复制代码
import xlrd
from pyecharts.charts import Bar
from pyecharts import options as opts

data = xlrd.open_workbook('xxxx.xls')  # 打开xls文件
table = data.sheet_by_index(0)  # 通过索引获取表格

# 初始化奖项字典
awards_dict = {
    "一等奖": 0,
    "二等奖": 0,
    "三等奖": 0
}

# 初始化科目字典
subjects_dict = {}

# 遍历表格的每一行,跳过表头
for i in range(1, table.nrows):
    row = table.row_values(i)
    if row[2] == 'xx大学':
        # 转为小写,避免首字母不一样带来的误判
        subject = row[4].lower()
        award = row[5]
        if subject not in subjects_dict:
            subjects_dict[subject] = awards_dict.copy()
        subjects_dict[subject][award] += 1

# 直方图展示xx大学在各个类别中的获奖情况
x_labels = subjects_dict.keys()

# 提取各个奖项的数量
y_first = [subjects_dict[subject]['一等奖'] for subject in x_labels]
y_second = [subjects_dict[subject]['二等奖'] for subject in x_labels]
y_third = [subjects_dict[subject]['三等奖'] for subject in x_labels]

# 使用 Pyecharts 绘制直方图
bar = (
    Bar()
    .add_xaxis(list(x_labels))
    .add_yaxis('一等奖', y_first)
    .add_yaxis('二等奖', y_second)
    .add_yaxis('三等奖', y_third)
    .set_global_opts(
        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
        yaxis_opts=opts.AxisOpts(name='获奖数量'),
        title_opts=opts.TitleOpts(title='xx大学在各个类别中的获奖情况'),
        legend_opts=opts.LegendOpts(pos_right='5%', pos_top='20%')
    )
)

# 生成图表并保存为 HTML 文件
bar.render('bar_chart.html')
相关推荐
Sirius.z18 小时前
第R6周:LSTM实现糖尿病探索与预测
python
名字还没想好☜18 小时前
Python f-string 进阶:数字格式化、对齐填充、调试 = 号与嵌套表达式
开发语言·数据库·python·字符串格式化·f-string
·薯条大王18 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
Dxy123931021619 小时前
Python 如何使用 MySQL 的事务
python·mysql
ZJH__GO1 天前
网络编程v4pro--实现聊天室文件传输功能
java·服务器·网络·计算机网络
程序员黑豆1 天前
Windows 系统 Java 环境变量配置全攻略:解决“不是内部或外部命令”
java·前端·ai编程
命运之光1 天前
【C语言完整代码】就诊信息管理系统
java·c语言·开发语言
凤山老林1 天前
Spring Boot @Async 线上实战:从默认配置到生产级线程池治理
java·spring boot·后端
marvelyu1 天前
每天10分钟学会OceanBase系列(Day 20):跨机房容灾实战——构建多数据中心高可用架构
java·大数据·数据库
今儿敲了吗1 天前
Python ——第三方包
笔记·python