我在编写一个脚本时遇到了一些困难,这个脚本可以用来自yaml或json文件的输入来生成一个图形。首先,我尝试用yaml文件,但是失败了-现在我把yaml文件转换成json文件,然后用它们来尝试。在
我想要什么?在
我有400多个json文件,其中包含有关特定结构的特定信息(有400多个结构,因此有400多个json文件)。我对json文件中的一个数据感兴趣,它是'Resolution':0.00(它是一些浮点值),因为我想知道有多少结构有什么分辨率(所以,x轴应该是出现在结构中的分辨率值,y轴应该是具有这种分辨率的结构的数量。在
我以前从未使用过matplotlib,所以我在脚本的开头就失败了,定义了x和y轴,以及如何实现json文件中的信息。在
import matplotlib.pyplot as plt
import numpy as np
import json
#this is preparation for my inputs, nothing important for my current question
cif_ID = "aaa, bbb" #there are 400+ IDs
cif_input = cif_ID.split(", ")
for ID in cif_input:
json_path = "/home/ME/something/something/{}/" .format(ID)
json_file = json_path + "{}.json" .format(ID)
with open(json_file, "r") as f:
doc = json.load(f)
resolution = doc['Resolution']
print(resolution) #this print me all resolution values (float) from all 400+ json files
x = (doc['Resolution']) #I know this is wrong way, but I want to just show that I tried
下面是基于您建议的json文件格式的示例代码。此代码将首先在“./data”目录中生成示例json文件,然后从这些文件中读取示例解析值。在
Ouput of this code is a bar plot. 希望这有帮助。请随意询问任何问题。在
相关问题 更多 >
编程相关推荐