Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 216
Roman Avatar asked May 07 '26 21:05

Roman


2 Answers

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

like image 178
Molly Avatar answered May 10 '26 09:05

Molly


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.

like image 24
Paul H Avatar answered May 10 '26 10:05

Paul H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!