简体   繁体   English

如何从字典列表创建 FusionCharts 热图?

[英]How to create FusionCharts heatmap from a list of dictionaries?

I want to show below data using FusionCharts heatmap in python:我想在 python 中使用 FusionCharts 热图显示以下数据:

[{'day_of_like': 'Monday', 'hours_of_like': 18, 'avg_of_likes': 8}, {'day_of_like': 'Monday', 'hours_of_like': 23, 'avg_of_likes': 5}]

But I can't find guide for that in FusionCharts site.但我在 FusionCharts 网站上找不到这方面的指南。 How can I do that using python?我怎样才能使用 python 做到这一点?

import pandas as pd
import seaborn as sns

# list of dictionaries
data = [{'day_of_like': 'Monday', 'hours_of_like': 18, 'avg_of_likes': 8}, {'day_of_like': 'Monday', 'hours_of_like': 23, 'avg_of_likes': 5}]

# convert to dataframe
df = pd.json_normalize(data)

# save the data to a csv if needed
df.to_csv('test.csv', index=False)

# display(df)
  day_of_like  hours_of_like  avg_of_likes
0      Monday             18             8
1      Monday             23             5

# to json
df.to_json()

[out]:
'{"day_of_like":{"0":"Monday","1":"Monday"},"hours_of_like":{"0":18,"1":23},"avg_of_likes":{"0":8,"1":5}}'

# to dict
df.to_dict()

[out]:
{'day_of_like': {0: 'Monday', 1: 'Monday'}, 'hours_of_like': {0: 18, 1: 23}, 'avg_of_likes': {0: 8, 1: 5}}

# plot
sns.heatmap(df[['hours_of_like', 'avg_of_likes']])

在此处输入图像描述

Based on the data shared, looks like you have to duplicate day_of_like property, you need to either set a unique value or set a unique value for each duplicate key here is a JavaScript representation of the same in FusionCharts - http://jsfiddle.net/j3r0avhz/3/根据共享的数据,看起来您必须复制 day_of_like 属性,您需要设置一个唯一值或为每个重复键设置一个唯一值,这里是 FusionCharts 中相同的 JavaScript 表示 - http://jsfiddle.net /j3r0avhz/3/

  "dataset": [{
          "data": [{
              "rowid": "hl",
              "columnid": "1",
              "value": "18",
              "colorRangeLabel": "Bad"
            },
            {
              "rowid": "al",
              "columnid": "1",
              "value": "8",
              "colorRangeLabel": "Bad"
            },
          {
              "rowid": "hl",
              "columnid": "2",
              "value": "23",
              "colorRangeLabel": "Good"
            },
            {
              "rowid": "al",
              "columnid": "2",
              "value": "5",
              "colorRangeLabel": "Bad"
            }
          ]
        }]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM