[英]Easiest way to plot data from JSON with matplotlib?
所以我有一些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])
最簡單的是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')
編輯:
也可以使用read_json
進行DataFrame
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.