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 pltdata = {"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 pltimport pandas as pddata =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
Post your comments / questions
Recent Article
- The request was aborted: Could not create SSL/TLS secure channel -Error in Asp.net
- FieldError: Cannot resolve keyword 'id' into field in Django project
- How to hide the ID field from the Django admin?
- It is impossible to add a non nullable field without specifying a default. Django error
- ImportError: cannot import name 'url' from 'django.conf.urls' - Django Error
- How to Enable Virtualization in BIOS Security Settings in Intel Processors For Android Studio?
- Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it.
- AttributeError: 'NoneType' object has no attribute 'get_text' - Python
Related Article