Package lib :: Package plotting :: Module gnuplot
[hide private]
[frames] | no frames]

Source Code for Module lib.plotting.gnuplot

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2014 Edward d'Auvergne                                        # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """Module for data plotting using gnuplot.""" 
 24   
 25  # Python module imports. 
 26  from os import chmod 
 27  from stat import S_IRWXU, S_IRGRP, S_IROTH 
 28   
 29  # relax module imports. 
 30  from lib.io import file_root, get_file_path, open_write_file, swap_extension 
 31  from lib.plotting import text 
 32   
 33   
34 -def correlation_matrix(matrix=None, labels=None, file=None, dir=None, force=False):
35 """Gnuplot plotting function for representing correlation matrices. 36 37 @keyword matrix: The correlation matrix. This must be a square matrix. 38 @type matrix: numpy rank-2 array. 39 @keyword labels: The labels for each element of the matrix. The same label is assumed for each [i, i] pair in the matrix. 40 @type labels: list of str 41 @keyword file: The name of the file to create. 42 @type file: str 43 @keyword dir: The directory where the PDB file will be placed. If set to None, then the file will be placed in the current directory. 44 @type dir: str or None 45 """ 46 47 # The dimensions. 48 n = len(matrix) 49 50 # Generate the text file for loading into gnuplot. 51 text.correlation_matrix(matrix=matrix, labels=labels, file=file, dir=dir, force=force) 52 53 # The script file name with the extension swapped. 54 file_name = swap_extension(file=file, ext='gnu') 55 56 # Open the script file for writing. 57 output = open_write_file(file_name, dir=dir, force=force) 58 59 # Gnuplot script setup. 60 output.write("#!/usr/bin/env gnuplot\n\n") 61 62 63 # Set up the terminal type and make the plot square. 64 output.write("# Set up the terminal type and make the plot square.\n") 65 output.write("set terminal postscript eps size 10,10 enhanced color font 'Helvetica,20' linewidth 0.1\n") 66 output.write("set size square\n") 67 68 # The colour map. 69 output.write("\n# Blue-red colour map.\n") 70 output.write("set palette model RGB\n") 71 output.write("set palette defined\n") 72 73 # The labels. 74 if labels != None: 75 output.write("\n# Labels.\n") 76 for axis in ['x', 'y']: 77 output.write("set %stics out " % axis) 78 if axis == 'x': 79 output.write("rotate ") 80 output.write("font \",8\" (") 81 for i in range(n): 82 if i != 0: 83 output.write(", ") 84 output.write("\"%s\" %s" % (format_enhanced(labels[i]), i)) 85 output.write(")\n") 86 87 # Output to EPS. 88 output.write("\n# Output to EPS.\n") 89 output.write("set output \"%s.eps\"\n" % file_root(file)) 90 91 # Load and show the text data. 92 output.write("\n# Load and show the text data\n") 93 output.write("plot \"%s\" matrix with image\n" % file) 94 95 # Close the file. 96 output.close() 97 98 # Make the script executable. 99 chmod(get_file_path(file_name=file_name, dir=dir), S_IRWXU|S_IRGRP|S_IROTH)
100 101
102 -def format_enhanced(text):
103 """Convert and return the text to handle enhanced postscript. 104 105 @param text: The text to convert to enhanced mode. 106 @type text: str 107 @return: The formatted text for enhanced postscript mode. 108 @rtype: str 109 """ 110 111 # Handle the '@' character. 112 text = text.replace('@', '\\\\@') 113 114 # Return the text. 115 return text
116