gpt4 book ai didi

python - Matplotlib 条形图 x 轴不会绘制字符串值

转载 作者:IT老高 更新时间:2023-10-28 21:52:58 26 4
gpt4 key购买 nike

我正在使用 Python 2.7 和 matplotlib。我正在尝试访问我的救护车调用数据库并计算每个工作日发生的调用数量。

然后,我将使用 matplotlib 为这些信息创建一个条形图,为医护人员提供他们每天忙碌程度的直观图形。

下面是运行良好的代码:

import pyodbc
import matplotlib.pyplot as plt
MySQLQuery = """
SELECT
DATEPART(WEEKDAY, IIU_tDispatch)AS [DayOfWeekOfCall]
, COUNT(DATEPART(WeekDay, IIU_tDispatch)) AS [DispatchesOnThisWeekday]
FROM AmbulanceIncidents
GROUP BY DATEPART(WEEKDAY, IIU_tDispatch)
ORDER BY DATEPART(WEEKDAY, IIU_tDispatch)
"""
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=MyServer;DATABASE=MyDatabase;UID=MyUserID;PWD=MyPassword')
cursor = cnxn.cursor()
GraphCursor = cnxn.cursor()
cursor.execute(MySQLQuery)

#generate a graph to display the data
data = GraphCursor.fetchall()
DayOfWeekOfCall, DispatchesOnThisWeekday = zip(*data)
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()

上面显示的代码运行良好。它返回一个漂亮的图表,我很高兴。我只想做一个改变。

X 轴不是显示星期几的名称,例如“星期日”,而是显示整数。换句话说,星期日是 1,星期一是 2,等等。

我的解决方法是重写我的 sql 查询以使用 DATENAME() 而不是 DATEPART()。下面显示的是我的 sql 代码,用于返回星期的名称(而不是整数)。

SELECT 
DATENAME(WEEKDAY, IIU_tDispatch)AS [DayOfWeekOfCall]
, COUNT(DATENAME(WeekDay, IIU_tDispatch)) AS [DispatchesOnThisWeekday]
FROM AmbulanceIncidents
GROUP BY DATENAME(WEEKDAY, IIU_tDispatch)
ORDER BY DATENAME(WEEKDAY, IIU_tDispatch)

我的 python 代码中的其他所有内容都保持不变。但是这不起作用,我无法理解错误消息。

以下是错误消息:

Traceback (most recent call last):
File "C:\Documents and Settings\kulpandm\workspace\FiscalYearEndReport\CallVolumeByDayOfWeek.py", line 59, in

<module>
plt.bar(DayOfWeekOfCall, DispatchesOnThisWeekday)
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2080, in bar
ret = ax.bar(left, height, width, bottom, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 4740, in bar
self.add_patch(r)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 1471, in add_patch
self._update_patch_limits(p)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 1489, in _update_patch_limits
xys = patch.get_patch_transform().transform(vertices)
File "C:\Python27\lib\site-packages\matplotlib\patches.py", line 547, in get_patch_transform
self._update_patch_transform()
File "C:\Python27\lib\site-packages\matplotlib\patches.py", line 543, in _update_patch_transform
bbox = transforms.Bbox.from_bounds(x, y, width, height)
File "C:\Python27\lib\site-packages\matplotlib\transforms.py", line 745, in from_bounds
return Bbox.from_extents(x0, y0, x0 + width, y0 + height)
TypeError: coercing to Unicode: need string or buffer, float found

总而言之,当我输出数据时,x 轴为整数,表示星期几,y 轴显示救护车事故的数量,Matplotlib 将生成一个漂亮的图表。但是当我的数据输出是 x 轴是一个字符串(星期日、星期一等)。那么 Matplotlib 将无法工作。

如何解决这个问题?

最佳答案

您的问题与 SQL 查询无关,它只是一种结束的手段。您真正要问的是如何更改 pylab 条形图上的文本标签。 bar chart 的文档对定制很有用,但只是为了change the labels这是一个最小的工作示例(MWE):

import pylab as plt

DayOfWeekOfCall = [1,2,3]
DispatchesOnThisWeekday = [77, 32, 42]

LABELS = ["Monday", "Tuesday", "Wednesday"]

plt.bar(DayOfWeekOfCall, DispatchesOnThisWeekday, align='center')
plt.xticks(DayOfWeekOfCall, LABELS)
plt.show()

enter image description here

关于python - Matplotlib 条形图 x 轴不会绘制字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9101497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com