Skip to content

Commit

Permalink
fix xticks when using dates (#59)
Browse files Browse the repository at this point in the history
* fix xticks when using dates

* remove unnecessary prints

* Update climate-stripes.xml

* fix lint issues

* time column may not be called 'time' but Month, etc.

* add option nxsplit

* replace image when using dates
  • Loading branch information
Anne Fouilloux authored Jul 9, 2022
1 parent da614bb commit e4c1448
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
11 changes: 8 additions & 3 deletions tools/climate-stripes/climate-stripes.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<tool id="climate_stripes" name="climate stripes" version="1.0.1">
<tool id="climate_stripes" name="climate stripes" version="1.0.2">
<description>from timeseries</description>
<edam_topics>
<edam_topic>topic_3855</edam_topic>
Expand All @@ -9,8 +9,8 @@
</edam_operations>
<requirements>
<requirement type="package" version="3">python</requirement>
<requirement type="package" version="3.1.1">matplotlib</requirement>
<requirement type="package" version="1.0.0">pandas</requirement>
<requirement type="package" version="3.5.2">matplotlib</requirement>
<requirement type="package" version="1.4.3">pandas</requirement>
</requirements>
<command detect_errors="exit_code"><![CDATA[
python3 '$__tool_directory__/climate_stripes.py'
Expand All @@ -28,13 +28,17 @@
#if str($adv.format_plot).strip()
--format_plot '$adv.format_plot'
#end if
#if $adv.nxsplit
--nxsplit '$adv.nxsplit'
#end if
--output image.png
]]></command>
<inputs>
<param name="ifilename" type="data" format="tabular" label="timeseries to plot"></param>
<param name="variable" type="text" value="global" label="column name to use for plotting" />
<param name="title" type="text" value="" label="plot title" />
<section name="adv" title="Advanced Options" expanded="false">
<param name="nxsplit" type="integer" optional="true" label="number of values per intervals" />
<param name="xname" type="text" value="" label="column name to use for x-axis" />
<param name="format_date" type="text" value="" label="format for input date/time column" />
<param name="format_plot" type="text" value="" label="format for plotting dates on the x-axis" />
Expand Down Expand Up @@ -134,6 +138,7 @@
<param name="variable" value="global" />
<param name="title" value="Surface Temperature stripes (1979-2019)" />
<param name="colormap" value="OrRd" />
<param name="nxsplit" value="50" />
<param name="xname" value="Month" />
<param name="format_date" value="XYXm" />
<param name="format_plot" value="XY" />
Expand Down
15 changes: 7 additions & 8 deletions tools/climate-stripes/climate_stripes.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def __init__(self, input, valname, cmap, output, xname="",
self.plot_format = plot_format.replace('X', '%')

def read_data(self):
self.data = pd.read_csv(self.input, sep='\t')
if self.xname is not None:
self.data = pd.read_csv(self.input, sep='\t', index_col=self.xname, infer_datetime_format=True)
else:
self.data = pd.read_csv(self.input, sep='\t')

def create_stripes(self):
data = np.zeros((2, self.data[self.valname].shape[0]), dtype='float')
Expand All @@ -86,15 +89,11 @@ def create_stripes(self):
vmax=self.data[self.valname].quantile(q=0.99))
if self.title:
plt.title(self.title)
if self.xname:
if self.xname is not None:
nrange = self.data.index.values
n = int(np.floor((nrange.max() - nrange.min()) / int(self.nxsplit)))
date_list = self.data[self.xname].loc[::n].apply(
lambda x: pd.to_datetime(str(x), format=self.format))
date_list = pd.to_datetime(nrange[::int(self.nxsplit)], format=self.format)
date_list = [i.strftime(self.plot_format) for i in date_list]
nval = int(self.data[self.xname].loc[::n].shape[0])
ax.xaxis.set_major_locator(plt.MaxNLocator(nval))
ax.xaxis.set_ticklabels(date_list)
ax.set_xticks(np.arange(0, len(nrange), int(self.nxsplit)), date_list)
ax.xaxis.set_tick_params(rotation=45)
else:
ax.set_xticks([])
Expand Down
Binary file modified tools/climate-stripes/test-data/T2Mstripes_with_axis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e4c1448

Please sign in to comment.