使用matplotlib从JSON绘制数据的最简单方法是什么?

时间:2017-06-18 18:49:33

标签: python json pandas matplotlib plot

所以我有一些json格式的数据,这里是一个片段:

"sell": [
           {
               "Rate": 0.001425,
               "Quantity": 537.27713514
           },
           {
               "Rate": 0.00142853,
               "Quantity": 6.59174681
           }
]

访问费率和数量的最简单方法是什么,以便我可以在Matplotlib中绘制它?我是否必须展平/标准化它,或者创建一个for循环来生成一个数组,或者我可以使用pandas或其他一些库将它自动转换为matplotlib友好数据吗?

我知道matplotlib可以通过几种方式处理输入

plt.plot([1,2,3,4], [1,4,9,16])

plt.plot([1,1],[2,4],[3,9],[4,16])

1 个答案:

答案 0 :(得分:10)

最简单的是DataFrame构造函数DataFrame.plot

import pandas as pd

d = {"sell": [
           {
               "Rate": 0.001425,
               "Quantity": 537.27713514
           },
           {
               "Rate": 0.00142853,
               "Quantity": 6.59174681
           }
]}

df = pd.DataFrame(d['sell'])
print (df)
     Quantity      Rate
0  537.277135  0.001425
1    6.591747  0.001429

df.plot(x='Quantity', y='Rate')

编辑:

也可以DataFrame用于function auth(userName, password) { $.ajax ({ type: "POST", //SEND TO MY SERVER URL url: "http:myserverlocationurl.com", dataType: 'json', async: false, data: '{"userName": "' + userName + '", "password" : "' + password + '"}', success: function (response) { alert(JSON.stringify(response)); } }) }