Skip to main content
 首页 » 编程设计

python matplotlib 散点图

2025年05月04日7jpfss

我正在使用 matplotlib 对 Oracle 表中的一些数据进行散点图绘制。其中一个字段是 DATE,在下面的查询中将其转换为 to_char 时出现错误。我想按 00-23 小时而不是日期来散点图。所以Y轴应该显示00-23。请参阅下面的代码示例以及错误。

感谢您的帮助。


import cx_Oracle, os 
import numpy 
import matplotlib.pyplot as plt 

这行得通

sql = """select number_of_small_reads, number_of_small_writes, number_of_large_reads, number_of_large_writes, end_time from schema.table order by end_time""" 

这行不通

sql = """select number_of_small_reads, number_of_small_writes, number_of_large_reads, number_of_large_writes, to_char(end_time, 'HH24') e from schema.table order by e""" 

我收到以下错误: “无法使用灵活类型执行 reduce”

conn = cx_Oracle.Connection("user/password@server:1521/database") 
cursor = conn.cursor() 
cursor.execute(sql) 
rowset = cursor.fetchall() 
cursor.close() 
cx1 = [] 
cx2 = [] 
cx3 = [] 
cx4 = [] 
cy = [] 
cx1, cx2, cx3, cx4, cy = zip(*rowset) 
 
ax = plt.figure().add_subplot(111) 
ax.scatter(cx1, cy, s=9, c='b', marker='o', label='number_of_small_reads') 
ax.scatter(cx2, cy, s=9, c='r', marker='o', label='number_of_small_writes') 
ax.scatter(cx3, cy, s=9, c='g', marker='o', label='number_of_large_reads') 
ax.scatter(cx4, cy, s=9, c='y', marker='o', label='number_of_large_writes') 
plt.legend(shadow=True) 
plt.title('Python Generated Chart') 
plt.xlabel('IOPS') 
plt.ylabel('By Date') 
plt.xlim(0) 
plt.show() 
conn.close() 

请您参考如下方法:

您可以只选择小时而不是日期。

SELECT .. DATEPART(mm, end_date) AS end_hour 

http://www.bennadel.com/blog/172-Ask-Ben-Selecting-Parts-of-a-Date-Time-Stamp-In-SQL.htm