IT评测·应用市场-qidao123.com技术社区

标题: Python中使用PostgreSQL和Apache AGE扩展来绘制和显示图表 [打印本页]

作者: 何小豆儿在此    时间: 2025-1-10 03:35
标题: Python中使用PostgreSQL和Apache AGE扩展来绘制和显示图表
要在Python中使用PostgreSQL和Apache AGE扩展来绘制和显示图表,你需要完成以下步调:
以下是一个简单的示例,假设你已经有一个包含AGE数据的PostgreSQL数据库:
安装依赖项

首先,确保你已经安装了必要的Python库:
  1. pip install psycopg2-binary matplotlib
复制代码
大概,假如你更喜好使用plotly:
  1. pip install psycopg2-binary plotly
复制代码
设置和毗连数据库

假设你的数据库名为mydatabase,用户名为myuser,暗码为mypassword,主机为localhost,端口为5432。
  1. import psycopg2
  2. import matplotlib.pyplot as plt
  3. # 或者使用 plotly: import plotly.express as px
  4. # 数据库连接参数
  5. conn_params = {
  6.     'dbname': 'mydatabase',
  7.     'user': 'myuser',
  8.     'password': 'mypassword',
  9.     'host': 'localhost',
  10.     'port': '5432'
  11. }
  12. # 连接到数据库
  13. conn = psycopg2.connect(**conn_params)
  14. cur = conn.cursor()
复制代码
查询AGE数据

假设你有一个表age_data,包含两列timestamp和value。
  1. # 查询数据
  2. query = "SELECT timestamp, value FROM age_data ORDER BY timestamp;"
  3. cur.execute(query)
  4. rows = cur.fetchall()
  5. # 获取列名
  6. colnames = [desc[0] for desc in cur.description]
  7. # 关闭游标和连接
  8. cur.close()
  9. conn.close()
复制代码
绘制图表

使用matplotlib绘制图表:
  1. # 提取数据
  2. timestamps = [row[0] for row in rows]
  3. values = [row[1] for row in rows]
  4. # 绘制图表
  5. plt.figure(figsize=(10, 5))
  6. plt.plot(timestamps, values, marker='o')
  7. plt.title('AGE Data')
  8. plt.xlabel('Timestamp')
  9. plt.ylabel('Value')
  10. plt.grid(True)
  11. plt.xticks(rotation=45)
  12. plt.tight_layout()
  13. # 显示图表
  14. plt.show()
复制代码
大概使用plotly绘制图表:
  1. # 提取数据
  2. import pandas as pd
  3. df = pd.DataFrame(rows, columns=colnames)
  4. df['timestamp'] = pd.to_datetime(df['timestamp'])
  5. # 绘制图表
  6. fig = px.line(df, x='timestamp', y='value', title='AGE Data')
  7. fig.show()
复制代码
完备代码示例

以下是完备的代码示例,使用matplotlib:
  1. import psycopg2import matplotlib.pyplot as pltimport pandas as pd# 数据库毗连参数conn_params = {    'dbname': 'mydatabase',    'user': 'myuser',    'password': 'mypassword',    'host': 'localhost',    'port': '5432'}# 毗连到数据库conn = psycopg2.connect(**conn_params)cur = conn.cursor()# 查询数据
  2. query = "SELECT timestamp, value FROM age_data ORDER BY timestamp;"
  3. cur.execute(query)
  4. rows = cur.fetchall()
  5. # 获取列名
  6. colnames = [desc[0] for desc in cur.description]
  7. # 关闭游标和连接
  8. cur.close()
  9. conn.close()
  10. # 提取数据
  11. timestamps = [row[0] for row in rows]
  12. values = [row[1] for row in rows]
  13. # 绘制图表
  14. plt.figure(figsize=(10, 5))
  15. plt.plot(timestamps, values, marker='o')
  16. plt.title('AGE Data')
  17. plt.xlabel('Timestamp')
  18. plt.ylabel('Value')
  19. plt.grid(True)
  20. plt.xticks(rotation=45)
  21. plt.tight_layout()
  22. # 显示图表
  23. plt.show()
复制代码
大概使用plotly:
  1. import psycopg2import plotly.express as pximport pandas as pd# 数据库毗连参数conn_params = {    'dbname': 'mydatabase',    'user': 'myuser',    'password': 'mypassword',    'host': 'localhost',    'port': '5432'}# 毗连到数据库conn = psycopg2.connect(**conn_params)cur = conn.cursor()# 查询数据
  2. query = "SELECT timestamp, value FROM age_data ORDER BY timestamp;"
  3. cur.execute(query)
  4. rows = cur.fetchall()
  5. # 获取列名
  6. colnames = [desc[0] for desc in cur.description]
  7. # 关闭游标和连接
  8. cur.close()
  9. conn.close()
  10. # 提取数据df = pd.DataFrame(rows, columns=colnames)df['timestamp'] = pd.to_datetime(df['timestamp'])# 绘制图表fig = px.line(df, x='timestamp', y='value', title='AGE Data')fig.show()
复制代码
请根据你的具体需求调解查询和绘图代码。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4