Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to close pandas dataframe plot

This feels like (and probably is) a really dumb question, but what do you call to close a dataframe plot that didn't explicitly call matplotlib?

for example, if you type:

df.hist(data)

Is there a way to close the plot (other than manually clicking on the window's x)?

I'm used to calling plt.close(), but is there something similar in pandas? I've tried close & close(), and neither work.

like image 760
anonygrits Avatar asked Jan 13 '15 17:01

anonygrits


People also ask

How do you reset the index of a data frame?

Use DataFrame.reset_index() function reset_index() to reset the index of the updated DataFrame. By default, it adds the current row index as a new column called 'index' in DataFrame, and it will create a new row index as a range of numbers starting at 0.

How do I visualize pandas DataFrame?

In order to visualize data from a Pandas DataFrame , you must extract each Series and often concatenate them together into the right format. It would be nicer to have a plotting library that can intelligently use the DataFrame labels in a plot.

What is Rot plot?

rot : This keyword argument allows to rotate the markings on the x axis for a horizontal plotting and y axis for a vertical plotting. fontsize : Allows to set the font size for the labels and axis points.


1 Answers

This will wait 5 seconds before closing the window:

import matplotlib.pyplot as plt
import pandas as pd
import time

ax = df.plot()
fig = ax.get_figure()
plt.show(block=False)
time.sleep(5)
plt.close(fig)

Alternatively:

fig, ax = plt.subplots()
df = pd.Series([1, 2, 3])
df.plot(ax=ax)
plt.show(block=False)
time.sleep(5)
plt.close(fig)
like image 158
elyase Avatar answered Sep 20 '22 17:09

elyase