Reputation: 1251
I was wondering how I can adjust my dates here so that they're smaller and fit better on the output.I've tried rotating but they just seem like they're floating aimlessly below the chart. Also Id like to make the legend just either have y_pred
and y_test
one time somewhere where it doesn't overlap my charts.
These subplots are added via a loop and it wont always be the same number of loops.
For reference no_splits will determines how many loops are run via the TimeSeriesSplit
method. Ive removed a lot of the irrelevant code so it's easier to follow
Here is my code:
fig = plt.figure()
tscv = TimeSeriesSplit(n_splits=self.no_splits)
for train_index, test_index in tqdm(tscv.split(X)):
X_train, X_test = X.iloc[train_index], X.iloc[test_index]
y_train, y_test = y.iloc[train_index], y.iloc[test_index]
# predict y values
y_pred = self.regressor.predict(X_test)
# plot y_pred vs y_test
y_df = pd.DataFrame(index= X_test_index)
y_pred = y_pred.reshape(len(y_pred), )
y_test = y_test.reshape(len(y_test), )
y_df['y_pred'] = y_pred
y_df['y_test'] = y_test
ax = fig.add_subplot(int(sqrt(self.no_splits)), int(sqrt(self.no_splits)+1), i)
y_df.plot(title = 'Split{}'.format(i), ax=ax, legend=False)
ax.tick_params(axis='x', rotation=45)
plt.figlegend()
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()
Upvotes: 0
Views: 163
Reputation: 1106
Regarding the date date-labels: You can assign the tick-alignment in the rotation command as shown in this post.
To shrink the labels you have two options:
Option A: Import matplotlib.dates
to get access to DateFormatter
and choose a format that results in smaller labels. (eg. omit the year or something). You can then also use locators to space the labels out differently.
Option B: use the rc_params
or tick_params
to define fontsize, family, etc. This post should get you started.
As you see, there should be a ot of material online to get you going...
Regarding the legend
You can set a plot to have no legend entry using plt.plot(x, y, label='_nolabel')
. This you could combine with a for loop for example, to only draw labels on the first iteration.
for i, (train_index, test_index) in enumerate(tqdm(tscv.split(X))):
if i==0:
plt.plot(x, y, label=label)
else:
plt.plot(x, y, label='_nolabel')
Upvotes: 1