pandas 使用 matplotlib 从 JSON 绘制数据的最简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44618376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easiest way to plot data from JSON with matplotlib?
提问by iontom
So I have some data in json format, here's a snippet:
所以我有一些json格式的数据,这是一个片段:
"sell": [
{
"Rate": 0.001425,
"Quantity": 537.27713514
},
{
"Rate": 0.00142853,
"Quantity": 6.59174681
}
]
What's the easiest way to access Rate and Quantity so that I can plot it in Matplotlib? Do I have to flatten/normalize it, or create a for loop to generate an array, or can I use pandas or some other library to convert it into matplotlib friendly data automatically?
访问 Rate 和 Quantity 以便我可以在 Matplotlib 中绘制它的最简单方法是什么?我是否必须对其进行展平/标准化,或者创建一个 for 循环来生成一个数组,或者我可以使用 Pandas 或其他一些库将其自动转换为 matplotlib 友好数据吗?
I know matplotlib can handle inputs in a few ways
我知道 matplotlib 可以通过几种方式处理输入
plt.plot([1,2,3,4], [1,4,9,16])
plt.plot([1,1],[2,4],[3,9],[4,16])
回答by jezrael
The simpliest is DataFrame
constructor with DataFrame.plot
:
最简单的是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')
EDIT:
编辑:
Also is possible use read_json
for DataFrame
.
也可read_json
用于DataFrame
.