【发布时间】:2021-01-26 17:09:05
【问题描述】:
这是我在stackoverflow上的第一篇文章,如果我做错了什么,请见谅。
我正在创建一个烧瓶网络应用程序,您可以在其中回答调查。
我想根据调查回复创建图表,根据用户信息在其中显示调查回复,例如显示有多少人喜欢披萨与他们的年龄相关。
我将调查结果保存为 json 格式,如下所示:
{
"results": [
{
"question": {
"Do you like pasta?": "no",
"Do you like pizza": "no"
},
"user_info": {
"age": 17,
"gender": "Male"
}
},
{
"question": {
"Do you like pasta?": "no",
"Do you like pizza": "yes"
},
"user_info": {
"age": 19,
"gender": "Male"
}
},
{
"question": {
"Do you like pasta?": "yes",
"Do you like pizza": "no"
},
"user_info": {
"age": 13,
"gender": "Male"
}
}
]
}
il chart che vorrei creare è questo:
import matplotlib.pyplot as plt
import numpy as np
labels = ['13', '14', '15', '16']
respose_1 = [50, 20, 15, 66]
respose_2 = [79, 88, 71, 50]
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, respose_1, width, label='Si')
rects2 = ax.bar(x + width/2, respose_2, width, label='No')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Respose')
ax.set_xlabel('Age')
ax.set_title('TEST.')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
def autolabel(rects):
for rect in rects:
height = rect.get_height()
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
fig.tight_layout()
plt.savefig('test3.png')
plt.show()
所以我需要标签 respose_1、respose_2。 我花了几个小时试图从 json 中提取这些信息,但我做不到。
谁能帮帮我?
【问题讨论】:
标签: python json pandas matplotlib charts