davidanddiesel
Posts: 15
Joined: Sun Jan 18, 2015 4:03 am

Need help graphing things...

Mon Feb 16, 2015 12:27 am

I am using a Raspberry Pi and a BMP085 to display barometric pressure and temperature. However, now I want my Pi to graph these things. EASILY! I've looked into many Python graphing methods, and they don't seem like the kind of thing I need. Do I really need to know calculus to graph some numbers? I also don't want to use online alternatives like plot.ly, I want to view the graph straight from my Python script.

Here is the code I already have:

Code: Select all

import time
import colorama
from colorama import *
import Adafruit_BMP.BMP085 as BMP085
sensor = BMP085.BMP085()
colorama.init()
starttemp= None
startpress = None
for i in range(0,6):
        print('\n')

time.sleep(3)
print(Fore.RED + Style.BRIGHT +  Back.YELLOW + "Welcome to the raspberry pi weather station!" + Style.RESET_ALL)
time.sleep(5)
try:
        while True:
                temp = sensor.read_temperature()
                pressure = sensor.read_pressure()
                car  = "%.2f millibars" % (pressure / 100.0)
                bus = "%.2f *F" % ((temp * 1.8) + 32)

                if starttemp is None:
                        starttemp=bus
                if startpress is None:
                        startpress=car

                tp = "Temperature: %.2f *F" % ((temp * 1.8) + 32)
                pp = "Pressure: %.2f millibars" % (pressure / 100.0)
                p1 = str(pp)
                t1 = str(tp)
                print(Fore.MAGENTA + p1)
                print(Fore.BLUE + t1 + '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n' + Style.RESET_ALL)
                time.sleep(1)

except KeyboardInterrupt:
        endtemp = t1
        endpress = p1
        print("\n\n\n")
        print(Fore.RED + "The starting temperature was: " + starttemp)
        print(Fore.BLUE + "The starting presure was: " + startpress)
        print(Fore.GREEN + "\n\n" + "The ending " + endtemp)
        print(Fore.MAGENTA +  "The ending " + endpress)
        print("\n")

I would like this converted to a program that graphs the temperatures and pressures!
Any help would be appreciated.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Need help graphing things...

Mon Feb 16, 2015 4:36 am

Hi there,

Like all things in Unix there are a hundred and one tools to help you do anything you want. All you have to do is provide the glue to link them together.

What you should pretty much never do is write the tools yourself. So, don't write a graphing library.

Here are a bunch of ones for Python:
https://wiki.python.org/moin/NumericAnd ... c/Plotting

Choose one and have a play.

The popular workhorse is GNUplot: http://gnuplot-py.sourceforge.net/

Maybe that's what you want, and maybe the demo will give you enough to start with.

How are you storing your values over time? I don't see it in your program. At some point you need to record a timestamp (date and time), and the two sensor values (temperature and pressure). You could store it in memory in a Python list, or you could write it to a CSV file (or something similar). The Right Answer is to store it in a database. Make that your goal, but you don't have to get there instantly.

Return to “Python”