python连接数据库生成 折线图,柱状图,饼图

本文介绍了如何使用Python连接数据库并从数据库中读取数据,然后利用Matplotlib库绘制折线图、柱状图和饼图,以直观展示学生的学习成绩。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

折线图
import pymysql
import matplotlib.pyplot as plt
mmm=pymysql.connect(“localhost”,“root”,“000000”,“cion”) #打开数据库
cursor=mmm.cursor() #创建游标
sql='select chinesse,english,amth,history,geographic from list1 '#创建sql指令
cursor.execute(sql) #执行sql指令
y1=cursor.fetchone() #获取sql数据
y2=cursor.fetchone()
y3=cursor.fetchone()
mmm.close()

x=(‘chinesse’,‘english’,‘math’,‘history’,‘geographic’)
plt.plot(x,y1,label=‘小白’)
plt.plot(x,y2,label=‘小红’)
plt.plot(x,y3,label=‘小黑’)
plt.title(“成绩对比”)
plt.rcParams[‘font.sans-serif’]=[‘SimHei’]
plt.legend()
plt.show()

在这里插入图片描述
柱状图
import pymysql
import matplotlib.pyplot as plt
mmm=pymysql.connect(“localhost”,“root”,“000000”,“cion”) #打开数据库
cursor=mmm.cursor() #创建游标
sql='select chinesse,english,amth,history,geographic from list1 '#创建sql指令
cursor.execute(sql) #执行sql指令
y1=cursor.fetchone() #获取sql数据
mmm.close()

x=(‘语文’,‘英语’,‘数学’,‘历史’,‘地理’)
plt.bar(x,y1)
plt.title(“小明的期末成绩”)
plt.xlabel(“科目”)
plt.ylabel(“成绩”)
plt.legend()
plt.rcParams[‘font.sans-serif’]=[‘SimHei’]
plt.show()
在这里插入图片描述
饼图
import pymysql
import matplotlib.pyplot as plt
mmm=pymysql.connect(“localhost”,“root”,“000000”,“cion”)
cursor=mmm.cursor()
sql=("select chinesse,english,amth,history,geographic from list1 ")
cursor.execute(sql)
y=cursor.fetchone()
mmm.close()
x=(‘chinesse’,‘english’,‘math’,‘history’,‘geographic’)
plt.pie(y,labels=x,autopct=’%1.1f%%’)
plt.title(“期末成绩”)
plt.rcParams[‘font.sans-serif’]=[‘SimHei’]
plt.show()
在这里插入图片描述
不懂请关注作者都会回答你哦

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值