comparison galaxy_micropita/src/breadcrumbs/src/PlotMatrix.py @ 3:8fb4630ab314 draft default tip

Uploaded
author sagun98
date Thu, 03 Jun 2021 17:07:36 +0000
parents
children
comparison
equal deleted inserted replaced
2:1c5736dc85ab 3:8fb4630ab314
1 """
2 Author: Timothy Tickle
3 Description: Plots matrices.
4 """
5
6 #####################################################################################
7 #Copyright (C) <2012>
8 #
9 #Permission is hereby granted, free of charge, to any person obtaining a copy of
10 #this software and associated documentation files (the "Software"), to deal in the
11 #Software without restriction, including without limitation the rights to use, copy,
12 #modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
13 #and to permit persons to whom the Software is furnished to do so, subject to
14 #the following conditions:
15 #
16 #The above copyright notice and this permission notice shall be included in all copies
17 #or substantial portions of the Software.
18 #
19 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
20 #INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
21 #PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #####################################################################################
26
27 __author__ = "Timothy Tickle"
28 __copyright__ = "Copyright 2012"
29 __credits__ = ["Timothy Tickle"]
30 __license__ = "MIT"
31 __maintainer__ = "Timothy Tickle"
32 __email__ = "ttickle@sph.harvard.edu"
33 __status__ = "Development"
34
35 #External libraries
36 import matplotlib.pyplot as plt
37 import numpy as np
38 from pylab import *
39
40 #Plots a matrix
41 class PlotMatrix:
42
43 #Given a matrix and labels consistent to the matrix, plot a matrix
44 @staticmethod
45 def funcPlotMatrix(npMatrix, lsXLabels, strOutputFigurePath, strXTitle="X Axis", strYTitle="Y Axis", fFlipYLabels=False):
46 """
47 Given a matrix and labels consistent to the matrix, plot a matrix.
48
49 :param npMatrix: Numpy Array (matrix) to plot.
50 :type: Numpy Array
51 :param lsXLabels: X Labels
52 :type: List of strings
53 :param strOutputFigurePath: File to create the figure file.
54 :type: String
55 :param strXTitle: X Axis label.
56 :type: String
57 :param strYTitle: Y axis label.
58 :type: String
59 :param fFlipYLabels: Flip the Y labels so they are opposite order of x axis.
60 :type: Boolean
61 """
62
63 #Get canvas/figure
64 plt.clf()
65 figConfusionMatrix = plt.figure()
66 objAxis = figConfusionMatrix.add_subplot(111)
67
68 #Get y labels
69 lNewYLabels = list(lsXLabels)
70 if fFlipYLabels:
71 lNewYLabels.reverse()
72
73 #Set x axis and position
74 objAxis.xaxis.set_ticklabels([""]+lsXLabels)
75 objAxis.xaxis.set_ticks_position('top')
76
77 #Set y axis
78 objAxis.yaxis.set_ticklabels([""]+lNewYLabels)
79
80 #Set axis titles
81 ylabel(strYTitle)
82 plt.suptitle(strXTitle)
83
84 #Plot matrix values
85 objPlot = objAxis.imshow(np.array(npMatrix), cmap=get_cmap("Blues"), interpolation='nearest')
86
87 #Plot text values
88 for yIndex, ldRow in enumerate(npMatrix):
89 for xIndex, dValue in enumerate(ldRow):
90 plt.text(xIndex, yIndex, dValue, fontdict = {'size':18,'weight':'bold'} )
91
92 #Add color bar
93 figConfusionMatrix.colorbar(objPlot, ticks=range(int(min(np.array(npMatrix).ravel())),int(max(np.array(npMatrix).ravel()))))
94
95 #Save to a file
96 savefig(strOutputFigurePath)