python - controling subgraphs independently -


the following code (most important parts)

import csv import sys import datetime import numpy np import matplotlib import matplotlib.pyplot plt matplotlib.widgets import slider matplotlib.dates import dateformatter pprint import pprint 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 plot enter image description here

now want add subplot , being able control both independently. 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 want set name whole figure, , particular sub graphs. i've tried bud gives error:

line0.set_title("title second plot") 

then i've read switching between plots , figures gives errors

plt.subplots(211) 

or

plt.subplots(1) 

how can control both sub graphs independently?

you can add title ax[0] saying

ax[0].set_title('blah') 

you cannot set title patch (in case line objects).

interestingly, there's method ax object corresponding 1 matplotlib.pyplot.


Comments