Как упорядочить гистограммы, используя matplotlib в порядке возрастания чисел? - PullRequest
0 голосов
/ 10 апреля 2020

Мне интересно, как выровнять гистограммы по оси X в порядке возрастания чисел в matplotlib. У меня есть 4 набора данных, которые представлены на гистограмме, но они сгруппированы в порядке, в котором я поместил их в свой сценарий. Есть ли способ разбить эту группировку и выровнять все столбцы в порядке возрастания?

Здесь приведен мой полный код, и я также разместил изображение, которое получаю, когда запускаю скрипт.

#usr/bin/env python3

import os
import xml.etree.ElementTree as ET  # Imports xml.etree.ElementTree which allows you to easily parse an XML file.import matplotlib.pyplot as plt
import matplotlib.pyplot as plt

path_to_concat_xml_dir = 'path/to/xml_dir/'
concat_xml_dir = os.listdir(path_to_concat_xml_dir) #this contains XML files from multiple directories. The files have not been concatenated.

first_hit_hsp_bit_score_list =[]
first_hit_hsp_e_value_list = []
second_hit_hsp_bit_score_list = []
second_hit_hsp_e_value_list = []
third_hit_hsp_bit_score_list = []
third_hit_hsp_e_value_list = []
fourth_hit_hsp_bit_score_list = []
fourth_hit_hsp_e_value_list = []

for file in concat_xml_dir:
    #print(file)
    if file.endswith(".xml"):
        xml_file_path = os.path.join(path_to_concat_xml_dir, file)
        #print(xml_file_path)
        xml_files = open(xml_file_path, 'r')
        for lines in xml_files:
            #print(lines)
            tree = ET.parse(xml_files)  # ElementTree is now parsing all of the XML tblastn files.
            root = tree.getroot()  # This defines the root for each of the XML files. Since all the files have an identical format the root of each file is 'BlastOutput'.
            for f_h_hsp_bit_score in root.findall('./BlastOutput_iterations/Iteration/Iteration_hits/Hit[1]/Hit_hsps/Hsp[1]/Hsp_bit-score'):
                print(f_h_hsp_bit_score.text)
                first_hit_hsp_bit_score_list.append(float(f_h_hsp_bit_score.text))
            for f_h_hsp_e_value in root.findall('./BlastOutput_iterations/Iteration/Iteration_hits/Hit[1]/Hit_hsps/Hsp[1]/Hsp_evalue'):
                print(f_h_hsp_e_value.text)
                first_hit_hsp_e_value_list.append(f_h_hsp_e_value.text)
            for s_h_hsp_bit_score in root.findall('./BlastOutput_iterations/Iteration/Iteration_hits/Hit[2]/Hit_hsps/Hsp[1]/Hsp_bit-score'):
                #print(s_h_hsp_bit_score.text)
                second_hit_hsp_bit_score_list.append(float(s_h_hsp_bit_score.text))
            for s_h_hsp_e_value in root.findall('./BlastOutput_iterations/Iteration/Iteration_hits/Hit[2]/Hit_hsps/Hsp[1]/Hsp_evalue'):
                #print(s_h_hsp_e_value.text)
                second_hit_hsp_e_value_list.append(s_h_hsp_e_value.text)
            for t_h_hsp_bit_score in root.findall('./BlastOutput_iterations/Iteration/Iteration_hits/Hit[3]/Hit_hsps/Hsp[1]/Hsp_bit-score'):
                #print(s_h_hsp_bit_score.text)
                third_hit_hsp_bit_score_list.append(float(t_h_hsp_bit_score.text))
            for t_h_hsp_e_value in root.findall('./BlastOutput_iterations/Iteration/Iteration_hits/Hit[3]/Hit_hsps/Hsp[1]/Hsp_evalue'):
                #print(s_h_hsp_e_value.text)
                third_hit_hsp_e_value_list.append(t_h_hsp_e_value.text)
            for fo_h_hsp_bit_score in root.findall('./BlastOutput_iterations/Iteration/Iteration_hits/Hit[4]/Hit_hsps/Hsp[1]/Hsp_bit-score'):
                #print(s_h_hsp_bit_score.text)
                fourth_hit_hsp_bit_score_list.append(float(fo_h_hsp_bit_score.text))
            for fo_h_hsp_e_value in root.findall('./BlastOutput_iterations/Iteration/Iteration_hits/Hit[4]/Hit_hsps/Hsp[1]/Hsp_evalue'):
                #print(s_h_hsp_e_value.text)
                fourth_hit_hsp_e_value_list.append(fo_h_hsp_e_value.text)

list1 = sorted(zip(*[first_hit_hsp_e_value_list,first_hit_hsp_bit_score_list]))
new_x, new_y = list(zip(*list1))
list2 = sorted(zip(*[second_hit_hsp_e_value_list,second_hit_hsp_bit_score_list]))
new_x2, new_y2 = list(zip(*list2))
list3 = sorted(zip(*[third_hit_hsp_e_value_list,third_hit_hsp_bit_score_list]))
new_x3, new_y3 = list(zip(*list3))
list4 = sorted(zip(*[fourth_hit_hsp_e_value_list,fourth_hit_hsp_bit_score_list]))
new_x4, new_y4 = list(zip(*list4))

plt.bar(new_x, new_y, label="First BLAST Hits n=8")
plt.bar(new_x2, new_y2, label="Second BLAST Hits n=8")
plt.bar(new_x3, new_y3, label="Third BLAST Hits n=8")
plt.bar(new_x4, new_y4, label="Fourth BLAST Hits n=8")
plt.xticks(rotation=90)
plt.legend(loc="upper right")
plt.title('tblastn E-Values and Associated Bit-Scores')
plt.xlabel('E-Value')
plt.ylabel('Bit-Score')
plt.show()

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...