如何用Python创建非ASCII树状图?
我在尝试用找到的代码块创建一个树状图,运行到某个地方时一切正常:
r('mt_dist <- dist(t(mt))')
然后就出现了这个错误:
RPy_RException: dist(t(mt)) 中出错:无法将(列表)对象转换为 'double' 类型
在那之前一切看起来都不错……我可能漏掉了什么很简单的东西
有没有人能帮帮我?
#importing modules
from numpy import array
from random import normalvariate,shuffle
from rpy import r
# creating a random matrix
# creating it with different 'samples' in different columns
mt = []
for l in range(20): #20 lines
line = []
means = range(1,9)
for c in range(8): # 8 columns
#Colum 1: mean 1; Column 2: mean 2.... values normally distributed s.d. = 0.5
line.append(normalvariate(means.pop(), 0.5))
mt.append(line)
# once we have a matrix, transform it in an array
mt_array = array(mt)
# The R work
# Pass the array to 'mt' variable in R
r.assign("mt", mt_array)
# manipulate R via r('command')
r('print(mt)') #print the matrix 'mt' to check values
#The clustering process
#Calculating distances with 'dist'
#'dist' calculates distance among lines, so I am transposing (with t()) in order to have my columns clustered
## I guess 'dist' uses euclidian distance as default
r('mt_dist <- dist(t(mt))')
# hclust does the clustering with upgma as default
r('result = hclust(mt_dist)')
# directs the output to a determinde file
r('png("output_file.png")')
# plot the result
labels = ["sample A", "sample B","sample C", "sample D","sample E", "sample F", "sample G", "sample H"]
r.assign("labels", labels)
r('plot(result, labels=labels, main="My title")')
# 'close' you output
r('dev.off()')