Skip to content

Commit

Permalink
Fix plot-series bug regarding x-axis labels (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
gracewgao committed Dec 13, 2020
1 parent 98b92ce commit 28646ab
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions sktime/utils/plotting/__init__.py
Expand Up @@ -5,8 +5,6 @@
__all__ = ["plot_series"]
__author__ = ["Markus Löning"]

import warnings

import numpy as np

from sktime.utils.check_imports import _check_soft_dependencies
Expand All @@ -30,6 +28,8 @@ def plot_series(*series, labels=None):
"""
_check_soft_dependencies("matplotlib", "seaborn")
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter, MaxNLocator
from matplotlib.cbook import flatten
import seaborn as sns

n_series = len(series)
Expand Down Expand Up @@ -75,10 +75,19 @@ def plot_series(*series, labels=None):

plot_func(x=x, y=y, ax=ax, marker="o", label=label, color=color)

# set combined index as xticklabels, suppress matplotlib warning
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
ax.set(xticklabels=index)
# combine data points for all series
xs_flat = list(flatten(xs))

# set x label of data point to the matching index
def format_fn(tick_val, tick_pos):
if int(tick_val) in xs_flat:
return index[int(tick_val)]
else:
return ""

# dynamically set x label ticks and spacing from index labels
ax.xaxis.set_major_formatter(FuncFormatter(format_fn))
ax.xaxis.set_major_locator(MaxNLocator(integer=True))

if legend:
ax.legend()
Expand Down

0 comments on commit 28646ab

Please sign in to comment.