3

I have the following code in my IPython notebook:

import matplotlib.pyplot as plt

plt.setp(plt.xticks()[1], rotation=45)
plt.figure(figsize=(17, 10)) # <--- This is the problematic line!!!!!!!!!!!!!
plt.plot_date(df['date'],df['x'], color='black', linestyle='-')
plt.plot_date(df['date'],df['y'], color='red', linestyle='-')
plt.plot_date(df['date'],df['z'], color='green', linestyle='-')

In the above example df is pandas data frame.

Without the marked line (containig figsize) the plot is too small. With the mentioned line I have an increased image as I want but before it I have an additional empty plot.

Does anybody know why it happens an how this problem can be resolved?

Roman
  • 124,451
  • 167
  • 349
  • 456
  • 1
    See http://stackoverflow.com/questions/14254379/how-can-i-attach-a-pyplot-function-to-a-figure-instance/14261698#14261698 for a ramble about the state machine vs OO interfaces to mpl – tacaswell Jul 24 '13 at 18:05

2 Answers2

4

Try reversing the first two lines after the import. plt.setp is opening a figure.

Molly
  • 13,240
  • 4
  • 44
  • 45
0

here's how I would do this:

import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(17, 10))
plt.setp(plt.xticks()[1], rotation=45)
ax.plot_date(df['date'],df['x'], color='black', linestyle='-')
ax.plot_date(df['date'],df['y'], color='red', linestyle='-')
ax.plot_date(df['date'],df['z'], color='green', linestyle='-')

It's a good practice to explicitly create and operate on your your Figure and Axes objects.

Paul H
  • 65,268
  • 20
  • 159
  • 136