I have spent 30+ hours trying to figure this out, including searching all over the web. Now I am reaching out.
My environment: Python 3.6.0, pandas 0.20.1, matplotlib 2.0.2, pypyodbc 1.3.4
I am trying to plot data from SQL Server. Everything in the code works except the final plot does not show the x axis tick marks or ANY x axis values. Otherwise, the plot looks correct. The x axis is a date field in the format of YYYY-mm-dd. I have tried dozens of variations to make it work. Do you see what is wrong with this code? Don't mind the complicated query, I really do not think that is related to the problem.
import pypyodbc
import pandas as pd
import pandas.io.sql
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.ticker import FormatStrFormatter
months = mdates.MonthLocator() # every month
days = mdates.DayLocator() # every day
daysFmt = FormatStrFormatter('%d')
#Connect to SQL Server databause using Trusted Connection
sql = """SELECT UID, startdate, lastdelivered FROM TABLENAME """
iddf = pandas.io.sql.read_sql(sql, conn)
for UID in range(len(iddf)):
sql = """SELECT impdate, sum(imp) as imps FROM TABLENAME where UID = """
+ str(iddf['UID'].iloc[UID]) + """ and impdate >= '2017-05-01' and impdate
<= '2017-05-15' group by impdate order by impdate"""
df = pandas.io.sql.read_sql(sql, conn)
df.impressions.fillna(value=0, inplace=True)
pd.to_datetime(df['impdate'], format= '%Y-%m-%d')
print (df['impdate']) #Shows the format to be YYYY-mm-dd
ax = (df[['impdate','imps']].plot(kind='line', title="UID: ", figsize=
(10, 10), legend=False, fontsize=10))
ax.set_xlabel("ImpDate", fontsize=12)
ax.set_ylabel("Imps", fontsize=12)
ax.xaxis.set_major_locator(days)
ax.xaxis.set_major_formatter(daysFmt)
plt.setp(ax.get_xticklabels(),visible = True)
plt.show(ax)
plt.close('all')