Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. #!/usr/bin/python
  2. import pgdb
  3. import matplotlib
  4. import matplotlib.pyplot as plt
  5.  
  6. params = {'host':'localhost', 'user': 'postgres', 'database':'postgres', 'password':'admin'}
  7.  
  8. connection1 = pgdb.Connection(**params)
  9. connection1.autocommit=False
  10. cursor1 = connection1.cursor()
  11.  
  12.  
  13. def query():
  14.     # Here we test some concurrency issues.
  15.     xy = "select FLOOR(year/10)*10 AS decade, AVG(population), STDDEV(population) from popdata GROUP BY decade ORDER BY decade;"
  16.     print("U1: (start) "+ xy)
  17.     cursor1.execute(xy)
  18.     data = cursor1.fetchall()
  19.     connection1.commit()
  20.  
  21.     x = []
  22.     y = []
  23.     y_err = []
  24.  
  25.     for r in data:
  26.         # you access ith component of row r with r[i], indexing starts with 0
  27.         # check for null values represented as "None" in python before conversion and drop
  28.         # row whenever NULL occurs
  29.         print("Considering tuple", r)
  30.  
  31.         if r[0] is not None and r[1] is not None and r[2] is not None:
  32.             x.append(int(r[0]))
  33.             y.append(int(r[1]))
  34.             y_err.append(int(r[2]))
  35.         elif r[2] is None:
  36.             x.append(int(r[0]))
  37.             y.append(int(r[1]))
  38.             y_err.append(0)
  39.         else:
  40.             print("Dropped tuple ", r)
  41.     print("x:", x)
  42.     print("y:", y)
  43.     return[x, y, y_err]
  44.  
  45.  
  46. def close():
  47.     connection1.close()
  48.  
  49.  
  50. # when calling python filename.py the following functions will be executed:
  51. [x, y, y_err] = query()
  52. # plt.scatter(x, y)
  53. plt.figure()
  54. plt.errorbar(x, y, yerr=y_err)
  55. plt.show()  # display figure if you run this code locally
  56. plt.savefig("figure.png") # save figure as image in local directory
  57. close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement