Sharpened Bar Graph in Matplotlib

Asked

Viewed 29 times

-1

I’m working on a chart that involves a file. csv, my question is what would be the best way to print a chart with 3 bars grouped for each country/country that contains: Median_wealth, Mean_wealth and Population. Follow my code, what it is printing and the contents of the CSV file:

Code:

import matplotlib.pyplot as plt
import csv
  
with open('wealth-per-country.csv', 'r', newline="") as arq:
    reader = csv.reader(arq)
    next(reader)
    for column in csv.reader(arq):
        if column: 
            print(f"Country: {column[0]}, \nMedian_Wealth: {column[1]}, \nMean_Wealth: {column[2]}, \nPopulation: {column[3]}")

Printa:

Country: Switzerland, 
Median_Wealth: 227,891, 
Mean_Wealth: 564,653, 
Population: 6,866
Country: Australia, 
Median_Wealth: 181,361, 
Mean_Wealth: 386,058, 
Population: 18,655
Country: Iceland, 
Median_Wealth: 165,961, 
Mean_Wealth: 380,868, 
Population: 250
Country: Hong Kong, 
Median_Wealth: 146,887, 
Mean_Wealth: 489,258, 
Population: 6,267
Country: Luxembourg, 
Median_Wealth: 139,789, 
Mean_Wealth: 358,003, 
Population: 461
Country: Belgium, 
Median_Wealth: 117,093, 
Mean_Wealth: 246,135, 
Population: 8,913
Country: New Zealand, 
Median_Wealth: 116,433, 
Mean_Wealth: 304,124, 
Population: 3,525
Country: Japan, 
Median_Wealth: 110,408, 
Mean_Wealth: 238,104, 
Population: 104,963
Country: Canada, 
Median_Wealth: 107,004, 
Mean_Wealth: 294,255, 
Population: 29,136
Country: Ireland, 
Median_Wealth: 104,842, 
Mean_Wealth: 272,310, 
Population: 3,491

CSV:

Country,Median_Wealth,Mean_Wealth,Population
Switzerland,"227,891","564,653","6,866"
Australia,"181,361","386,058","18,655"
Iceland,"165,961","380,868",250
Hong Kong,"146,887","489,258","6,267"
Luxembourg,"139,789","358,003",461
Belgium,"117,093","246,135","8,913"
New Zealand,"116,433","304,124","3,525"
Japan,"110,408","238,104","104,963"
Canada,"107,004","294,255","29,136"
Ireland,"104,842","272,310","3,491"

1 answer

0


Using the data shown, I would make it as simple as possible ...

Take a look at how I’m using the append to store the appropriate data in specific lists, I am rudimentary using a replace to remove the comma and convert the values to a float, after giving the proper sanitized data is only create subplots of each specific var with the data of interest, obviously use the plt.bar to plot in bar form, running here in a version python3.3 I used numpy to linearly create the aide vectors of Plot, I believe it can be replaced by another equivalent function, follow the code:

import matplotlib.pyplot as plt
import numpy as np
import csv

country=[]
Population=[]
Median_Wealth=[]
Mean_Wealth=[]

  
with open('stack_csv.csv', 'r', newline="") as arq:
    reader = csv.reader(arq)
    next(reader)
    for column in csv.reader(arq):
        if column: 
            country=np.append(country, column[0])
            column[1]=column[1].replace(',', '.')
            Median_Wealth=np.append(Median_Wealth, column[1])
            column[2]=column[2].replace(',', '.')
            Mean_Wealth=np.append(Mean_Wealth, column[2])
            column[3]=column[3].replace(',', '.')
            Population=np.append(Population, column[3])
            

width = 0.15

x1 = np.arange(len(country))
x2 = [x + width for x in x1]
x3 = [x + width for x in x2]

Median_Wealth = list(map(float, Median_Wealth))
Mean_Wealth = list(map(float, Mean_Wealth))
Population = list(map(float, Population))


fig, ax = plt.subplots()


plt.bar(x1, Median_Wealth, color='blue', width=width, edgecolor='white', label='Median_Wealth')
plt.bar(x2, Mean_Wealth, color='green', width=width, edgecolor='white', label='Mean_Wealth')
plt.bar(x3, Population, color='red', width=width, edgecolor='white', label='Population')

ax.set_ylabel('Valores')
ax.set_title('Agrupados')
ax.set_xticks(x1)
ax.set_xticklabels(country)
ax.legend()

fig.tight_layout()

plt.show()

The plot of the above code looked like this:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.