Answer :
Explanation:
Firstly, to do any kind of plotting in Python we make use of matplotlib library.
Then we create lists of expenses and categories to write them in a file.
Then we open a file named "expenses.txt" if it doesn't exist in your computer then it will generated automatically. The first argument is file name along with type of the file and second argument is r+ that is reading plus writing mode.
Then we used writelines() function to write the contents in the file. Then we used readlines() function to read the contents of the file.
Then we plotted the pie chart using plt.pie which takes the data, label names, starting angle doesn't really matter much it is up to your preference and finally 0.1 is the precision we just want percentage with 1 decimal point.
Then a title is given to the plot and then display the plot using plt.show() and finally close the file.
Python Code:
import matplotlib.pyplot as plt
expense = ["300", "150", "400", "150", "100", "200"]
category = ["Rent", "Gas", "Food", "Clothing", "Car Payment", "Misc"]
f =open("expense.txt", "r+")
f.writelines(category)
f.writelines(expense)
f.readlines()
f.readlines()
plt.pie (expense, labels = category, startangle = 90, autopct = '%.1f%%')
plt.title ('Montely Expense Pie Chart')
plt.show()
f.close ()
Output:
Attached as image
