I am relatively knew to Python, so please bear with me if this is some stupid question but I cannot figure it out myself. I am also happy about any pointers to previously asked questions concerning the issue that I possibly missed.
So, the content of my file is the following:
file.txt
N 38668442 0.32%
V 14008521 0.12%
ADJ 16112215 0.13%
N 375644830 3.13%
V 380340512 3.17%
ADJ 438651326 3.66%
N 691647661 5.77%
V 832219601 6.94%
ADJ 540782210 4.51%
I like to turn the percentages of file.txt into a plot using matplotlib with the following code (excerpt):
import numpy as np
import matplotlib.pyplot as plt
# data to plot
n_groups = 3
N = (0.32, 3.13, 5.77)
V = (0.12, 3.17, 6.94)
ADJ = (0.13, 3.66, 4.51)
# create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.25
opacity = 0.7
rects1 = plt.bar(index, N, bar_width,
alpha=opacity,
color='darkred',
label='N')
rects2 = plt.bar(index + bar_width, V, bar_width,
alpha=opacity,
color='green',
label='V')
rects3 = plt.bar(index + bar_width + bar_width, ADJ, bar_width,
alpha=opacity,
color='darkblue',
label='ADJ')
plt.xlabel('Category')
plt.ylabel('Percentage')
plt.xticks(index + bar_width + bar_width + bar_width + bar_width, ('1.5', '2', '2.5'))
plt.ylim(0, 100)
plt.tight_layout()
plt.show()
In this code N, V and ADJ are all tuples. So, I thought of reading the percentages (without the percentage sign) from file.txt into a tuple that can then be used to generate the plot (instead of entering the values manually for N, V, ADJ).
I managed to create the tuples from the file but unfortunately an error message is raised if I use my generated tuples. What is the difference between the tuples? What am I missing or misunderstanding?
code for generating the tuples from file.txt
with open('file.txt', 'r') as f:
N_lines = []
V_lines = []
ADJ_lines = []
for line in f:
if line.startswith("N"):
line = line.strip()
line = line[:-1]
parts = line.split("\t")
N_lines.append(parts[2])
if line.startswith("V"):
line = line.strip()
line = line[:-1]
parts = line.split("\t")
V_lines.append(parts[2])
if line.startswith("ADJ"):
line = line.strip()
line = line[:-1]
parts = line.split("\t")
ADJ_lines.append(parts[2])
N = tuple(N_lines)
V = tuple(V_lines)
ADJ = tuple(ADJ_lines)
Also, I printed my tuples and they look slightly different as the quotes are around the items, so I tried to remove them.
I greatly appreciate any help or suggestions!