Skip to content

Instantly share code, notes, and snippets.

@countrymarmot
Created August 16, 2014 07:46
Show Gist options
  • Save countrymarmot/7ee78d80126dffc8ad9b to your computer and use it in GitHub Desktop.
Save countrymarmot/7ee78d80126dffc8ad9b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
import os
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d
from matplotlib import cm
import time
class DrawPlot:
def __init__(self, path2save):
self.picture_number = 0
if not os.path.exists(path2save):
os.makedirs(path2save)
self.path = path2save
def draw_hist(self, data_list, data_name):
self.picture_number += 1
y = np.array(data_list, dtype=float)
y = y.ravel()
# Create a new figure
plt.figure(self.picture_number, figsize=(8, 4))
plt.hist(y, bins=50, range=None, normed=False)
# Add text description
text = ("Min:" + str(y.min()) + "\nMax:" +
str(y.max()) + "\nMean:" + str(y.mean()))
plt.figtext(0.2, 0.7, text)
plt.title(data_name + " Histogram Plot") # title of figure
plt.xlabel('value') # Label of x-axis
plt.ylabel('count') # Label of y-axis
plt.grid(True) # Turn the grid on
plt.savefig(self.path + data_name + '_hist.pdf')
plt.close()
def draw_runchart(self, data_list, data_name):
self.picture_number += 1
# 2D Matrix, duts * sensors
data_array = np.array(data_list, dtype=int)
# caculate stdev of data, output 1D Matrix, rows + columns
std_data = np.std(data_array, axis=0)
x = np.arange(len(std_data))
y = std_data
plt.figure(self.picture_number, figsize=(8, 4))
plt.plot(x, y, 'ro')
plt.title(data_name + " StdDev Plot")
plt.xlabel("sensor")
plt.ylabel("value")
plt.grid(True)
plt.savefig(self.path + data_name + '_stdDev.pdf')
plt.close()
def draw_3Dplot(self, data_list, data_name):
self.picture_number += 1
# 3D Matrix, duts * rows * columns
data_array = np.array(data_list, dtype=int)
# caculate stdev of duts, output 2D Matrix, rows * columns
std_data = np.std(data_array, axis=0)
x_arange, y_arange = std_data.shape
x, y = np.mgrid[:x_arange, :y_arange]
plt.figure(self.picture_number, figsize=(8, 4))
plt.title(data_name + " StdDev Surface")
# 111 means 1 row 1 column and the No.1 subplot
ax = plt.subplot(111, projection='3d')
ax.plot_surface(x, y, std_data, rstride=1, cstride=1, cmap=cm.coolwarm)
ax.set_xlabel("sensor_x")
ax.set_ylabel("sensor_y")
ax.set_zlabel(data_name + " StdDev")
plt.savefig(self.path + data_name + '_stdDev_3D.pdf')
plt.close()
if __name__ == "__main__":
start_time = time.time()
draw_plot = DrawPlot("./")
draw_plot.draw_hist(np.random.rand(100), "random 100")
draw_plot.draw_runchart(np.random.rand(2, 100), "random 100")
draw_plot.draw_3Dplot(np.random.rand(3, 3, 100), "random 100")
elasped_time = time.time() - start_time
print("Succeed. Elapsed Time: {0}".format(elasped_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment