0

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!

2
  • What's the error message? Commented Apr 14, 2017 at 18:08
  • TypeError: unsupported operand type (s) for +: 'int' and 'str' Commented Apr 14, 2017 at 18:18

1 Answer 1

1

Your tuples from the file actually contain strings. That is why they can't be plotted.

To read in the file you can use the code below instead.

It will convert the values in the text file to decimals without the percent symbol so that you may use them in the plotting code.

import Decimal as dc

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(dc.Decimal(parts[2][:-1]))
        elif line.startswith("V"):
            line = line.strip()
            line = line[:-1]
            parts = line.split("\t")
            V_lines.append(dc.Decimal(parts[2][:-1]))
        elif line.startswith("ADJ"):
            line = line.strip()
            line = line[:-1]
            parts = line.split("\t")
            ADJ_lines.append(dc.Decimal(parts[2][:-1]))
Sign up to request clarification or add additional context in comments.

5 Comments

maybe a silly question but what is the advantage of this vs numpy.genfromtxt()
@bernie thank you! really nice! I did not know about that!
@dani_anyman: you're very welcome. If you like you can accept this answer with the checkmark if it helped you.
@Astrom: you could definitely use numpy here but I wanted to keep as close to the OP's code as possible.
@bernie of course, sorry I was not able to try it out before. It works perfectly, thank you so much! It is great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.