def plot_watertxt_parameter_comparison(watertxt_data, name1, name2, is_visible = False, save_path = None): """ Plot a comparison of two parameters contained in WATER.txt data file. Save plots to a particular path. Parameters ---------- watertxtdata_data : dictionary A dictionary containing data found in WATER data file. name1 : string String name of parameter name2 : string String name of parameter is_visible : bool Boolean value to show plots save_path : string String path to save plot(s) """ parameter1 = watertxt.get_parameter(watertxt_data, name = name1) parameter2 = watertxt.get_parameter(watertxt_data, name = name2) assert parameter1 is not None, "Parameter name {} is not in watertxt_data".format(name1) assert parameter2 is not None, "Parameter name {} is not in watertxt_data".format(name2) dates = watertxt_data["dates"] fig = plt.figure(figsize = (12,10)) ax1 = fig.add_subplot(211) ax1.grid(True) ax1.set_title("Parameter: {} vs {}".format(parameter1["name"], parameter2["name"])) ax1.set_xlabel("Date") ylabel = "\n".join(wrap(parameter1["name"], 60)) ax1.set_ylabel(ylabel) ax1.plot(dates, parameter1["data"], color = "b", label = parameter1["name"], linewidth = 2) ax1.hold(True) ax1.plot(dates, parameter2["data"], color = "r", label = parameter2["name"], linewidth = 2, alpha = 0.75) # increase y axis to have text and legend show up better curr_ylim = ax1.get_ylim() ax1.set_ylim((curr_ylim[0], curr_ylim[1] * 1.5)) # use a more precise date string for the x axis locations in the toolbar ax1.fmt_xdata = mdates.DateFormatter("%Y-%m-%d") # legend; make it transparent handles1, labels1 = ax1.get_legend_handles_labels() legend1 = ax1.legend(handles1, labels1, fancybox = True) legend1.get_frame().set_alpha(0.5) legend1.draggable(state=True) # show text of mean, max, min values on graph; use matplotlib.patch.Patch properies and bbox text = "mean = %.2f\nmax = %.2f\nmin = %.2f\n---\nmean = %.2f\nmax = %.2f\nmin = %.2f" % (parameter1["mean"], parameter1["max"], parameter1["min"], parameter2["mean"], parameter2["max"], parameter2["min"]) patch_properties = {"boxstyle": "round", "facecolor": "wheat", "alpha": 0.5 } ax1.text(0.05, 0.95, text, transform = ax1.transAxes, fontsize = 14, verticalalignment = "top", horizontalalignment = "left", bbox = patch_properties) # plot difference = values_b - values_a ax2 = fig.add_subplot(212, sharex = ax1) ax2.grid(True) ax2.set_title("Difference") ax2.set_xlabel("Date") ax2.set_ylabel(ylabel) diff = parameter2["data"] - parameter1["data"] ax2.plot(dates, diff, color = "k", linewidth = 2) # use a more precise date string for the x axis locations in the toolbar ax2.fmt_xdata = mdates.DateFormatter("%Y-%m-%d") # rotate and align the tick labels so they look better; note that ax2 will # have the dates, but ax1 will not. do not need to rotate each individual axis # because this method does it fig.autofmt_xdate() # save plots if save_path: # set the size of the figure to be saved curr_fig = plt.gcf() curr_fig.set_size_inches(12, 10) # split the parameter name to not include units because some units contain / character which Python interprets as an escape character filename = "-".join([watertxt_data1["user"], watertxt_data1["stationid"], "vs", watertxt_data2["user"], watertxt_data2["stationid"], parameter1["name"].split("(")[0].strip()]) + ".png" filepath = os.path.join(save_path, filename) plt.savefig(filepath, dpi = 100) # show plots if is_visible: plt.show() else: plt.close()
def plot_watertxt_parameter(watertxt_data, name, is_visible = False, save_path = None): """ Plot a parameter contained in WATER.txt data file. Save plots to a particular path. Parameters ---------- watertxtdata_data : dictionary A dictionary containing data found in WATER data file. name : string String name of parameter is_visible : bool Boolean value to show plots save_path : string String path to save plot(s) """ parameter = watertxt.get_parameter(watertxt_data, name = name) assert parameter is not None, "Parameter name {} is not in watertxt_data".format(name) dates = watertxt_data["dates"] fig = plt.figure(figsize = (12,10)) ax = fig.add_subplot(111) ax.grid(True) ax.set_title("Parameter: {}".format(parameter["name"])) ax.set_xlabel("Date") ylabel = "\n".join(wrap(parameter["name"], 60)) ax.set_ylabel(ylabel) # get proper color that corresponds to parameter name color_str = COLORS[name] # plot parameter ax.plot(dates, parameter["data"], color = color_str, label = parameter["name"], linewidth = 2) # rotate and align the tick labels so they look better fig.autofmt_xdate() # use a more precise date string for the x axis locations in the # toolbar ax.fmt_xdata = mdates.DateFormatter("%Y-%m-%d") # legend; make it transparent handles, labels = ax.get_legend_handles_labels() legend = ax.legend(handles, labels, fancybox = True) legend.get_frame().set_alpha(0.5) legend.draggable(state=True) # show text of mean, max, min values on graph; use matplotlib.patch.Patch properies and bbox text = "mean = %.2f\nmax = %.2f\nmin = %.2f" % (parameter["mean"], parameter["max"], parameter["min"]) patch_properties = {"boxstyle": "round", "facecolor": "wheat", "alpha": 0.5 } ax.text(0.05, 0.95, text, transform = ax.transAxes, fontsize = 14, verticalalignment = "top", horizontalalignment = "left", bbox = patch_properties) # save plots if save_path: # set the size of the figure to be saved curr_fig = plt.gcf() curr_fig.set_size_inches(12, 10) # split the parameter name to not include units because some units contain / character which Python interprets as an escape character filename = "-".join([watertxt_data["user"], watertxt_data["stationid"], parameter1["name"].split("(")[0].strip()]) + ".png" filepath = os.path.join(save_path, filename) plt.savefig(filepath, dpi = 100) # show plots if is_visible: plt.show() else: plt.close()