Django 图表 - 日期和时间轴
我有一个模型,如下所示:
class Measurement(models.Model):
date = models.DateField('date')
time = models.TimeField('time')
Q = models.DecimalField(max_digits=10, decimal_places=6)
P = models.DecimalField(max_digits=10, decimal_places=6)
f = models.DecimalField(max_digits=10, decimal_places=6)
在我看来,我想代表它。所以我做了这个功能:
def plotMeas(request):
# Count the events
c = Measurement.objects.all()
c = c.count()
# Variables
i = 0
a = [0]
P = a*c
Q = a*c
t = a*c
# Save dP_L1 & dQ_L1 in lists
for i in range(c):
meas = Measurement.objects.get(pk = i+1)
P [i] = meas.P
Q [i] = meas.Q
t [c-1-i] = i*10
if c > 100:
P = P[-100:]
Q = Q[-100:]
t [i] = t[-100:]
# Construct the graph
fig = Figure()
q = fig.add_subplot(211)
q.set_xlabel("time (minutes ago)")
q.set_ylabel("Q (VAR)")
p = fig.add_subplot(212)
p.set_xlabel("time (minutes ago)")
p.set_ylabel("P (W)")
p.plot(t,P, 'go-')
q.plot(t,Q, 'o-')
canvas = FigureCanvas(fig)
response = HttpResponse(content_type='image/png')
canvas.print_png(response)
return response
但是,我希望水平轴显示日期和时间(保存在模型中)。有谁知道该怎么做?
I have one model which looks like this:
class Measurement(models.Model):
date = models.DateField('date')
time = models.TimeField('time')
Q = models.DecimalField(max_digits=10, decimal_places=6)
P = models.DecimalField(max_digits=10, decimal_places=6)
f = models.DecimalField(max_digits=10, decimal_places=6)
In my views, I would like to represent it. So I made this function:
def plotMeas(request):
# Count the events
c = Measurement.objects.all()
c = c.count()
# Variables
i = 0
a = [0]
P = a*c
Q = a*c
t = a*c
# Save dP_L1 & dQ_L1 in lists
for i in range(c):
meas = Measurement.objects.get(pk = i+1)
P [i] = meas.P
Q [i] = meas.Q
t [c-1-i] = i*10
if c > 100:
P = P[-100:]
Q = Q[-100:]
t [i] = t[-100:]
# Construct the graph
fig = Figure()
q = fig.add_subplot(211)
q.set_xlabel("time (minutes ago)")
q.set_ylabel("Q (VAR)")
p = fig.add_subplot(212)
p.set_xlabel("time (minutes ago)")
p.set_ylabel("P (W)")
p.plot(t,P, 'go-')
q.plot(t,Q, 'o-')
canvas = FigureCanvas(fig)
response = HttpResponse(content_type='image/png')
canvas.print_png(response)
return response
However, I would like that the horizontal axis would show the date and the time (saved in the model). Does anyone know how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看
plot_date 的文档
。方便的是,plot_date
采用与plot
类似的参数。调用可能如下所示:使用
matplotlib.dates
然后您可以自定义 x 轴标签的格式。
一个简单的例子:
以下内容将指定 x 轴仅每三个月显示一次,格式为
Jan '09
(假设为英语区域设置)。由于您的日期和时间是单独存储的,您可能想要
DateTimeField
,或组合
它们。例如:
要迭代两个序列并将它们组合起来,您可以使用
zip
(代码仅用于说明目的,不一定是优化的):如果您在实现细节时遇到困难,请随意提出另一个问题。
Have a look at the documentation for
plot_date
. Convenientlyplot_date
takes similar arguments toplot
. A call might look like:Using
matplotlib.dates
you can then customize the format of your x-axis labels.A simple example:
The following will specify that the x-axis displays only every third month in the format
Jan '09
(assuming English-speaking locale).Since you have dates and times stored separately you may either want to
DateTimeField
, orcombine
them.For example:
To iterate over two sequences and combine them you might use
zip
(code for illustrative purposes only, not necessarily optimized):Feel free to open another question if you get stuck implementing the specifics.