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

折线图
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()

python连接数据库生成 折线图,柱状图,饼图
柱状图
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()
python连接数据库生成 折线图,柱状图,饼图
饼图
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()
python连接数据库生成 折线图,柱状图,饼图
不懂请关注作者都会回答你哦