Précédent   Forum du club des développeurs et IT Pro > Autres langages > Python & Zope > Calcul scientifique
Calcul scientifique Forum d'entraide sur la programmation scientifique et bibliothèques associées (PIL, NumPy, SciPy, ...)
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 31/05/2012, 18h54   #1
Drexler82
Invité de passage
 
Homme
Inscription : mai 2011
Messages : 13
Détails du profil
Informations personnelles :
Sexe : Homme

Informations forums :
Inscription : mai 2011
Messages : 13
Points : 3
Points : 3
Par défaut Une légende commune à plusieurs subplots avec matplotlib

Bonjour à tous,

Je me tourne vers vous après m'être cassé les dents quelques temps sur ce problème. Je souaite créer plusieurs camemberts (ou "pies" dans matplotlib) à partir de données stockées dans un tableau. j'arrive parfaitement à les organiser grâce à la fonction "subplot". Ce qui fait que mes camemberts sont
J'ai défini dans un dictionnaire les noms de mes éléments de mes pies ainsi que leurs couleurs associées. Ainsi, quelque soit mes valeurs retrouvées, les mêmes couleurs sont assoiées aux mêmes éléments de chaque "pie".

Bref, tout ce ceci fonctionne, sauf que je n'arrive pas à avoir une légende commune à tous mes "pies". En effet, certains éléments sont présents dans le pie1, mais pas dans les autres. Et si j'utilise la fonction legend(), il me crée une légende pour chaque pie, ce que je ne souhaite pas.

Quelqu'un a-t-il déjà rencontré ce problème et ce questionnement ?? Merci d'avance.

PS : je suis assez mauvais en python, je m'y connais plus en Perl, mais j'aime bien cette librairie matplotlib, elle fonctionne bien ^^

Voilà mon code :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
 
#-------------------------------------------------------------------------------------------------------
#Step to create phyla pies based on data given by the main program
#-------------------------------------------------------------------------------------------------------
i = 0
j=0
taxo_highest = list()
taxo = list()
my_samples = list()
 
my_pie_name = ""
my_names_for_pie = list()
my_values_for_pie = list()
my_colors = {'Acidobacteria':'Gold', 'Actinobacteria':'Darkorange', 'Aquificae':'Cyan',\
    'Armatimonadetes':'OldLace', 'BRC1':'Tan', 'OD1':'RosyBrown', 'OP11':'Peru', 'SR1':'SaddleBrown',\
    'TM7':'Sienna', 'WS3':'Wheat', 'Chrysiogenetes':'BurlyWood', 'Caldiserica':'AntiqueWhite', 'Bacteroidetes':'CornflowerBlue',\
    'Chlamydiae':'White', 'Chloroflexi':'DarkGoldenRod', 'Crenarchaeota':'DarkGreen', 'Euryarchaeota':'DarkSeaGreen',\
    'Nanoarchaeota':'GreenYellow', 'Korarchaeota':'LightGreen', 'Cyanobacteria/Chloroplast':'GreenYellow', 'Chlorobi':'Chartreuse',\
    'Deferribacteres':'LighSeaGreen', 'Deinococcus-Thermus':'YellowGreen', 'Dictyoglomi':'Chocolate', 'Elusimicrobia':'DarkKhaki',\
    'Fibrobacteres':'MediumVioletRed', 'Firmicutes':'FireBrick', 'Fusobacteria':'Thistle', 'Gemmatimonadetes':'DeepSkyBlue',\
    'Lentisphaerae':'Moccasin', 'Nitrospira':'LightSalmon', 'Planctomycetes':'ForestGreen', 'Proteobacteria':'Blue',\
    'Thermotogae':'PaleGoldenRod', 'Verrucomicrobia':'Purple', 'Spirochaetes':'PowderBlue', 'Synergistetes':'Plum',\
    'Tenericutes':'Turquoise', 'Thermodesulfobacteria':'PeechPuff', 'Unknown':'Gray', 'Undefined':'Black', 'Low Groups':'LightGrey'}
 
 
