The following code (most important parts)
import csv
import sys
import datetime
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from matplotlib.dates import DateFormatter
from pprint import pprint as pp
...
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)
l, = plt.plot(x,y1, label="kw")
l, = plt.plot(x,y2, label="pred")
plt.xlabel('date/time')
plt.ylabel('kw_energy')
plt.title("Gym")
plt.legend()
formatter = DateFormatter('%m-%d %H:%M:%S')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)
locs, labels = plt.xticks()
plt.setp(labels, rotation=45)
plt.grid()
x_min_index = 0
x_max_index = 96
x_min = x[x_min_index]
x_max = x[x_max_index]
x_dt = x_max - x_min
y_min = plt.axis()[2]
y_max = plt.axis()[3]
plt.axis([x_min, x_max, y_min, y_max])
axcolor = 'lightgoldenrodyellow'
axpos = plt.axes([0.2, 0.1, 0.65, 0.03], axisbg=axcolor)
slider_max = len(x) - x_max_index - 1
spos = Slider(axpos, 'Pos', matplotlib.dates.date2num(x_min), matplotlib.dates.date2num(x[slider_max]))
spos.valtext.set_visible(False)
def update(val):
pos = spos.val
xmin_time = matplotlib.dates.num2date(pos)
xmax_time = matplotlib.dates.num2date(pos) + x_dt
# DEBUG:
print "x_min: %s, x_max: %s" % (xmin_time.strftime("%Y-%m-%d %H:%M:%S.%f"), xmax_time.strftime("%Y-%m-%d %H:%M:%S.%f"))
print pos
print spos.valtext
print "##################"
xmin_time = pos
ax.axis([xmin_time, xmax_time, y_min, y_max])
spos.on_changed(update)
plt.show()
Produces this plot
Now I want to add another subplot and being able to control both independently. I have this:
plt.close('all')
fig, ax = plt.subplots(nrows=2, ncols=1)
plt.subplots_adjust(bottom=0.25)
line0 = ax[0].plot(x,y1, lw=2, label='kw_energy_consumption')
line1 = ax[1].plot(x,y2, lw=2, label='prediction')
Now I want to set the name for whole figure, and also for particular sub graphs. I've tried this bud gives an error:
line0.set_title("Title for second plot")
Then I've read about switching between plots and figures but it also gives errors
plt.subplots(211)
or
plt.subplots(1)
How can I control both sub graphs independently?