0

I have already done a line plot using the csv data and import matplotlib.pyplot as plt charge, total and year have already been append by rows. csv table

My code

plt.plot(year, charge, '-g', label = 'Chargeable Income')
plt.plot(year, total, '-r', label = 'Total Income')
plt.title('Chargeable and Total Income VS Year')
plt.xlabel('Year')
plt.ylabel('(S$)')

plt.grid()
plt.legend()
plt.show()

This is how the current line plot looks like:

this is how the current line plot looks like

How do I code so that it becomes a bar chart like this: this

QuagTeX
  • 112
  • 1
  • 12
Jon
  • 5
  • 3

1 Answers1

0

There is an example in the matplotlib site and you can find another approach of your issue here. Basically, you just shift the x values by width. Here is the relevant bit:

import numpy as np
import matplotlib.pyplot as plt
# create sample data as np.array in the provided format year | chargeable | total
arr = np.array([[2017, 375873, 78833], [2018, 783893, 98288]])

ind = np.arange(arr.shape[0])  # the x locations for the groups
width = 0.35       # the width of the bars

fig = plt.figure()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, arr[:, 2], width, color='royalblue')

rects2 = ax.bar(ind+width, arr[:, 1], width, color='seagreen')

# add some
ax.set_ylabel('(S$)')
ax.set_ylabel('Year')
ax.set_title('Chargeable and Total Income VS Year')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels( arr[:, 0] )

ax.legend( (rects1[0], rects2[0]), ('Chargeable Income', 'Total Income') )

plt.show()
QuagTeX
  • 112
  • 1
  • 12
  • instead of typing "arr = np.array([[2017, 375873, 78833], [2018, 783893, 98288]])" is there any other way to get the number from the csv data since i have already imported csv and also appended the year, charge and total income? – Jon Jul 17 '21 at 14:53
  • which number do you mean? Do you mean how you can access e.g. a specific line in your imported csv data or loading values without importing the csv file? Could you define your question more precisely? – QuagTeX Jul 18 '21 at 08:43
  • I'll try to explain clearer. So let's say I want to display "chargeable" and "total income" VS "Years" as a bar chart, which means that I need to tell the code what are the values for each of them. Instead of manually type "2017, 375873, 78833" and other years, is there any other way to retrieve the data straightaway and display it? I don't wish to type every value for each year. I have already assigned all values of Year, Total_Income and Chargeable_Income as year, total and charge in my code. – Jon Jul 19 '21 at 15:19
  • @Jon If your data is in a [.csv](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html) or [.xlsx](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html) file format (or other tabular formats) you can easily import them into python as an array. – QuagTeX Jul 20 '21 at 06:20
  • Oh yes how do i do that – Jon Jul 25 '21 at 12:22
  • Just click on the attached links in the previous comment :) – QuagTeX Jul 25 '21 at 20:38