国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

python讀取mysql數(shù)據(jù)繪制條形圖

瀏覽:4日期:2022-08-01 10:55:31

本文實(shí)例為大家分享了python讀取mysql數(shù)據(jù)繪制條形圖的具體代碼,供大家參考,具體內(nèi)容如下

Mysql 腳本示例:

create table demo( id int,product varchar(50),price decimal(18,2),quantity int,amount decimal(18,2),orderdate datetime);insert into demoselect 1,’AAA’,15.2,5,76,’2017-09-09’ union allselect 2,’BBB’,10,6,60,’2016-05-18’ union allselect 3,’CCC’,21,11,231,’2014-07-11’ union allselect 4,’DDD’,55,2,110,’2016-12-24’ union allselect 5,’EEE’,20,4,80,’2017-02-08’ union allselect 6,’FFF’,45,2,90,’2016-08-19’ union allselect 7,’GGG’,33,5,165,’2017-10-11’ union allselect 8,’HHH’,5,40,200,’2014-08-30’ union allselect 9,’III’,3,20,60,’2015-02-25’ union allselect 10,’JJJ’,10,15,150,’2015-11-02’;

python 繪圖分析:

# -*- coding: utf-8 -*-#import numpyimport MySQLdbimport plotly.plotlyimport plotly.graph_objs as pg host = 'localhost'port = 3306user = 'root'passwd = 'mysql'charset = 'utf8'dbname = 'test'conn = None try: conn = MySQLdb.Connection( host = host, port = port, user = user, passwd = passwd, db = dbname, charset = charset ) cur = conn.cursor(MySQLdb.cursors.DictCursor) cur.execute('select * from demo;') rows = cur.fetchall() #rows = numpy.array(rows) lists = [[],[],[],[]] for row in rows: lists[0].append(row['product']) lists[1].append(row['price']) lists[2].append(row['quantity']) lists[3].append(row['amount']) #print(lists) #print(lists[0]) #print(([x[0] for x in lists])) date_price = pg.Bar( x=lists[0], y=lists[1], name=’價(jià)格’) date_quantity = pg.Bar( x=lists[0], y=lists[2], name=’數(shù)量’) date_amount = pg.Bar( x=lists[0], y=lists[3], name=’總價(jià)’) data = [date_price, date_quantity, date_amount] #barmode = [stack,group,overlay,relative] layout = pg.Layout( barmode=’group’,title='各產(chǎn)品銷售情況' ) fig = pg.Figure(data=data, layout=layout) plotly.offline.plot(fig, filename = 'C:/Users/huangzecheng/Desktop/test.html') finally: if conn: conn.close()

將代碼保存為文件 bartest.py ,執(zhí)行腳本 python bartest.py ,生成 html 文件如下:

python讀取mysql數(shù)據(jù)繪制條形圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 千阳县| 兴业县| 长岭县| 桂东县| 三河市| 图片| 乌海市| 福海县| 美姑县| 普陀区| 化州市| 栾川县| 琼海市| 贞丰县| 合肥市| 万年县| 沙洋县| 敦煌市| 喀喇沁旗| 梁山县| 长子县| 泰宁县| 体育| 岚皋县| 松江区| 禹城市| 威远县| 夏河县| 灵川县| 阳春市| 额尔古纳市| 和顺县| 孟连| 昔阳县| 油尖旺区| 乌海市| 丹江口市| 鄂州市| 全南县| 阜新| 礼泉县|