Reputation: 3928
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:
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:
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):
Sample dataset available here
Upvotes: 0
Views: 3709
Reputation: 10650
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()
Upvotes: 2
Reputation: 8709
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()
Upvotes: 1