Search code examples
pythonlinuxmatplotlibvisualizationlinux-mint

Creation of a Horizontal Bar Graph with data extracted from txt file Matplotlib Python3


Thanks to users VirtualScooter and Anton Volkov for the advice in my last post.

I have the following code:

    import os
    import sys
    import re
    import glob
    import datetime
    import subprocess
    import matplotlib.pyplot as plt; plt.rcdefaults()
    import numpy as np
    import matplotlib.pyplot as plt
    from functions import *
    
    
    event_dict = {1102: 0, 4611: 0,  4624: 0, 4634: 0, 4648: 0, 4661: 0, 4662: 0, 4663: 0, 4672: 0, 4673: 0, 4688: 0, 4698: 0, 4699: 0, 4702: 0, 4703: 0, 4719: 0, 4732: 0, 4738: 0, 4742: 0, 4776: 0, 4798: 0, 4799: 0, 4985: 0, 5136: 0, 5140: 0, 5142: 0, 5156: 0, 5158: 0}
    
    event_IDs = [1102,4611,4624,4634,4648,4661,4662,4663,4672,4673,4688,4698,4699,4702,4703,4719,4732,4738,4742,4776,4798,4799,4985,5136,5140,5142,5156,5158]
  

finding_matched_events()
    
    log_output()
    
    
    def plotting_events():
            #event_ids = []
            #event_count = []
            #with open('/home/user/CI5235_K1715142_Gary/CI5235_Logs/visdata_log_07_Mar_2021_15:22:34.txt', 'r') as bar:
    
    #height = [15, 2, 46, 1, 6]
            #bars = [1102, 4611, 4624, 4634, 4648]
            #y_pos = np.arange(len(bars))
            #plt.xlabel('Event ID codes', fontsize = 12) 
            #plt.ylabel('Event Count', fontsize = 12) 
            
         #plt.barh(y_pos, height)
     
    
            #plt.yticks(y_pos, bars)
    
            #plt.show()

The above code prints the following to a logfile:

Event ID: 1102 - Event Count: 15
Event ID: 4611 - Event Count: 2
Event ID: 4624 - Event Count: 46
Event ID: 4634 - Event Count: 1
Event ID: 4648 - Event Count: 6
Event ID: 4661 - Event Count: 19
Event ID: 4662 - Event Count: 33
Event ID: 4663 - Event Count: 114
Event ID: 4672 - Event Count: 12
Event ID: 4673 - Event Count: 2
Event ID: 4688 - Event Count: 35
Event ID: 4698 - Event Count: 2
Event ID: 4699 - Event Count: 2
Event ID: 4702 - Event Count: 4
Event ID: 4703 - Event Count: 1
Event ID: 4719 - Event Count: 8
Event ID: 4732 - Event Count: 2
Event ID: 4738 - Event Count: 5
Event ID: 4742 - Event Count: 4
Event ID: 4776 - Event Count: 7
Event ID: 4798 - Event Count: 2
Event ID: 4799 - Event Count: 1
Event ID: 4985 - Event Count: 2
Event ID: 5136 - Event Count: 42
Event ID: 5140 - Event Count: 6
Event ID: 5142 - Event Count: 1
Event ID: 5156 - Event Count: 92
Event ID: 5158 - Event Count: 14

When calling the function log_ouput.

I'm then writing another function labeled plot_events. In which I am attempting to use that information from the log file as shown above to visualize a horizontal bar graph.

I have attempted to do it through manual input as seen in the hashed code below the function. How would I be able to use that information in the log file and convert it into a horizontal bar graph, without manual input of all the logfile data so that it looks something like this:

Bar Graph Example

I'm fairly new to coding, using Python3, Linux Mint, and VisualStudio


Solution

  • After running the function finding_matched_events(), your event_dict has been transformed to the following:

    event_dict = {1102: 15, 4611: 2,  4624: 46, 4634: 1, 4648: 6, 4661: 19, 4662: 33, 4663: 114, 4672: 12, 4673: 2, 4688: 35, 4698: 2, 4699: 2, 4702: 4, 4703: 1, 4719: 8, 4732: 2, 4738: 5, 4742: 4, 4776: 7, 4798: 2, 4799: 1, 4985: 2, 5136: 42, 5140: 6, 5142: 1, 5156: 92, 5158: 14}
    

    You can plot your horizontal bar graph based on your event_dict as below:

    import matplotlib.pyplot as plt
    
    height = list(event_dict.values())
    bars = list(event_dict.keys())
    y_pos = list(range(len(bars)))
    
    plt.barh(y_pos, height)
    plt.yticks(y_pos, bars)
    plt.xlabel("Counts")
    plt.ylabel("Event ID Codes")
    plt.title("EVENT ID COUNTS")
    plt.show()
    

    enter image description here