快活林资源网 Design By www.csstdc.com

本文实例为大家分享了python读取mysql数据绘制条形图的具体代码,供大家参考,具体内容如下

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='价格')
  date_quantity = pg.Bar( x=lists[0], y=lists[2], name='数量')
  date_amount = pg.Bar( x=lists[0], y=lists[3], name='总价')
  data = [date_price, date_quantity, date_amount]
  #barmode = [stack,group,overlay,relative]
  layout = pg.Layout( barmode='group',title="各产品销售情况" )
  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 ,执行脚本 python bartest.py ,生成 html 文件如下:

python读取mysql数据绘制条形图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

快活林资源网 Design By www.csstdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
快活林资源网 Design By www.csstdc.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。