简体   繁体   中英

Why I get additional empty plot in matplotlib?

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM