python Sqlserver数据库画双轴图

import matplotlib.pyplot as plt
import numpy as np
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']#用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False#用来正常显示负号

if __name__ == '__main__':
 conn = pymssql.connect(host='localhost', user='wonderful', password='wonderful', database='ProcreateV93',
                        charset='GBK')
sql="select left(convert(char, op.MadeDate, 12), 4) as months, "\
"COUNT(DISTINCT OP.O_iD)                 AS patientcount, "\
"sum(op.OPSTimes)                           optime, "\
"(COUNT(DISTINCT OP.O_iD)+0.0)/(sum(op.OPSTimes)+0.0) as pct "\
"from Oplan op "\
"where op.GroupName is not null "\
"and UserName is not null "\
"group by left(convert(char, op.MadeDate, 12), 4)"

df0 = pd.read_sql(sql, conn)
df = pd.DataFrame(df0)
print(df)
fig, ax1 = plt.subplots()
plt.xticks(rotation=45)
ax1.bar(df.months.values,df.patientcount.values, color="black", alpha=0.5, label="患者数")
ax1.set_xlabel("患者人数")
ax1.set_ylabel("患者数据")
ax2 = ax1.twinx()
ax2.plot(df.months.values, df.pct.values, color="green", label="周期数")
ax2.set_ylabel("周期数")
fig.legend(loc="upper right", bbox_to_anchor=(1, 1), bbox_transform=ax1.transAxes)
plt.show();

 

posted @ 2021-10-13 13:43  残阳飞雪  阅读(60)  评论(0编辑  收藏  举报