简体   繁体   English

如何生成从 JSON 文件获取数据的图表

[英]How can I generate a chart getting data from a JSON file

I have a json file in which I have different data, I need to generate a graph whose legend is "Dates" and whose data is all the viewers of each user.我有一个 json 文件,其中有不同的数据,我需要生成一个图例,其图例为“日期”,其数据是每个用户的所有查看者。 I've been trying to do it all morning, but I have no idea, can someone help me?我整个早上都在尝试这样做,但我不知道,有人可以帮助我吗?

{
    "Avg": 49,
    "Max": 70,
    "Dates": ["01/01/2019 01:05", "01/01/2019 01:10", "01/01/2019 01:15", "01/01/2019 01:20", "01/01/2019 01:25", "01/01/2019 01:30", "01/01/2019 01:35"],
    "Viewers": [
        {
            "Name": "User1",
            "Viewers": [1,2,3,4,5,6,7]
        },
        {
            "Name": "User2",
            "Viewers": [2,3,4,5,6,7,8]
        },
        {
            "Name": "User3",
            "Viewers": [3,4,5,6,7,8,9]
        },
        {
            "Name": "User4",
            "Viewers": [4,5,6,7,8,9,10]
        },
        {
            "Name": "user5",
            "Viewers": [5,6,7,8,9,10,11]
        },
        {
            "Name": "User6",
            "Viewers": [6,7,8,9,10,11,12]
        },
        {
            "Name": "User7",
            "Viewers": [7,8,9,10,11,12,13]
        },
        {
            "Name": "Total",
            "Viewers": [28, 35, 42, 49, 56, 63, 70]
        }
    ]
}

Assume that you have put the json data in a faile named input.json假设您已将 json 数据放入名为 input.json 的故障中

import json
import matplotlib.pyplot as plt

with open('input.json') as fp:
    json_data = json.load(fp)

for v in json_data['Viewers']:
    plt.plot(json_data['Dates'], v['Viewers'], label=v['Name'])
    
plt.legend()
plt.xticks(rotation = 20)

# to save as png, it should be done before plt.show()
plt.savefig('graph.png')
plt.show()

And here is the result这是结果多重情节

Assuming original question is exact, meaning plot needs to show 'Dates' in Legend, and data should not include Total , only data for each user:假设原始问题是准确的,这意味着情节需要在 Legend 中显示“日期”,并且数据不应包括Total ,只有每个用户的数据:

  • understanding the data:理解数据:

      import pandas as pd将熊猫导入为 pd 
      import plotly.express as px将 plotly.express 导入为 px
      data = [...]数据 = [...]
      dates = data['Dates']日期 = 数据['日期']
      users = [x['Name'] for x in data['Viewers']] users = [x['Name'] for x in data['Viewers']]
      viewers = [x['Viewers'] for x in data['Viewers']][:-1] viewers = [x['Viewers'] for x in data['Viewers']][:-1]
      data_list = [(dates[index], users[index2], y) for index, x in enumerate(viewers) for index2, y in enumerate(x)] data_list = [(dates[index], users[index2], y) for index, x in enumerate(viewers) for index2, y in enumerate(x)]
      df = pd.DataFrame(data_list, columns =['Date', 'User', 'Viewers']) df = pd.DataFrame(data_list, columns =['Date', 'User', 'Viewers'])
      
  • plotting it:绘制它:

      fig = px.bar(df, y='Viewers', x = 'User', color = 'Date', barmode="group") fig = px.bar(df, y='Viewers', x = 'User', color = 'Date', barmode="group")
      fig.show()图.show()
      
  • result: Plot with 'Dates' as legend结果:以“日期”作为图例进行绘图

That being said, a time series is usually intelligible if timeline is on x axis.话虽如此,如果时间线在 x 轴上,时间序列通常是可以理解的。 However, given the requirements, I believe this is the correct answer.但是,鉴于要求,我相信这是正确的答案。

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

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