gpt4 book ai didi

python - 尝试从 DataFrame 或 Matrix 创建热图

转载 作者:行者123 更新时间:2023-12-01 09:30:27 27 4
gpt4 key购买 nike

我有这样的示例数据。

product <- c('Credit')
startdate <- c('12/30/2018','12/30/2018','12/30/2018','12/30/2018','12/30/2018')
reporting_amount <- c('29918501.83','50000000','40000000','13766666.67','75000000')
mydata <- data.frame(product, startdate, reporting_amount)

这一切都来自于SQL Server;转储为 CSV 文件。我想根据该数据集创建热图。这是否需要转换为矩阵,或者我可以将数据帧输入热图中吗?

我尝试过这个:

heat_matrix <- data.matrix(heat)
heat_heatmap <- heatmap(heat_matrix, Rowv=NA, Colv=NA, col = cm.colors(256), scale="column", margins=c(5,10))

然后我得到了这个:

enter image description here

我觉得我需要几个维度才能使这项工作顺利进行。我每个日期有多个产品,每个产品有多个 reporting_amount 值。该数据集基本上是 SQL Server 按产品按日期排列的前 10 名收入。

最终,我希望看到这样的东西!

enter image description here

但不要列出股票代码和上涨/下跌百分比,而是列出某个日期或所有日期的产品和报告金额。如果更容易的话,一次约会就可以了。显然这是 R 代码,但如果 Python 是适合此类工作的更好工具,我可以轻松切换到 Python。

最佳答案

您的最后一个示例看起来不像热图,而是树状图。也许你可以试试这个:

library(treemapify)
product <- c('Credit')
startdate <- c('12/30/2018','12/30/2018','12/30/2018','12/31/2018','12/31/2018')
reporting_amount <- c(29918501.83,50000000,40000000,13766666.67,75000000)
mydata <- data.frame(product, startdate, reporting_amount)
mydata$product <- as.character(product)

用于定义区域或颜色(填充)的reporting_amount应该是数字而不是字符,所以我删除了引号。标签(这里我使用产品)应该是字符。

ggplot(mydata,aes(area = reporting_amount,fill = reporting_amount,subgroup = startdate,label = product)) +
geom_treemap() +
geom_treemap_subgroup_border(size = 10)+
geom_treemap_text(color = 'white',grow = T,place = 'center') +
geom_treemap_subgroup_text()

然后我得到了这张照片:

enter image description here

我不确定这是否是您正在寻找的,只是面积和颜色可以按某个值改变,看起来与您的最终示例非常相似。也许当数据集中有更多维度时,树状图可以定义更多特征。

关于python - 尝试从 DataFrame 或 Matrix 创建热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50011028/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com