用SQL取代pandas 聚合运算结合plot画图的方法案例

#!/usr/bin/python
# -*- coding: <encoding name> -*-
import matplotlib.pyplot as plt
import pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)
import pymssql  # 引入pymssql模块
import seaborn as sns  # Provides a high level interface for drawing attractive and informative statistical graphics
from matplotlib.font_manager import FontProperties
from pylab import *
import matplotlib.pyplot as plt
import seaborn as sns

plt.rcParams['font.sans-serif'] = ['SimHei']
# Matplotlib中设置字体-黑体,解决Matplotlib中文乱码问题
plt.rcParams['axes.unicode_minus'] = False
# 解决Matplotlib坐标轴负号'-'显示为方块的问题
sns.set(font='SimHei')
# Seaborn中设置字体-黑体,解决Seaborn中文乱码问题
import warnings                                            # Ignore warning related to pandas_profiling
warnings.filterwarnings('ignore')
conn = pymssql.connect(host='localhost', user='wonderful', password='wonderful', database='ProcreateV93',charset='GBK')

font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=10)
sql = "select username,COUNT(DISTINCT o_id) AS Oidcount from Oplan GROUP BY username order by Oidcount desc "
df0 = pd.read_sql(sql, conn)
df = pd.DataFrame(df0)


#orient='h'表示是水平展示的,alpha表示颜色的深浅程度

#设置y轴、X轴的坐标名字与字体大小
sns.barplot(x=df.username.values,y=df.Oidcount.values,orient='v', color='green')

plt.xlabel('医生')
plt.ylabel('患者数')

#设置X轴的各列下标字体是水平的
plt.xticks(rotation='horizontal')

#设置Y轴下标的字体大小

plt.show()

  

posted @ 2021-10-09 17:05  残阳飞雪  阅读(98)  评论(0编辑  收藏  举报