c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
Python

How to plot data from json with matplotlib in python?

| | python

In this tutorial I will show you how to plot data from json with matplotlib in python. Python's matplotlib library is used for data visualization and graphing. To begin we need to import matplotlib like this,

import matplotlib.pyplot as plt
Method 1:
import matplotlib.pyplot as plt

data = {
    "sell": [
        {
            "Rate": 0.0545,
            "Quantity": 330
        },
        {
            "Rate": 0.0314,
            "Quantity": 650
        },
        {
            "Rate": 0.0242,
            "Quantity": 200
        }
    ]}

Rates = [i["Rate"] for i in data["sell"]]
Qty = [i['Quantity'] for i in data['sell']]

plt.plot(Rates, Qty)
plt.show()

Method 2:

create a demo.json file and paste this code.

{
    "sell": [
        {
            "Rate": 0.0545,
            "Quantity": 330
        },
        {
            "Rate": 0.0314,
            "Quantity": 650
        },
        {
            "Rate": 0.0242,
            "Quantity": 200
        }
    ]
}

To access data from the json file using pandas. We can load the data using the read_csv function.

import matplotlib.pyplot as plt
import pandas as pd

data =pd.read_json('demo.json')

Rates = [i["Rate"] for i in data["sell"]]
Qty = [i['Quantity'] for i in data['sell']]

plt.plot(Rates, Qty)
plt.show()

VIDEO TUTORIAL: Plot data from JSON