0

I need some help with creating this graph using matplotlib.

This is where the event id and the count are being extracted from:

this is where the event id and the count are being extracted from

I need to create a graph. For this I have attempted some code for this but I keep getting errors as there are lists to extract and I am honestly quite confused and this is the code I have done so far:

def plotnew():
    event = []
    eventcount = []

    with open("path here", 'r') as visualise:
        for line in visualise:
            if line.startswith("Event ID: "):
                event.append(line.split([2]))
            elif line.startswith("Count:"):
                eventcount.append(int(line.split()[2]))

    plt.barh(event, eventcount)
    plt.xlabel('Event Count')
    plt.ylabel('Event ID')
    plt.title('EventID Count')
    plt.tight_layout()
    plt.show()

I have tried to add [3] after the line split to get the count value or event id but I keep getting errors all over the place and I am quite confused.

7
  • What errors are you getting? What do the variables eve and eventcount return after your for loop has completed? Commented Mar 19, 2021 at 19:54
  • @DerekO TypeError: the dtypes of parameters y (object) and height (float64) are incompatible - this is one of them and its because they are in a list and i do not know how to fix this issue Commented Mar 19, 2021 at 20:14
  • @JohanC i have amended it sorry about that and I tried it with 2 and I get this error now TypeError: 'builtin_function_or_method' object is not subscriptable Commented Mar 19, 2021 at 21:13
  • @JohanC i need to make a simple bar graph with the extracted data e.g. in the image I have given but I keep getting errors I do not know what to do Commented Mar 19, 2021 at 22:11
  • @JohanC after i changed that thanks for pointing it out now I get this error: TypeError: must be str or None, not list Commented Mar 20, 2021 at 0:46

1 Answer 1

2

The following code mimics reading a file via StringIO, to make the example code self-containing. In your code you can just keep reading from the file.

For each line in the file, the code first tests whether it can have useful information, here testing the line length to be at least 10. Then, the start of the line is checked: if the line starts with "Event ID:", we split the line and take the 3rd part to eve. If the line starts with "Event Count:", the 3rd part is converted to an integer and appended to eventcount.

import matplotlib.pyplot as plt
from io import StringIO

file_contents = '''
Event ID: 1102
Event Count: 15

Event ID: 4611
Event Count: 2

Event ID: 4624
Event Count: 46

Event ID: 4634
Event Count: 1
'''

eve = []
eventcount = []

# with open("path", 'r') as visualise: # when really reading from a file
with StringIO(file_contents) as visualise: # when mimicking a file with a string
    for line in visualise:
        if len(line) > 10:
            if line.startswith("Event ID:"):
                eve.append(line.split()[2])
            elif line.startswith("Event Count:"):
                eventcount.append(int(line.split()[2]))

plt.barh(eve, eventcount, color='springgreen')
plt.xlabel('Event Count')
plt.ylabel('Event ID')
plt.title('EventID Count')
plt.tight_layout()
plt.show()

barh plot

Sign up to request clarification or add additional context in comments.

7 Comments

for real i feel so unmotivated i literally do not understand, I got a file that has a bunch of the event ids with the count of how many times it has been found and its in a simple text format as shown in the picture at the top and it needs to make a bar graph as you have made but it does not work
seriously appriciat you helping me and going out of your way honestly but I literally do not know why it does not work for me with the text file I got.
Does your file look exactly like the example string? Do you get errors? Do the lists eve and eventcount get filled in? Are they filled in as expected? Maybe you can run my code exactly as written, and copy-paste part of the file into the string?
i copied your one and it does not work it comes blank with negative values on the axis I don't get why with no axes
I have edited the code on the page on what I have made
|

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.