2016-06-21 84 views
1

我无法为我的matplotlib图设置x/ylabel,它使用的是来自sql查询的数据。Matplotlib设置xlabels

我的代码:

import matplotlib.pyplot as plt 
import pyodbc 
import pandas 

conn = pyodbc.connect(r'DRIVER={SQL Server Native Client 11.0};SERVER=servername;DATABASE=dbname;Trusted_Connection=Yes;') 

sql = pandas.read_sql(('SELECT COUNT(Person) AS Cnt, BirthMonth FROM Person GROUP BY BirthMonth'),conn) 

df = pandas.DataFrame(sql, columns = ['Cnt', 'BirthMonth']) 


ax = df.plot(kind='bar', title = "BirthMonthCounts",figsize=(15,10),legend=False, fontsize=12, x='BirthMonth', y='Cnt', xticks='BirthMonth') 
ax.set_xlabel("X",fontsize=12) 
ax.set_ylabel("Y",fontsize=12) 
plt.show() 

当我运行我的代码图表显示与正确的价值观,但没有x或ylabels。

我试过了几件事,但我怀疑我刚刚写了我的剧情代码。

回答

0

传递参数ax到,

fig, ax = plt.subplots() 

df.plot(kind='bar', ax=ax, title="BirthMonthCounts", figsize=(15,10), legend=False, fontsize=12, x='BirthMonth', y='Cnt', xticks='BirthMonth')