小编bia*_*tor的帖子

如何在Django/Python中减去两个日期?

我正在研究一个小健身追踪器,以便自学Django.我希望随着时间的推移绘制我的体重图,所以我决定使用Python Google Charts Wrapper.Google图表要求您将日期转换为ax坐标.要做到这一点,我想通过从最后一次称重中减去第一次称重然后使用它来计算x坐标来获取数据集中的天数(例如,我可以通过结果得到100并增加x坐标每个y坐标的结果数.)

无论如何,我需要弄清楚如何从彼此中减去Django日期时间对象,到目前为止,我在谷歌和堆栈中都是罢工.我知道PHP,但从来没有掌握OO编程,所以请原谅我的无知.这是我的模型的样子:

class Goal(models.Model):
    goal_weight = models.DecimalField("Goal Weight", 
        max_digits=4, 
        decimal_places=1)
    target_date = models.DateTimeField("Target Date to Reach Goal")
    set_date = models.DateTimeField("When did you set your goal?")
    comments = models.TextField(blank=True)

    def __unicode__(self):
        return unicode(self.goal_weight)

class Weight(models.Model):
    """ Weight at a given date and time. """

    goal = models.ForeignKey(Goal)
    weight = models.DecimalField("Current Weight",
        max_digits=4, 
        decimal_places=1)
    weigh_date = models.DateTimeField("Date of Weigh-In")
    comments = models.TextField(blank=True)

    def __unicode__(self):
        return unicode(self.weight)

    def recorded_today(self):
        return self.date.date() == datetime.date.today()
Run Code Online (Sandbox Code Playgroud)

关于如何在视图中进行的任何想法?非常感谢!

python django time datetime date

35
推荐指数
3
解决办法
4万
查看次数

将igraph对象转换为R中的数据框

我正在使用iGraph库,我需要在网络上运行一些统计分析.我正在使用iGraph计算几个变量,然后想要将这些指标用作几个回归中的因变量,将顶点属性用作模型中的自变量.

所以,我能够加载数据,运行igraph分析,但是我无法将igraph对象转回数据框.我真的不需要保留边缘,只需将每个顶点转换为观察结果,每个行中的属性作为列.

我尝试了以下方法:

fg <- fastgreedy.community(uncompg, merges=TRUE)
z<-which.max(fg$modularity)
fgc<- community.to.membership(uncompg, fg$merges,z)
names<-array(V(uncompg)$name)
fccommunity<-array(fgc$membership)
fcresult<-as.matrix(cbind(names,fccommunity))
compg <- set.vertex.attribute(compg, "community", value=fccommunity)

uncompg<-simplify(as.undirected(compg))
hubscore<-hub.score(compg)$vector
authscore<-authority.score(compg)$vector

netdata<-as.data.frame(compg)
Run Code Online (Sandbox Code Playgroud)

但它会引发以下错误:

  cannot coerce class '"igraph"' into a data.frame
Run Code Online (Sandbox Code Playgroud)

任何帮助或指针将不胜感激.

statistics r igraph

14
推荐指数
1
解决办法
1万
查看次数

是否可以在python中将xticks的文本包装在matplotlib中?

有人知道是否可以在matplotlib中包装xtick标签?现在我已经得到了以下代码(有点乱 - 已经黑了一段时间了):

def plotResults(request, question_id):
 responses = ResponseOption.objects.filter(question__id=question_id).order_by('order').annotate(response_num=Count('response'))

 counts = []
 labels = [] 

 for response in responses:
  counts.append(response.response_num)
  labels.append(smart_truncate('$'+response.text+'$'))

 N = len(labels)
 labels = tuple(labels)
 counts = tuple(counts)
 ind = na.array(range(N))+0.5
 width = .35
 fig = Figure(facecolor='white',edgecolor='white')
 ax = fig.add_subplot(1,1,1)


 rects1 = ax.bar(ind, counts,linewidth=0)

 ax.set_ylabel('$Count$')

 ax.set_title('$Response Historgram$')
 ax.set_xticks(ind+width)
 ax.set_xticklabels(labels)

 print mpl.matplotlib_fname()

 canvas = FigureCanvas(fig)
 response = HttpResponse(content_type='image/png')

 canvas.print_png(response)

 return response
Run Code Online (Sandbox Code Playgroud)

这会产生这样的情节:

替代文字

你可以看到xticks是骨头.关于如何包装它们的任何想法,或使它们可读的baring?再次感谢!

PS:这是Django项目的一部分.我将绘图作为png图像返回 - 通常在各种视图中从i​​mg标签中调用它们.

python django matlab matplotlib

5
推荐指数
2
解决办法
4471
查看次数

标签 统计

django ×2

python ×2

date ×1

datetime ×1

igraph ×1

matlab ×1

matplotlib ×1

r ×1

statistics ×1

time ×1