[英]Python: Plot a graph with the same x-axis values
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. Dates
中的第一个条目确实对应于yvalues
的第一个值,依此类推。 The dates repeat themselves because I observe multiple yvalues
every 5 days. 这些日期重复出现,因为我每5天观察到多个yvalues
。
Now if I want to plot the yvalues
with Dates
as x-axis, I do: 现在,如果要以“ Dates
为x轴绘制yvalues
,则可以执行以下操作:
plt.plot(yvalues)
plt.xticks(dates)
It gives me an error. 它给了我一个错误。 If I try: plt.plot(Dates, yvalues)
, I get this nasty graph: 如果我尝试: plt.plot(Dates, yvalues)
,我得到这个讨厌的图:
How can I plot on the x-axis the correct date values (ie 20110103) and without the straight lines that separates the observation? 如何在x轴上绘制正确的日期值(即20110103),并且没有将观察值分开的直线?
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. 实际上,每个观察之间有5分钟的时间差。 I decided to convert my Dates
list using: 我决定使用以下方法转换“ Dates
列表:
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): 现在,如果我不使用任何日期作为x轴,则会得到以下图形(这是我想要的图形,但我希望x轴作为日期):
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()
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.