pymysql访问mysql数据库并且plotly实现可视化

本文介绍使用Python连接MySQL数据库的方法,并通过pymysql模块实现数据查询。此外,还展示了如何利用Plotly库从数据库获取的数据进行图表绘制。

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

Anaconda资源管理器:http://python.jobbole.com/86236/
①python和mysql数据库的交互
MysqlDb和pyMysql,推荐使用后者

官方文档:http://pymysql.readthedocs.io/en/latest/user/examples.html
pymysql


import pymysql.cursors
conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='',db='',charset='utf8')
cursor=conn.cursor()
cursor.execute("select region from jdb group by region")
res=cursor.fetchall()
for row in res:
    for t in row:
        print (t)
conn.commit()
cursor.close()
conn.close()

和mysql交互 http://nbviewer.jupyter.org/gist/jackparmer/5485807511a58be48bf2
plotly绘图库:http://www.cnblogs.com/wangshuo1/p/6185932.html
本人使用plotly访问mysql数据库,绘制的图表

#!/usr/bin/env python
# encoding: utf-8
import pymysql.cursors
import  pandas   as pd
import plotly
import plotly.plotly as py
from plotly.graph_objs import *
plotly.tools.set_credentials_file(username='templarz', api_key='PtKMjV9gAzINZqmQRU4T')
conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='',db='native1',charset='utf8')
cursor=conn.cursor()
cursor.execute("select x,y,z from jdb1 group by x,y,z")
res=cursor.fetchall()
str(res)[0:300]
df=pd.DataFrame( [[j for j in i] for i in res] )
df.rename(columns={0:'x',1:'y',2:'z'},inplace=True);
print (df.head())
trace1=Scatter(
   x=df['x'],
   y=df['y']
)
trace2=Scatter(
   x=df['x'],
   y=df['z']
)
data=Data([trace1,trace2])
py.plot(data,filename='plotly绘图')

plotly在线预览地址:https://plot.ly/~templarz/213/
这里写图片描述
分享几个plotly画的比较酷的图
https://plot.ly/python/ribbon-plots/
如何解决访问mysql数据库的乱码问题?

import pandas as pd
pd.options.mode.chained_assignment = None
data=pd.read_csv('D:demo3.csv')
dat2=pd.DataFrame(data)
pro_names = dat2['province']
for i in range(len(pro_names)):
    try:
        pro_names[i] = str(pro_names[i]).decode('GBK')
    except:
        pro_names[i] = 'decode error'
print(dat2)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值