0

Say that I have two lists:

yvalues = [30, 40, -20, 0, -10, 20, 45, 12, -5, ....]
Dates = ['20110103', '20110103', '20110103', '20110108', '20110108', '20110108', '20110113', '20110113', '20110113', ....]

The first entry in Dates does correspond to the first value in yvalues and so on. The dates repeat themselves because I observe multiple yvalues every 5 days.

Now if I want to plot the yvalues with Dates as x-axis, I do:

plt.plot(yvalues) 
plt.xticks(dates)

It gives me an error. If I try: plt.plot(Dates, yvalues), I get this nasty graph: enter image description here

How can I plot on the x-axis the correct date values (i.e. 20110103) and without the straight lines that separates the observation?

UPDATE

I don't want my values to be plotted on the same vertical line for each day but one after the other. In fact there is 5 minutes time difference between each observations. I decided to convert my Dates list using:

Dates = [datetime.date(int(d[0:4]), int(d[4:6]), int(d[6:8])) for d in Dates]

Then I do:

plt.plot(dates, yvalues) 

and get the following plot:

enter image description here

Clearly, this picture shows the values on the same date to be on the same vertical lines. I still have the annoying straight lines that separate each dates.

Now if I don't use any dates as for the x-axis, I get the following graph (which is the one that I want but I want the x-axis as dates): enter image description here

Sample dataset available here

12
  • So do you want to have lines between each point on each day, but not between each day, and it all to be the same colour? Why not split it into multiple data sets? Commented Nov 6, 2014 at 22:38
  • What is the actual data? There might be a better way to visualise it. Commented Nov 6, 2014 at 22:39
  • @will I updated the graph that I want in my question but you will see there are no dates on the x-axis otherwise I end-up with the previous (blue bar with space) graphs. I simply want to add dates on my x-axis graph. I don't need any distance between each point but as long as each point on each day does not appear on the same vertical. Commented Nov 6, 2014 at 23:32
  • The data structure is exactly like the two list in my question. Thanks for all the help @will Commented Nov 6, 2014 at 23:49
  • What actually is the data? This plot you have at the moment is just a mess, do you not think something like a mean with a maximum/minimum envolope, or maybe something like this. Commented Nov 7, 2014 at 0:08

2 Answers 2

2

Well after a bit of discussion, here's what i eventually landed on;

import datetime
import random
import numpy as np
import datetime
import itertools


dates, allSpillovers, allBins, allDigitised = [], [], [], []
with open("year.dat") as year:
  previousDate = None
  spillovers = []
  for line in year.readlines()[1:]:
    _, strdate, spillover = line.split(",")
    spillover = float(spillover)
    year, month, day = [int(i) for i in strdate.split("-")]

    date = datetime.date(year, month, day)

    if previousDate == date:
      spillovers.append(spillover)
    elif previousDate != None:
      mean = np.mean(spillovers)
      stdev = np.std(spillovers)

      spillovers.sort()
      if len(spillovers) > 70:
          allSpillovers.append([mean, mean-stdev, mean+stdev] + spillovers)
          dates.append(date)
      spillovers = []

    previousDate = date



#itertools.izip_longest(*allSpillovers, fillvalue=0)
allSpillovers = zip(*allSpillovers)


from matplotlib import pyplot

print len(dates), len(allSpillovers[0]), len(allSpillovers[1])

fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)


for i in range(3, len(allSpillovers)-1):
  alpha = 0.5 - abs(i / float(len(allSpillovers)) - 0.5)
  print len(dates), len(allSpillovers[i]), len(allSpillovers[i+1])
  ax.fill_between(dates, allSpillovers[i], allSpillovers[i+1], facecolor='green', interpolate=True, alpha=alpha, linewidth=0)

#ax.fill_between(dates, allSpillovers[1], allSpillovers[2], facecolor='green', interpolate=True, alpha=0.5)

#for b, d in bins, digitised:



ax.plot(dates, allSpillovers[0], color="blue", linewidth=2)
ax.plot(dates, [0 for _ in dates], color="red", linewidth=2)
ax.grid()

fig.autofmt_xdate()

pyplot.show()

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Hehe, you beat me to it! I was going with using strptime and included fig.autofmt_xdate() as well.
@will great suggestion. I should have been more clear in my question. I don't want the values for the same date to appear on the same vertical line but one after the other. In fact there is a 5 minute distance between each observations on each day. I will update my question.
1

Try this:

>>> from matplotlib import pyplot as plt
>>> Dates = ['20110103', '20110103', '20110103', '20110108', '20110108', '20110108', '20110113', '20110113', '20110113']
>>> yvalues = [30, 40, -20, 0, -10, 20, 45, 12, -5]
>>> x=range(len(Dates))
>>> plt.xticks(x,Dates)
>>> plt.plot(x,yvalues)
>>> plt.show()

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.