| Library Name | Import Command | Description |
|---|---|---|
| Matplotlib | import matplotlib.pyplot as plt | We are importing pyplot from matplotlib library as pyplot is the main function which will help in plotting the charts. |
| Pandas | import pandas as pd | We are importing full pandas library with alias name "pd". |
| Numpy | import numpy as np | We are importing full numpy library with alias name "np." |
import matplotlib.pyplot as plt
grocery_data={"Bread":50, "Milk":70, "Veggies": 150, "Nuts": 500, "Fries":20}
item= list(grocery_data.keys())
amount= list(grocery_data.values())
fig, ax = plt.subplots()
plt.bar(item, amount, color="green", width=0.2)
plt.xlabel("Groceries")
plt.ylabel("Price")
#plt.show()
fig
In the above code we have data in form of dictionary from which we are extracting information in two different lists and representing it with the chart to analyze the spendings.
We can use the below sample python program in the above compiler to generate matplotlib pie chart.
import matplotlib.pyplot as plt
x=[5,9,14]
y=['5','9','14']
fig, ax = plt.subplots()
plt.pie(x,labels=y)
#plt.show()
fig
import matplotlib.pyplot as plt
from matplotlib import style
x=[5,9,14]
y=[6,8,15]
x1=[7,10,12]
y1=[9,11,18]
fig, ax = plt.subplots()
plt.plot(x,y, label="Principal")
plt.plot(x1,y1, label="Interest")
plt.legend()
fig