Django 图表 - 日期和时间轴

发布于 2024-11-02 04:52:58 字数 1302 浏览 1 评论 0原文

我有一个模型,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏蝶舞 2024-11-09 04:52:58

查看 plot_date 的文档。方便的是,plot_date 采用与 plot 类似的参数。调用可能如下所示:

p.plot_date(sequence_of_datetime_objects, y_axis_values, 'go-') 

使用 matplotlib.dates然后您可以自定义 x 轴标签的格式。
一个简单的例子:
以下内容将指定 x 轴仅每三个月显示一次,格式为 Jan '09(假设为英语区域设置)。

p.xaxis.set_major_locator(mdates.MonthLocator(interval=3))
p.xaxis.set_major_formatter(mdates.DateFormatter("%b '%y"))

由于您的日期和时间是单独存储的,您可能想要

  1. 更改模型以使用 DateTimeField,或
  2. 使用 Python 组合它们。

例如:

import datetime as dt
t1 = dt.time(21,0,1,2) # 21:00:01.2
d1 = dt.date.today()
dt1 = dt.datetime.combine(d1,t1)
# result: datetime.datetime(2011, 4, 15, 21, 0, 1, 2)

要迭代两个序列并将它们组合起来,您可以使用 zip(代码仅用于说明目的,不一定是优化的):

sequence_of_datetime_objects = []
for a_date, a_time in zip(sequence_of_date_objects, sequence_of_time_objects):
    sequence_of_datetime_objects.append(dt.datetime.combine(a_date, a_time))

如果您在实现细节时遇到困难,请随意提出另一个问题。

Have a look at the documentation for plot_date. Conveniently plot_date takes similar arguments to plot. A call might look like:

p.plot_date(sequence_of_datetime_objects, y_axis_values, 'go-') 

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).

p.xaxis.set_major_locator(mdates.MonthLocator(interval=3))
p.xaxis.set_major_formatter(mdates.DateFormatter("%b '%y"))

Since you have dates and times stored separately you may either want to

  1. change your model to use a DateTimeField, or
  2. use Python to combine them.

For example:

import datetime as dt
t1 = dt.time(21,0,1,2) # 21:00:01.2
d1 = dt.date.today()
dt1 = dt.datetime.combine(d1,t1)
# result: datetime.datetime(2011, 4, 15, 21, 0, 1, 2)

To iterate over two sequences and combine them you might use zip (code for illustrative purposes only, not necessarily optimized):

sequence_of_datetime_objects = []
for a_date, a_time in zip(sequence_of_date_objects, sequence_of_time_objects):
    sequence_of_datetime_objects.append(dt.datetime.combine(a_date, a_time))

Feel free to open another question if you get stuck implementing the specifics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文