#Loop to treat all files found in the Summary_files folder
for file_name in treated_files:
    #If the name of the file contain Taxonomy_results, we analyze this file
    if (0<=file_name.find("Taxonomy_results_")):
        #Open the targeted taxonomy summary file
        Taxo_file = open("../Summary_files/"+file_name, 'r')
        #For each read line of the file (as the line is not empty)
        Read_tot_lines = Taxo_file.readlines()
        #We read each line found in the file
        for i in range(len(Read_tot_lines)):
            #We delete the \n character using the replace function
            Read_tot_lines[i] = Read_tot_lines[i].replace("\n","")
            #If we found the line corresponding to the taxonomy_highest (phylum)
            if (0<=Read_tot_lines[i].find("Taxonomy_highest")):
                #We store it in an array form in taxo_highest
                taxo_highest = Read_tot_lines[i].split("\t")
            #If we found the line corresponding to the taxonomy studied
            elif (0<=Read_tot_lines[i].find("Taxonomy_phylum")):
                #We store it in an array form in taxo
                taxo = Read_tot_lines[i].split("\t")
            #If not, it correspond to a line of a sample
            else:
                #We store the array in an array names my_samples
                my_samples.append(Read_tot_lines[i].split("\t"))
 
 
        #We then determine the number of samples to create a figure with the good number of columns
        x = len(my_samples) - 1
        if( (x%2) == 0):
            Nbcols = 2
            #We define also the number of rows
            Nbrows = x/Nbcols
        else:
            Nbcols = 3
            #We define also the number of rows
            Nbrows = x/Nbcols + 1
 
        k = 1
        #Definition of the subplot distances
        plt.subplots_adjust(hspace=0.4)
 
 
        #For each line of the samples
        for my_line in my_samples:
            #We not treat empty lines
            if(my_line[0] == ''):
                continue
            my_line = compute_percents(my_line)
            #Needed empty variables to treat several lines
            my_names_for_pie = list()
            my_values_for_pie = list()
            my_colors_list = list()
            my_lowers_values = float()
            #We study each element in the line of the sample
            for j in range(len(my_line)):
                #We catch the name of the sample
                if (0<=str(my_line[j]).find("Taxo_")):
                    #We catch the simplified name of the sample
                    my_pie_name = found_file_name(my_line[j])
                #We avoid to treat the last element of the line
                elif my_line[j] == '':
                    continue
                #If it's an element
                else:
                    #We verify that at least one read was detected
                    if (float(my_line[j]) > 0):
                        if (float(my_line[j]) < 1):
                            my_lowers_values += float(my_line[j])
                        else:
                            #We catch the name of the detected taxonomic elements
                            my_names_for_pie.append(taxo[j])
                            my_colors_list.append(my_colors[taxo[j]])
                            #And also their corresponding values
                            my_values_for_pie.append(my_line[j])
 
            #Finally, we add the lowers in the same group for the graphics
            my_names_for_pie.append("Low Groups")
            #Thier corresponding color
            my_colors_list.append(my_colors['Low Groups'])
            #And also their corresponding value
            my_values_for_pie.append(my_lowers_values)            
 
            #We create the subplot for each plot
            plt.subplot(Nbrows,Nbcols,k)
            #We use these elements to store them
            plt.pie(my_values_for_pie, colors=my_colors_list, autopct='%1.1f%%', pctdistance=1.2, shadow=True)
            #We also define the font properties using the FontProperties from the class font_manager
            #This give an object properties that can directly been given to the legend object.
            prop = fm.FontProperties(size=7)
 
 
            plt.title(my_pie_name)
            plt.tight_layout()
            #We finally do this function to adapt the graphics to its size
            #plt.grid(True)
            k = k + 1
 
            plt.legend(my_names_for_pie, prop = prop)
        #We finally save the figure in various formats
        plt.savefig('../Summary_files/Taxo_pie_results.png')
        plt.savefig('../Summary_files/Taxo_pie_results.emf')
        plt.savefig('../Summary_files/Taxo_pie_results.pdf')
Fichiers attachés
Type de fichier : pdf Taxo_pie_results.pdf (32,6 Ko, 4 affichages)
Drexler82 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 21h32.


 
 
 
 
Partenaires

Hébergement Web