Reputation: 61
I'm trying to make a graph that has multiple stacked bar graphs, here is my code:
import numpy as np
import matplotlib.pyplot as plt
NEAlosses1 = open("NEA losses with altitude - 2015-2020.txt", "r")
NEAlosses2 = open("NEA losses with altitude - 2015-2025.txt", "r")
NEAlosses3 = open("NEA losses with altitude - 2015-2035.txt", "r")
altitudes = ['0.0', '30.0', '40.0', '50.0', '60.0']
data1 = {} # 2015-2020
for line in NEAlosses1:
col = line.strip().split("|")
location = col[0]
altitude = col[1]
eighteen = col[2] # V < 18
twenty = col[3] # V < 20
twentytwo = col[4] # V < 22
try:
data1[location][str(altitude)] = [eighteen, twenty, twentytwo]
except KeyError:
data1[location] = {}
data1[location][str(altitude)] = [eighteen, twenty, twentytwo]
data2 = {} # 2015-2025
for line in NEAlosses2:
col = line.strip().split("|")
location = col[0]
altitude = col[1]
eighteen = col[2] # V < 18
twenty = col[3] # V < 20
twentytwo = col[4] # V < 22
try:
data2[location][str(altitude)] = [eighteen, twenty, twentytwo]
except KeyError:
data2[location] = {}
data2[location][str(altitude)] = [eighteen, twenty, twentytwo]
data3 = {} # 2015-2035
for line in NEAlosses3:
col = line.strip().split("|")
location = col[0]
altitude = col[1]
eighteen = col[2] # V < 18
twenty = col[3] # V < 20
twentytwo = col[4] # V < 22
try:
data3[location][str(altitude)] = [eighteen, twenty, twentytwo]
except KeyError:
data3[location] = {}
data3[location][str(altitude)] = [eighteen, twenty, twentytwo]
NEAlosses1.close(); NEAlosses2.close(); NEAlosses3.close()
for i in data1:
eighteen1 = []; twenty1 = []; twentytwo1 = []
eighteen2 = []; twenty2 = []; twentytwo2 = []
eighteen3 = []; twenty3 = []; twentytwo3 = []
for j in altitudes:
eighteen1.append(data1[i][j][0])
twenty1.append(data1[i][j][1])
twentytwo1.append(data1[i][j][2])
eighteen2.append(data2[i][j][0])
twenty2.append(data2[i][j][1])
twentytwo2.append(data2[i][j][2])
eighteen3.append(data3[i][j][0])
twenty3.append(data3[i][j][1])
twentytwo3.append(data3[i][j][2])
# plot details
bar_width = 0.25
epsilon = .015
line_width = 1
opacity = 0.7
centre_bar_positions = np.arange(1, len(eighteen1)+1)
left_bar_positions = centre_bar_positions - bar_width
right_bar_positions = centre_bar_positions + bar_width
# Make bar plots
bar_2020_18 = plt.bar(left_bar_positions, eighteen1, bar_width, color='red')
bar_2020_20 = plt.bar(left_bar_positions, twenty1, bar_width-epsilon, alpha=opacity, color='white', edgecolor='red', linewidth=line_width, hatch='//')
bar_2020_22 = plt.bar(left_bar_positions, twentytwo1, bar_width-epsilon, alpha=opacity, color='white', edgecolor='red', linewidth=line_width, hatch='0')
bar_2025_18 = plt.bar(centre_bar_positions, eighteen2, bar_width, color='blue')
bar_2025_20 = plt.bar(centre_bar_positions, twenty2, bar_width-epsilon, alpha=opacity, color='white', edgecolor='blue', linewidth=line_width, hatch='//')
bar_2025_22 = plt.bar(centre_bar_positions, twentytwo2, bar_width-epsilon, alpha=opacity, color='white', edgecolor='blue', linewidth=line_width, hatch='0')
bar_2035_18 = plt.bar(right_bar_positions, eighteen3, bar_width, color='green')
bar_2035_20 = plt.bar(right_bar_positions, twenty3, bar_width-epsilon, alpha=opacity, color='white', edgecolor='green', linewidth=line_width, hatch='//')
bar_2035_22 = plt.bar(right_bar_positions, twentytwo3, bar_width-epsilon, alpha=opacity, color='white', edgecolor='green', linewidth=line_width, hatch='0')
plt.xticks(centre_bar_positions, altitudes)
plt.ylabel("Number of NEAs")
plt.xlabel("Altitude")
plt.show()
But when I run it, I keep getting the error: TypeError: unsupported operand type(s) for +: 'int' and 'str'. The traceback points to the line
bar_2020_18 = plt.bar(left_bar_positions, eighteen1, bar_width, color='red')
But I don't see where in that line I'm adding a int and str...
Thanks in advance for your help!! It's much appreciated :)
Upvotes: 1
Views: 1916
Reputation: 3386
eighteen1
is a list which contains strings of numbers. As a result, you cannot implicitly pass this list as an integer to plt.bar
. Remember to convert from strings to floats!
Upvotes: 2