Created
July 21, 2017 05:49
-
-
Save sirmo/4126a9450a769a3cc9d011beb6a7eded to your computer and use it in GitHub Desktop.
Logging with multiple Y axis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Copyright (c) 2017 Muxr, http://www.eevblog.com/forum/profile/?u=105823 | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE | |
""" | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
import numpy as np | |
import time as std_time | |
from scipy.interpolate import spline | |
import matplotlib.ticker as plticker | |
from matplotlib.ticker import FormatStrFormatter | |
import matplotlib | |
from mpl_toolkits.axes_grid1 import host_subplot | |
import mpl_toolkits.axisartist as AA | |
import argparse | |
import sys | |
#COLORS = ["#6e3c82", "#f70c43", "#e74c3c", "#3498db", "#95a5a6", "#34495e", "#2ecc71"] | |
COLORS = ["#6e3c82", "#e74c3c", "#3498db", "#95a5a6", "#34495e", "#2ecc71"] | |
def format_time(ts): | |
res = [] | |
for each in ts: | |
res.append(std_time.strftime("%H:%M.%S", std_time.localtime(np.asscalar(np.int32(each))))) | |
return res | |
def get_date_range(df): | |
max_time = df.timestamp.max() | |
min_time = df.timestamp.min() | |
t_to = std_time.strftime("%d-%b-%Y", std_time.localtime(np.asscalar(np.int32(max_time)))) | |
t_from = std_time.strftime("%d-%b-%Y", std_time.localtime(np.asscalar(np.int32(min_time)))) | |
if t_to == t_from: | |
return t_to | |
return "{} - {}".format(t_from, t_to) | |
def time_delta(df): | |
start = df.timestamp.min() | |
stop = df.timestamp.max() | |
d = divmod(stop-start, 86400) # days | |
h = divmod(d[1], 3600) # hours | |
m = divmod(h[1], 60) # minutes | |
s = m[1] # seconds | |
return '{:.0f}d {:02.0f}:{:02.0f}.{:02.0f}'.format(d[0], h[0], m[0], int(s)) | |
def yscale(df_obj): | |
minimum = df_obj.min() | |
maximum = df_obj.max() | |
margin = (maximum - minimum) / 6 | |
return minimum - margin, maximum + margin | |
def yscale_by(df_obj, by): | |
by = by / 2 | |
mean = df_obj.mean() | |
return mean - by, mean + by | |
def get_ppm(df_obj): | |
mean = df_obj.mean() | |
p2p = df_obj.max() - df_obj.min() | |
ppm = 1000000 / mean | |
ppm = p2p * ppm | |
return round(ppm, 1) | |
def get_tempco(value_obj, temp_obj): | |
temp_p2p = temp_obj.max() - temp_obj.min() | |
value_ppm = get_ppm(value_obj) | |
return round(value_ppm / temp_p2p, 1) | |
def get_ppm_std(df_obj): | |
mean = df_obj.mean() | |
std = df_obj.std() | |
ppm = 1000000 / mean | |
ppm = std * ppm | |
return round(ppm, 1) | |
def set_spine_color(axis, color): | |
for child in axis.get_children(): | |
print child | |
if isinstance(child, matplotlib.spines.Spine): | |
child.set_color(color) | |
def plot(options): | |
#sns.set(style="darkgrid") | |
sns.set_style("ticks", {"xtick.major.size": 8, "ytick.major.size": 8}) | |
sns.set_palette(COLORS) | |
df = pd.read_csv(options.infile, delimiter=';') | |
# Apply a rolling average filter if requested via cmdline options. | |
if options.avg_window: | |
window_len = options.avg_window | |
df.value = df.value.rolling(window=window_len).mean() | |
# Until the window fills up, the output will be a bunch of NaN values, | |
# which we remove here: | |
#avg_df = avg_df[window_len-1:] | |
#df = avg_df | |
if options.avg_overlay: | |
window_len = df.timestamp.count() / options.avg_overlay | |
avg_df = df.rolling(window=window_len).mean() | |
# handling multiple Y axis | |
host = host_subplot(111, axes_class=AA.Axes) | |
plt.subplots_adjust(right=0.82) | |
plt.grid(True) | |
plt.grid(color=COLORS[0], linestyle='dashed') | |
plt.grid(alpha=0.2, linewidth=0.4) | |
par1 = host.twinx() | |
par2 = host.twinx() | |
par3 = host.twinx() | |
offset = 50 | |
new_fixed_axis = par2.get_grid_helper().new_fixed_axis | |
par2.axis["right"] = new_fixed_axis(loc="right", | |
axes=par2, | |
offset=(offset, 0)) | |
par2.axis["right"].toggle(all=True) | |
par3.axis["right"] = new_fixed_axis(loc="right", | |
axes=par3, | |
offset=(offset*2, 0)) | |
par3.axis["right"].toggle(all=True) | |
#host.set_xlim(0, 2) | |
#host.set_ylim(0, 2) | |
#par1.set_ylim(25, 26) | |
host.set_xlabel("Time") | |
host.set_ylabel("DMM Value") | |
par1.set_ylabel("Temperature") | |
par2.set_ylabel("Humidity") | |
par3.set_ylabel("Pressure") | |
p1, = host.plot(df['timestamp'], df['value'], label="Value", linewidth=0.6 + options.bold, zorder=20) | |
if options.avg_overlay: | |
p1a, = host.plot(avg_df['timestamp'], avg_df['value'], label="Mov. Avg.", linewidth=0.6 + options.bold, zorder=30) | |
p2, = par1.plot(df['timestamp'], df['temp'], label="Temperature", linewidth=0.5 + options.bold, zorder=10, alpha=0.8) | |
p3, = par2.plot(df['timestamp'], df['humidity'], label="Humidity", linewidth=0.3 + options.bold, zorder=5, alpha=0.5, markeredgecolor='r') | |
p4, = par3.plot(df['timestamp'], df['pressure'], label="Pressure", linewidth=0.1 + options.bold, zorder=2, alpha=0.2) | |
# adjust the scale of secondary values so they are not hitting edge to edge | |
host.set_xlim(df.timestamp.min(), df.timestamp.max()) | |
if not options.yscale == 0: | |
host.set_ylim(yscale_by(df.value, options.yscale)) | |
avg_w_cap = None | |
if options.avg_window: | |
avg_w_cap = ', moving average: {}pts'.format(options.avg_window) | |
host.set_ylabel("DMM Value (scale adjusted to: {:.06f} p-p{})".format(options.yscale, | |
avg_w_cap)) | |
par1.set_ylim(yscale(df.temp)) | |
par2.set_ylim(yscale(df.humidity)) | |
par3.set_ylim(yscale(df.pressure)) | |
lgnd = host.legend(frameon=True) | |
lgnd.get_frame().set_linewidth(0.6) | |
host.axis["left"].label.set_color(p1.get_color()) | |
par1.axis["right"].label.set_color(p2.get_color()) | |
par2.axis["right"].label.set_color(p3.get_color()) | |
par3.axis["right"].label.set_color(p4.get_color()) | |
# par1.spines['right'].set_color(p2.get_color()) | |
# par2.spines['right'].set_color(p3.get_color()) | |
# par3.spines['right'].set_color(p4.get_color()) | |
#par2.yaxis.set_label_coords(-0.1, 2.02) | |
par1.tick_params(axis='y', color=p2.get_color()) | |
par2.tick_params(axis='y', color=p3.get_color()) | |
par3.tick_params(axis='y', color=p4.get_color()) | |
# print dir(par2) | |
# for tick in par2.get_ybound(): | |
# tick.set_color('red') | |
# for tl in par2.get_yticklabels(): | |
# print tl | |
# tl.set_color(p3.get_color()) | |
#print dir(par1.axis['right']) | |
#print par1.axis['right'].get_sketch_params() | |
#par1.axis['right'].set_visible(False) | |
plt.locator_params(axis='y', nticks=20) | |
#plot = host.plot(figsize=(23, 11), linewidth=0.3) | |
# set labels for X and Y axis | |
n = len(host.xaxis.get_ticklabels()) | |
evened_out_ts = np.linspace(df.timestamp.min(), df.timestamp.max(), n) | |
host.set_xticklabels(format_time(evened_out_ts), rotation=-15) | |
ny = len(host.yaxis.get_ticklabels()) | |
host.set_yticklabels(np.linspace(df.value.min(), df.value.max(), ny)) | |
host.yaxis.set_major_formatter(FormatStrFormatter('%.{}f'.format(options.ydigits))) | |
par3.yaxis.set_major_formatter(FormatStrFormatter('%.1f')) | |
# ax1 = fig.add_subplot(111) | |
# ax2 = ax1.twinx() | |
# ax2.plot(df.timestamp, df.temp, COLORS[1]) | |
# ny2 = len(ax2.yaxis.get_ticklabels()) | |
# print ny2 | |
# ax2.set_yticklabels(np.linspace(df.temp.min(), df.temp.max(), ny2)) | |
# plot3 = plot.twinx() | |
# plot3.set_yticklabels(np.linspace(df.pressure.min(), df.pressure.max(), ny)) | |
# | |
# plot4 = plot.twinx() | |
# plot4.set_yticklabels(np.linspace(df.humidity.min(), df.humidity.max(), ny)) | |
# TODO add minor ticks | |
# plot.yaxis.set_tick_params(which='minor', right='off') | |
# | |
# plot the trend line | |
z = np.polyfit(df.timestamp, df.value, 1) | |
p = np.poly1d(z) | |
plt.plot(df.timestamp, p(df.timestamp), "r--", color=COLORS[0], linewidth=0.8) | |
#fig = plt.figure(figsize=(30, 20)) | |
fig = host.get_figure() | |
fig.set_size_inches(26, 11, forward=True) | |
# | |
# add some captions | |
title = '{} ({})'.format(options.title, get_date_range(df)) | |
fig.text(0.40, 0.90, title, fontsize=13, fontweight='bold', color=COLORS[0]) | |
print title | |
height = 0.295 | |
x_pos = 0.900 | |
spacing = 0.020 | |
mean = 'meanT: {} (p-p:{}) C'.format(round(df.temp.mean(), 2), round((df.temp.max() - df.temp.min()), 2)) | |
fig.text(x_pos, height, mean, fontsize=12, color=COLORS[1]) | |
height -= spacing | |
print mean | |
mean = 'meanH: {}'.format(round(df.humidity.mean(), 2)) | |
fig.text(x_pos, height, mean, fontsize=12, color=COLORS[2]) | |
height -= spacing | |
print mean | |
mean = 'meanP: {}'.format(round(df.pressure.mean(), 2)) | |
fig.text(x_pos, height, mean, fontsize=12, color=COLORS[3]) | |
height -= spacing | |
print mean | |
value_max = 'max: {}'.format(df.value.max()) | |
fig.text(x_pos, height, value_max, fontsize=12, color=COLORS[0]) | |
height -= spacing | |
print value_max | |
value_min = 'min: {}'.format(df.value.min()) | |
fig.text(x_pos, height, value_min, fontsize=12, color=COLORS[0]) | |
height -= spacing | |
print value_min | |
value_p2p = 'p-p: {:.08f} ({}ppm)'.format(float(df.value.max() - df.value.min()), get_ppm(df.value)) | |
fig.text(x_pos, height, value_p2p, fontsize=12, color=COLORS[0]) | |
height -= spacing | |
print value_p2p | |
value_std_dev = 'o: {} ({}ppm)'.format(round(df.value.std(), 9), get_ppm_std(df.value)) | |
fig.text(x_pos, height, value_std_dev, fontsize=12, color=COLORS[0]) | |
height -= spacing | |
print value_std_dev | |
value_tempco_dev = 'tempco: {} ppm/C'.format(get_tempco(df.value, df.temp)) | |
fig.text(x_pos, height, value_tempco_dev, fontsize=12, color=COLORS[0]) | |
height -= spacing | |
print value_tempco_dev | |
count = 'samples: {}'.format(df.value.count()) | |
fig.text(x_pos, height, count, fontsize=12, color=COLORS[0]) | |
height -= spacing | |
print count | |
value_duration = 'duration: {}'.format(time_delta(df)) | |
fig.text(x_pos, height, value_duration, fontsize=12, color=COLORS[0]) | |
height -= spacing | |
print value_duration | |
mean = 'mean: {}'.format(round(df.value.mean(), 9)) | |
fig.text(x_pos, height, mean, fontsize=13, fontweight='bold', color=COLORS[0]) | |
height -= spacing | |
print mean | |
fig.savefig(options.outfile, bbox_inches='tight') | |
def main(): | |
print 'mplot' | |
parser = argparse.ArgumentParser() | |
parser.add_argument('infile', nargs='?') | |
parser.add_argument('outfile', nargs='?') | |
parser.add_argument('-t', | |
'--title', | |
dest='title', | |
action='store', | |
help='title to be used in the chart') | |
parser.add_argument('-y', | |
'--ydigits', | |
dest='ydigits', | |
action='store', | |
default="7", | |
help='Number of least significant digits in the Y labels') | |
parser.add_argument('-a', | |
'--adjustyscale', | |
dest='yscale', | |
action='store', | |
default=0, | |
type=float, | |
help='Default is 0 which means auto, but you can use a p-p from another graph') | |
parser.add_argument('-r', | |
'--rolling-average-window', | |
dest='avg_window', | |
type=int, | |
action='store', | |
help='Apply a rolling-average with a window of N data points') | |
parser.add_argument('-o', | |
'--average_overlay', | |
dest='avg_overlay', | |
type=int, | |
action='store', | |
help='Overlay the rolling average on top of the Value') | |
parser.add_argument('-b', | |
'--bold', | |
dest='bold', | |
action='store', | |
default=0, | |
type=float, | |
help='Increase boldness of traces') | |
options = parser.parse_args() | |
if options.infile is None: | |
print "use -h for help" | |
sys.exit(-1) | |
if options.outfile is None: | |
extensionless = options.infile.split('.')[0] | |
options.outfile = extensionless + '.png' | |
if options.title is None: | |
options.title = options.infile | |
plot(options) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment