0

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 enter image description here

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122

1 Answers1

1

You can add a title to ax[0] by simply saying

ax[0].set_title('blah')

You cannot set title to a patch (in your case the line objects).

Interestingly, there's often a method for an ax object corresponding to one for matplotlib.pyplot.

cntswj
  • 335
  • 3
  • 10
  • This is true for setting the title but I am searching for something more general. E.g. how to rotate x labels. I've specified the details, please see my question http://stackoverflow.com/questions/31372953/plt-setp-alternative-for-subplots-or-how-to-set-text-rotation-on-x-axis-for-subp – Wakan Tanka Jul 12 '15 at 22:14
  • you can use something like `ax[0].set_xticklabels(..., rotation=70)` – cntswj Jul 13 '15 at 02:41
  • I've tried add `line1_labels = axarr[1].get_xticklabels()` and `axarr[0].set_xticklabels(line1_labels, rotation=45)` but instead of values I get weird numbers (which had rotation of 45 degrees) – Wakan Tanka Jul 13 '15 at 08:15
  • Something like `Text(2,0,u'Text(2,0,u"Text(0.4,u\'0.4\')")')`. I've added those lines just before plt.show() – Wakan Tanka Jul 14 '15 at 07:19