python讀取mysql數(shù)據(jù)繪制條形圖
本文實(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 demo select 1,'AAA',15.2,5,76,'2017-09-09' union all select 2,'BBB',10,6,60,'2016-05-18' union all select 3,'CCC',21,11,231,'2014-07-11' union all select 4,'DDD',55,2,110,'2016-12-24' union all select 5,'EEE',20,4,80,'2017-02-08' union all select 6,'FFF',45,2,90,'2016-08-19' union all select 7,'GGG',33,5,165,'2017-10-11' union all select 8,'HHH',5,40,200,'2014-08-30' union all select 9,'III',3,20,60,'2015-02-25' union all select 10,'JJJ',10,15,150,'2015-11-02';
python 繪圖分析:
# -*- coding: utf-8 -*- #import numpy import MySQLdb import plotly.plotly import plotly.graph_objs as pg host = "localhost" port = 3306 user = "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 文件如下:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python如何讀取MySQL數(shù)據(jù)庫表數(shù)據(jù)
- python3 pandas 讀取MySQL數(shù)據(jù)和插入的實(shí)例
- Python3讀取Excel數(shù)據(jù)存入MySQL的方法
- 使用python的pandas庫讀取csv文件保存至mysql數(shù)據(jù)庫
- python讀取word文檔,插入mysql數(shù)據(jù)庫的示例代碼
- python連接mysql數(shù)據(jù)庫并讀取數(shù)據(jù)的實(shí)現(xiàn)
- Python 讀取千萬級(jí)數(shù)據(jù)自動(dòng)寫入 MySQL 數(shù)據(jù)庫
相關(guān)文章
Python實(shí)現(xiàn)生成密碼字典的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)生成密碼字典的方法,結(jié)合實(shí)例形式詳細(xì)分析了Python密碼字典的實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),涉及字符串運(yùn)算、文件讀寫等相關(guān)操作技巧,需要的朋友可以參考下2019-09-09python實(shí)現(xiàn)無證書加密解密實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)無證書加密解密的方法,實(shí)例講述了無證書加密解密的原理與具體實(shí)現(xiàn)過程,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10Python使用dict.fromkeys()快速生成一個(gè)字典示例
這篇文章主要介紹了Python使用dict.fromkeys()快速生成一個(gè)字典,結(jié)合實(shí)例形式分析了Python基于dict.fromkeys()生成字典的相關(guān)操作技巧,需要的朋友可以參考下2019-04-04Python實(shí)現(xiàn)進(jìn)度條和時(shí)間預(yù)估的示例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)進(jìn)度條和時(shí)間預(yù)估的代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06