My Project
quickPlotting.py
Go to the documentation of this file.
1 import os
2 import sys
3 import numpy as np
4 from matplotlib import pyplot as plt
5 
6 import matplotlib as mpl
7 
8 mpl.rcParams["svg.fonttype"] = "none"
9 mpl.rcParams["font.size"] = 16
10 
11 mpl.rcParams["savefig.directory"] = "../../Figures"
12 
13 
14 mpl.rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
15 mpl.rc('text', usetex=True)
16 
17 import yaml
18 
19 try:
20  from yaml import CLoader as Loader, CDumper as Dumper
21 except ImportError:
22  from yaml import Loader, Dumper
23 
25  def __init__(self, run_name = "", numeric_method = "_midpoint_"):
26  self.loadFiles(run_name, numeric_method)
27  self.setUpPlot()
28 
29  def loadFiles(self, run_name, numeric_method):
30  self.results_path = os.path.dirname(os.path.realpath(__file__)) + "/../../../results/"
31 
32  results_folder = "/results/"
33 
34  stream = open(self.results_path + run_name + "/doc/yaml/parameters.yaml", 'r')
35  self.run_parameters = yaml.load(stream, Loader = Loader)
36 
37  shared_names = self.run_parameters["Parameters"]["file_name"]
38 
39  self.block_position = np.loadtxt(self.results_path + run_name + results_folder + shared_names + numeric_method + "block_position.csv", delimiter=",")
40  #print(len(self.block_position), len(self.block_position[0]))
41  self.block_velocity = np.loadtxt(self.results_path + run_name + results_folder + shared_names + numeric_method + "block_velocity.csv", delimiter=",")
42 
43  self.pad_position = np.loadtxt(self.results_path + run_name + results_folder + shared_names + numeric_method + "pad_position.csv", delimiter = ',')
44  self.pad_velocity = np.loadtxt(self.results_path + run_name + results_folder + shared_names + numeric_method + "pad_velocity.csv", delimiter = ',')
45 
46  def setUpPlot(self):
47  self.one_block_boolean = False
48 
49  start = 0
50  stop = self.run_parameters["Parameters"]["max_time"]
51 
52  if(len(self.block_position.shape) == 1):
53  self.one_block_boolean = True
54  self.x = np.linspace(start = start, stop = stop, num = len(self.block_position))
55  else:
56  self.x = np.linspace(start = start, stop = stop, num = len(self.block_position[0]))
57 
58  def example_function(self):
59  sigma = 1.0
60  mu = 0
61  return( (1.0/np.sqrt(2*np.pi*np.square(sigma))) * np.exp(-np.square(self.x - mu)/(2*np.square(sigma))) )
62 
63  def example_plot(self):
64  self.y = self.example_function()
65  fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, sharex=True)
66 
67  if(not self.one_block_boolean):
68  for i in range(0, len(self.block_position)):
69  ax1.plot(self.x, self.block_position[i], label = "Block: " + str(i))
70  ax2.plot(self.x, self.block_velocity[i], label = "Velocity: " + str(i))
71  else:
72  ax1.plot(self.x, self.block_position, label = "Block: " + "1")
73  ax2.plot(self.x, self.block_velocity, label = "Velocity: " + "1")
74  ax3.plot(self.x, self.pad_position)
75  ax4.plot(self.x, self.pad_velocity)
76  #plt.title("Example plot", fontdict={'fontname': 'Times New Roman', 'fontsize': 21}, y=1.03)
77  #plt.ylabel("$y$")
78  plt.xlabel("$t$")
79  #plt.legend()
80  plt.show()
81 
83  if(not self.one_block_boolean):
84  for i in range(0, len(self.block_position)):
85  plt.plot(self.x, self.block_position[i] - self.block_velocity[i], label = "Block: " + str(i))
86  else:
87  plt.plot(self.x, self.block_position, label = "Block: " + "1")
88 
89  #plt.title("Position plot of blocks", fontdict={'fontname': 'Times New Roman', 'fontsize': 21}, y=1.03)
90  plt.ylabel("$y$")
91  plt.xlabel("$t$")
92  plt.legend()
93  plt.show()
94 
95  def plot_pad_position(self):
96  plt.plot(self.x, self.pad_position, label = "Pad position")
97 
98  plt.ylabel("$y$")
99  plt.xlabel("$t$")
100  plt.legend()
101  plt.show()
102 
104  if(not self.one_block_boolean):
105  for i in range(0, len(self.block_position)):
106  plt.plot(self.block_position[i], self.block_velocity[i], label = "Block: " + str(i))
107  else:
108  plt.plot(self.block_position, self.block_velocity, label = "Block: " + "1")
109 
110  plt.show()
111 
112 
113 def plotastic():
114  if(len(sys.argv) > 2):
115  example = Plot_results(sys.argv[1], sys.argv[2])
116  elif(len(sys.argv) == 2):
117  example = Plot_results(sys.argv[1])
118  else:
119  example = Plot_results()
120  example.example_plot()
121  example.plot_blocks_position()
122  example.plot_pad_position()
123  example.plot_blocks_position_vers_velocity()
124 
125 if __name__ == "__main__":
126  plotastic()
def __init__(self, run_name="", numeric_method="_midpoint_")
def loadFiles(self, run_name, numeric_method)
def plot_blocks_position_vers_velocity(self)