需求:
查詢2022年各月訂單量,并用柱狀圖展示
SQL:
select
date_format(create_time,'%Y-%m') as mon
,count(distinct order_id) as ord_cnt
from prod.order_info
where date_format(create_time,'%Y-%m') between '2022-01' and '2022-12'
group by date_format(create_time,'%Y-%m')
order by date_format(create_time,'%Y-%m')
Python代碼:
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import pandas as pd
import sqlalchemy
#import DataFrame
from matplotlib.font_manager import FontProperties
con=sqlalchemy.create_engine(數(shù)據(jù)庫連接信息)
#創(chuàng)建數(shù)據(jù)庫的連接引擎
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14)
data=pd.read_sql_query(sql="""select
date_format(create_time,'%%Y-%%m') as mon
,count(distinct order_id) as ord_cnt
from prod.order_info
where date_format(create_time,'%%Y-%%m') between '2022-01' and '2022-12'
group by date_format(create_time,'%%Y-%%m')
order by date_format(create_time,'%%Y-%%m')"""
,con=con,index_col=['mon'])
#雙%處理
print(data['ord_cnt'].values)
tuli=['訂單量']
df=pd.DataFrame(data['ord_cnt'].values,
columns=tuli,
index=data.index)
df.plot(grid=True,xlim=[0,12],ylim=[0,5000],kind='bar')
plt.title(u"2022年每月訂單量",fontproperties=font)
plt.savefig('fig5.jpg')
結(jié)果展示:
文章來源:http://www.zghlxwxcb.cn/news/detail-544407.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-544407.html
到了這里,關(guān)于【Python】查詢SQL并用柱狀圖展示的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!