0
$\begingroup$

I wanted to plot a bar graph which shows covid cases reported in different regions in different regions of USA.

So here is my code:

import pandas as pd
import matplotlib.pyplot as plt
datainput = pd.read_csv("MD_COVID-19.csv")
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
rgn1=list(datainput['Allegany'])
rgn2=list(datainput['Anne_Arundel'])
rgn3=list(datainput['Baltimore'])
rgn4=list(datainput['Baltimore_City'])
rgn5=list(datainput['Calvert'])
rgn6=list(datainput['Caroline'])
rgn7=list(datainput['Washington'])
region=['Allegany','Anne_Arundel','Baltimore','Baltimore_City','Calvert','Caroline','Washington']
rgn1s=0
rgn2s=0
rgn3s=0
rgn4s=0
rgn5s=0
rgn6s=0
rgn7s=0
for item in rgn1:
 rgn1s +=item
for item in rgn2:
 rgn2s +=item
for item in rgn3:
 rgn3s +=item
for item in rgn4:
 rgn4s +=item
for item in rgn5:
 rgn5s +=item
for item in rgn6:
 rgn6s +=item
for item in rgn7:
 rgn7s +=item
tc=[]
tc.append(rgn1s)
tc.append(rgn2s)
tc.append(rgn3s)
tc.append(rgn4s)
tc.append(rgn5s)
tc.append(rgn6s)
tc.append(rgn7s)
ax.bar(region,tc)
plt.ylabel("Number of Cases")
plt.show()

My bar graph is shown as above. Why are only three bar graphs coming? Where are the other four? Also how can I draw pie chart?

$\endgroup$
2
$\begingroup$

The reason the other bars are missing is because of your method of summing the values and the missing values in your dataset. The way you are adding the values together means that if even one value is missing (NA) the total for that column will be missing as well, and as a result, will not be in your final plot. It is better to use pandas built-in methods (for conciseness, speed, but also to avoid errors such as the one you encountered) to add up the values as follows (using random values):

import pandas as pd
import matplotlib.pyplot as plt

datainput = pd.read_csv("MD_COVID-19.csv")
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
regions = ['Allegany', 'Anne_Arundel', 'Baltimore', 'Baltimore_City', 'Calvert', 'Caroline', 'Washington']
# select all region columns and calculate the sum column-wise
total_cases = datainput[regions].sum()
ax.bar(regions, total_cases)
plt.ylabel("Number of Cases")
plt.show()

Which gives the following result:

enter image description here

$\endgroup$
6
  • $\begingroup$ Is there any way we can take the input so that we ignore black spaces @Oxbowerce $\endgroup$
    – Anonymous
    Jan 28 at 10:33
  • $\begingroup$ What do you mean with "ignore blank spaces"? You want to not count blank spaces (i.e. count them as zero) or remove rows where at least one of the regions has no value? $\endgroup$
    – Oxbowerce
    Jan 28 at 10:48
  • $\begingroup$ Yes suppose now if I want to input Allegany I couldn't use rgn1=list(datainput['Allegany']) because of blank spaces in Allegany dataset. So how could I input the data in Allegany? What method I have to use? @Oxbowece $\endgroup$
    – Anonymous
    Jan 28 at 11:03
  • $\begingroup$ You can use just the Allegany column, however you just have to use the pandas methods of selecting the rows/columns as pandas takes missing values into account. Simply use datainput["Allegany"].sum() to get the sum of the 'Allegany' column. Look into the pandas library and it's methods to index and select data. $\endgroup$
    – Oxbowerce
    Jan 28 at 11:11
  • $\begingroup$ No I don't want the sum I just want to input the data as a list. @Oxbowerce $\endgroup$
    – Anonymous
    Jan 28 at 11:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.