Auxiliary function to format time axis
(fig,ax,
plot_local_time,
local_time_offset,
timelimits
)
| 1865 | |
| 1866 | |
| 1867 | def _format_time_axis(fig,ax, |
| 1868 | plot_local_time, |
| 1869 | local_time_offset, |
| 1870 | timelimits |
| 1871 | ): |
| 1872 | """ |
| 1873 | Auxiliary function to format time axis |
| 1874 | """ |
| 1875 | ax[-1].xaxis_date() |
| 1876 | if timelimits is not None: |
| 1877 | timelimits = [pd.to_datetime(tlim) for tlim in timelimits] |
| 1878 | hour_interval = _determine_hourlocator_interval(ax[-1],timelimits) |
| 1879 | if plot_local_time is not False: |
| 1880 | if plot_local_time is True: |
| 1881 | localtimefmt = '%I %p' |
| 1882 | else: |
| 1883 | assert isinstance(plot_local_time,str), 'Unexpected plot_local_time format' |
| 1884 | localtimefmt = plot_local_time |
| 1885 | # Format first axis (local time) |
| 1886 | ax[-1].xaxis.set_minor_locator(mdates.HourLocator(byhour=range(0,24,hour_interval))) |
| 1887 | ax[-1].xaxis.set_minor_formatter(mdates.DateFormatter(localtimefmt)) |
| 1888 | ax[-1].xaxis.set_major_locator(mdates.DayLocator(interval=12)) #Choose large interval so dates are not plotted |
| 1889 | ax[-1].xaxis.set_major_formatter(mdates.DateFormatter('')) |
| 1890 | |
| 1891 | # Set time limits if specified |
| 1892 | if not timelimits is None: |
| 1893 | local_timelimits = pd.to_datetime(timelimits) + pd.to_timedelta(local_time_offset,'h') |
| 1894 | ax[-1].set_xlim(local_timelimits) |
| 1895 | |
| 1896 | tstr = 'Local time' |
| 1897 | |
| 1898 | ax2 = [] |
| 1899 | for axi in ax: |
| 1900 | # Format second axis (UTC time) |
| 1901 | ax2i = axi.twiny() |
| 1902 | ax2i.xaxis_date() |
| 1903 | |
| 1904 | # Set time limits if specified |
| 1905 | if not timelimits is None: |
| 1906 | ax2i.set_xlim(timelimits) |
| 1907 | else: |
| 1908 | # Extract timelimits from main axis |
| 1909 | local_timelimits = mdates.num2date(axi.get_xlim()) |
| 1910 | timelimits = pd.to_datetime(local_timelimits) - pd.to_timedelta(local_time_offset,'h') |
| 1911 | ax2i.set_xlim(timelimits) |
| 1912 | |
| 1913 | # Move twinned axis ticks and label from top to bottom |
| 1914 | ax2i.xaxis.set_ticks_position("bottom") |
| 1915 | ax2i.xaxis.set_label_position("bottom") |
| 1916 | |
| 1917 | # Offset the twin axis below the host |
| 1918 | ax2i.spines["bottom"].set_position(("axes", -0.35)) |
| 1919 | |
| 1920 | # Turn on the frame for the twin axis, but then hide all |
| 1921 | # but the bottom spine |
| 1922 | ax2i.set_frame_on(True) |
| 1923 | ax2i.patch.set_visible(False) |
| 1924 | #for sp in ax2.spines.itervalues(): |
no test coverage detected