Matplotlib bar graph x axis won't plot string values
Your question has nothing to do with an SQL query, it is simply a means to end. What you are really asking is how to change the text labels on a bar chart in pylab. The docs for the bar chart are useful for customizing, but to simply change the labels here is a minimal working example (MWE):
import pylab as pltDayOfWeekOfCall = [1,2,3]DispatchesOnThisWeekday = [77, 32, 42]LABELS = ["Monday", "Tuesday", "Wednesday"]plt.bar(DayOfWeekOfCall, DispatchesOnThisWeekday, align='center')plt.xticks(DayOfWeekOfCall, LABELS)plt.show()
Don't change your SQL code just to alter the illustration. Instead, make a small addition to your Python code.
I believe you can do something like this answer. Set the tick labels to be the days of the week.
It may be as simple as adding the following line:
plt.xticks((1, 2, ..., 7), ('Sunday', 'Monday', ..., 'Saturday'))
EDIT: Example in response to comment using a fictional table IncidentTypes
that maps integer keys to names of incident types.
cursor.execute('select incident_type_id, count(*), incident_type from Incidents join IncidentTypes using (incident_type_id) group by incident_type_id')results = cursor.fetchall()tickpositions = [int(r[0]) for r in results]numincidents = [int(r[1]) for r in results]ticklabels = [r[2] for r in results]plt.bar(tickpositions, numincidents)plt.xticks(tickpositions, ticklabels)
Final completed answer that resolved the issue:Thank you very much Steve. You have been a great help. I studied geography in college, not programming, so this is quite difficult for me. Here is the final code that works for me.
import pyodbc import matplotlib.pyplot as plt MySQLQuery = """ SELECT DATEPART(WEEKDAY, IIU_tDispatch)AS [IntegerOfDayOfWeek] , COUNT(DATENAME(WeekDay, IIU_tDispatch)) AS [DispatchesOnThisWeekday] , DATENAME(WEEKDAY, IIU_tDispatch)AS [DayOfWeekOfCall] FROM IIncidentUnitSummary INNER JOIN PUnit ON IIU_kUnit = PUN_Unit_PK WHERE PUN_UnitAgency = 'LC' AND IIU_tDispatch BETWEEN 'October 1, 2010' AND 'October 1, 2011' AND PUN_UnitID LIKE 'M__' GROUP BY DATEPART(WEEKDAY, IIU_tDispatch), DATENAME(WEEKDAY, IIU_tDispatch) ORDER BY DATEPART(WEEKDAY, IIU_tDispatch) """ cnxn = pyodbc.connect("a bunch of stuff I don't want to share") cursor = cnxn.cursor() GraphCursor = cnxn.cursor() cursor.execute(MySQLQuery) results = cursor.fetchall() IntegerDayOfWeek, DispatchesOnThisWeekday, DayOfWeekOfCall = zip(*results) tickpositions = [int(r[0]) for r in results] numincidents = [int(r[1]) for r in results] ticklabels = [r[2] for r in results] plt.bar(tickpositions, numincidents) plt.xticks(tickpositions, ticklabels) #plt.bar(DayOfWeekOfCall, DispatchesOnThisWeekday) plt.grid() plt.title('Dispatches by Day of Week') plt.xlabel('Day of Week') plt.ylabel('Number of Dispatches') plt.show() cursor.close() cnxn.close()
I don't really understand the lines between "results=cursor.fetchall()" and the following four lines of code that involve creating arrays. I am glad you do, because I look at it and it still does not sink in.thank you very much. This helps out a lot.David