From be11e60a4eb1cc79e5414180930edf25d78fb500 Mon Sep 17 00:00:00 2001 From: Kim Whitehall Date: Tue, 7 Apr 2015 06:51:35 -0700 Subject: [PATCH] CLIMATE-586 - add barchart as a plotter option - add draw_barchart function to plotter.py to plot basic (horizontal) barchart --- ocw/plotter.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/ocw/plotter.py b/ocw/plotter.py index b931c6cb..f40bf14d 100644 --- a/ocw/plotter.py +++ b/ocw/plotter.py @@ -373,7 +373,7 @@ def draw_time_series(results, times, labels, fname, fmt='png', gridshape=(1, 1), :param xlabel: (Optional) x-axis title. :type xlabel: :mod:`string` - :param ylabel: (Optional) y-ayis title. + :param ylabel: (Optional) y-axis title. :type ylabel: :mod:`string` :param ptitle: (Optional) plot title. @@ -488,6 +488,57 @@ def draw_time_series(results, times, labels, fname, fmt='png', gridshape=(1, 1), fig.savefig('%s.%s' %(fname, fmt), bbox_inches='tight', dpi=fig.dpi) fig.clf() +def draw_barchart(results, yvalues, fname, ptitle='', fmt='png', + xlabel='', ylabel=''): + ''' Draw a barchart. + + :param results: 1D array of data. + :type results: :class:`numpy.ndarray` + + :param yvalues: List of y-axis labels + :type times: :class:`list` + + :param fname: Filename of the plot. + :type fname: :mod:`string` + + :param ptitle: (Optional) plot title. + :type ptitle: :mod:`string` + + :param fmt: (Optional) filetype for the output. + :type fmt: :mod:`string` + + :param xlabel: (Optional) x-axis title. + :type xlabel: :mod:`string` + + :param ylabel: (Optional) y-axis title. + :type ylabel: :mod:`string` + + ''' + + y_pos = list(range(len(yvalues))) + fig = plt.figure() + fig.set_size_inches((11., 8.5)) + fig.dpi = 300 + ax = plt.subplot(111) + plt.barh(y_pos, results, align="center", height=0.8, linewidth=0) + plt.yticks(y_pos, yvalues) + plt.tick_params(axis="both", which="both", bottom="on", top="off",labelbottom="on", left="off", right="off", labelleft="on") + ax.spines["top"].set_visible(False) + ax.spines["right"].set_visible(False) + + ymin = min(y_pos) + ymax = max(y_pos) + ymin = min((ymin - ((ymax - ymin) * 0.1)/2),0.5) + ymax = ymax + ((ymax - ymin) * 0.1) + ax.set_ylim((ymin, ymax)) + plt.xlabel(xlabel) + plt.tight_layout() + + # Save the figure + fig.savefig('%s.%s' %(fname, fmt), bbox_inches='tight', dpi=fig.dpi) + fig.clf() + + def draw_contour_map(dataset, lats, lons, fname, fmt='png', gridshape=(1, 1), clabel='', ptitle='', subtitles=None, cmap=None, clevs=None, nlevs=10, parallels=None, meridians=None,