0

How can I plot two lines in the same plot? The code below is my attempt to do that but it didn't work. 'Temp' is the new variable I added

     (2nd EDITED )

    from datetime import datetime
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    import numpy as np
    from scipy.stats import mode
    import re
    linematchregex = re.compile('(\d+/\d+/\d+ \d+:\d+),(\d+\.\d+),(\d+\.\d+)')

    startTime = datetime.strptime(raw_input('please enter start time in format like 21/7/2014 0:00 :'), '%d/%m/%Y %H:%M')
    endTime   = datetime.strptime(raw_input('please enter end time in format like 22/7/2014 23:57 :') , '%d/%m/%Y %H:%M')

    with open(r'data.txt', 'r') as strm:
        strm.next() #skip first line
        t, y, temp = zip(*[p for line in strm for p in linematchregex.findall(line)])
    t = [datetime.strptime(x, '%d/%m/%Y %H:%M') for x in t ]
    temp = [float(x) for i,x in enumerate(temp) if startTime<=t[i]<=endTime]
    y = [float(x) for i,x in enumerate(y) if startTime<=t[i]<=endTime]

    t = [x for x in t if startTime<=x<=endTime]
    fig = plt.figure()
    ax1 = fig.add_subplot(1, 1, 1, axisbg='white')
    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
    ax1.plot(t, y, 'c', linewidth=3.3)
    ax1.plot(t, temp)
    plt.show()

My Program can plot TimeStamp with Irradiance but how can i plot all the columns below Time with Irradiance & Time with Temperature

(EDITED)

TimeStamp,Irradiance,Ambient_Temperature
21/7/2014 12:00:06 AM,0.66,29.16
21/7/2014 12:00:20 AM,0.71,29.16
21/7/2014 12:00:34 AM,0.65,29.17
21/7/2014 12:00:48 AM,0.67,29.17
21/7/2014 12:01:08 AM,0.58,29.17
21/7/2014 12:01:23 AM,0.54,29.18
21/7/2014 12:01:37 AM,0.63,29.17
21/7/2014 12:01:52 AM,0.65,29.16
21/7/2014 12:02:07 AM,0.64,29.16
21/7/2014 12:02:22 AM,0.63,29.18
21/7/2014 12:02:36 AM,0.63,29.16
21/7/2014 12:02:50 AM,0.64,29.17
21/7/2014 12:03:04 AM,0.62,29.19
21/7/2014 12:03:24 AM,0.64,29.21
  • I added a side by side plot answer. Were you looking to have the datasets plotted on the same graph? – Gabriel Jul 31 '14 at 02:22
  • ya to plot both irradiance,temperature with time on the graph - so like two graphs on the window –  Jul 31 '14 at 02:36
  • hi Gabriel, thanks for the help, i say wrong - i meant like one graph but with 2 lines –  Jul 31 '14 at 02:41
  • in that case, just update your definition of `temp` as in my answer and don't do the top part. if this helps with your problem, please accept the answer by clicking the green arrow next to it. – Gabriel Jul 31 '14 at 02:42
  • when i add the following code, it shows two separate graph, i want to like show one graph but with like 2 lines in it –  Jul 31 '14 at 02:42
  • ok, Gabriel, what i want is like Irradiance one line then Temperature another line something like this http://stackoverflow.com/questions/4805048/how-to-get-different-lines-for-different-plots-in-a-single-figure –  Jul 31 '14 at 02:45
  • 1
    got it. don't use the top part of my answer. only change the one line `temp = [float(x) for i,x in enumerate(temp) if startTime<=t[i]<=endTime]` from your original code – Gabriel Jul 31 '14 at 02:46
  • so by changing that it will work? –  Jul 31 '14 at 02:57
  • the problem wasn't in your plotting it was probably a copy/paste mistake. you had `enumerate(y)` when defining `temp`. you were getting two line originally, they were just the same line – Gabriel Jul 31 '14 at 02:59
  • but when i run, it only show one line –  Jul 31 '14 at 03:20
  • what do you get when you print `temp` and print `y` – Gabriel Jul 31 '14 at 03:27
  • how do i print the temp and y –  Jul 31 '14 at 03:50
  • 1
    you left out `ax1.plot(t, temp)` – Gabriel Jul 31 '14 at 04:05

1 Answers1

1

If you want to create another subplot in your plot. Try this,

fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1, axisbg='white')
ax2 = fig.add_subplot(1, 2, 2, axisbg='white')
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))
ax2.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))
ax1.plot(t, y, 'c', linewidth=3.3)
ax2.plot(t, temp)

The method add_subplot adds a subplot. The first two arguments give the number of rows and columns, the third indicates which subplot in the 1x2 matrix you want to create. The above code will make one subplot on the left and one on the right. If you want one on top of the other you can reverse the first two arguments in both calls to add_subplot.

Another concern is (as best I can tell) there is a bug when you create temp,

temp = [float(x) for i,x in enumerate(y) if startTime<=t[i]<=endTime]

should be

temp = [float(x) for i,x in enumerate(temp) if startTime<=t[i]<=endTime]

If you want the two plots on the same set of axes your original code will work with just the above change. If you want the lines to be labelled in a legend you can do that too by changing your plot commands to,

ax1.plot(t, y, 'c', linewidth=3.3, label="Irradiance")
ax1.plot(t, temp, label="Temperature")
plt.legend()
Gabriel
  • 10,524
  • 1
  • 23
  • 28