python中json属性的Matplotlib



我正试图将这个json文件属性转换为matplotlib,其中plot中的x"显示关注者的数量",y"拥有特定数量关注者的用户频率"。

这就是我的json文件看起来像的样子

[{"user": "person1", "follower": 1008, "following": 2520},
{"user": "person2", "follower": 144, "person3": 394},
{"user": "person4", "follower": 483, "following": 1582},...]

我已经将json文件导入到python中,但我不知道如何在matplotlib(而不是pandas plot(中使用特定属性绘制绘图

import pandas as pd
import json
instagram = json.loads(open('J:\data.json').read())
df = pd.DataFrame(instagram)
print (df)
df.plot(x='user', y='follower')

以下是如何在x轴上显示关注者以及在y轴上拥有该数量关注者的用户频率

import pandas as pd
import matplotlib.pyplot as plt
# if you are running in jupyter notebook - uncomment the below line
# %matplotlib inline
d = [{"user": "person1", "follower": 1008, "following": 2520},
{"user": "person6", "follower": 1008, "following": 2520},
{"user": "person7", "follower": 1008, "following": 2520},
{"user": "person3", "follower": 1008, "following": 123},
{"user": "person2", "follower": 144, "following": 394},
{"user": "person5", "follower": 144, "following": 987},
{"user": "person4", "follower": 483, "following": 1582}]
df = pd.DataFrame(d)
dfg = df.groupby('follower').count()
# using matplotlib scatter chart
plt.scatter(x=dfg.index, y=dfg['user'])
plt.show()
# using pandas plot
dfg.plot()

您可以使用:

import matplotlib.pyplot as plt
plt.scatter(df['x'], df['y'])
plt.show()

相关内容

最新更新