使用从 txt 文件 Matplotlib 中提取的数据创建水平条形图 Python3

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

感谢用户 VirtualScooter 和 Anton Volkov 在我上次 post.

中的建议

我有以下代码:

    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()

以上代码将以下内容打印到日志文件中:

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

调用函数时log_ouput.

然后我正在编写另一个标记为 plot_events 的函数。我在其中尝试使用如上所示的日志文件中的信息来可视化水平条形图。

我试图通过手动输入来完成,如函数下方的散列代码所示。 我如何能够在日志文件中使用该信息并将其转换为水平条形图,而无需手动输入所有日志文件数据,使其看起来像这样:

我对编码相当陌生,使用 Python3、Linux Mint 和 VisualStudio

在 运行 函数 finding_matched_events() 之后,您的 event_dict 已转换为以下内容:

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}

您可以根据您的 event_dict 绘制水平条形图,如下所示:

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()