I'm trying to suppress the output/plots run in the code below (because I plan on adjusting the plots later), but regardless of what I've tried, nothing seems to work.
I've tried all the following based on the referenced articles (littering my code, will need to clean up), but nothing seems to work.
add semi-colons
pass; statements
adjusting the notebook's environment conditions
using subprocess functions and modified suppress functions
Related SO:
Remove output of all subprocesses in Python without access to code
Silence the stdout of a function in Python without trashing sys.stdout and restoring each function call
Python: Suppress library output not using stdout
IPython, semicolon to suppress output does not work
https://github.com/ipython/ipython/issues/10794
Suppress output in matplotlib
dictionary_of_figures = OrderedDict()
dictionary_of_images = OrderedDict()
from contextlib import contextmanager
import sys, os
import subprocess
import inspect
import contextlib
import io
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import matplotlib.ticker as ticker
from collections import OrderedDict
def draw_year_close_plot(df, group_by_column_name, year):
reduced_range = df.loc[(df['Year'] == year)]
year_string = str(year)
# 0 - Setup
matplotlib.rc_file_defaults();
ax1 = sns.set_style("darkgrid"); #"style must be one of white, dark, whitegrid, darkgrid, ticks"
fig, ax1 = plt.subplots(figsize=(5,2));
# 1 - Create Closing Plot
lineplot = sns.lineplot(data = reduced_range['Close'], sort = False, ax=ax1);
pass;
ax1.xaxis.set_major_formatter(ticker.EngFormatter())
lineplot.set_title(company_name + str(" (")+ stock_ticker + str(") - ") + 'Historical Close & Volume - ' + year_string, fontdict= { 'fontsize': 8, 'fontweight':'bold'})
# 2 - Create Secondary Plot - Volume
ax2 = ax1.twinx();
ax2.grid(False);
sns.lineplot(data = reduced_range['Volume'], sort = False, ax=ax2, alpha=0.15);
pass;
return fig
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "last_expr"
##contextmanager
#def suppress_stdout():
# with open(os.devnull, "w") as devnull:
# old_stdout = sys.stdout
# sys.stdout = devnull
# try:
# yield
# finally:
# sys.stdout = old_stdout
##contextlib.contextmanager
#def nostdout():
# save_stdout = sys.stdout
# sys.stdout = io.BytesIO()
# yield
# sys.stdout = save_stdout
with contextlib.redirect_stdout(io.StringIO()):
for year in range(min_year,min_year+5):
dictionary_of_figures[year] = draw_year_close_plot(daily_df,'Year', year);
dictionary_of_images[year] = fig2img(dictionary_of_figures[year]);
Any ideas?
It looks like you are asking to suppress plots while in the Jupyter environment. The %matplotlib inline causes the plots to be rendered on the output. If you remove that line, you will not get the plot rendered and you'll get back the plot object (I tested this on your code).
You can't comment out %matplotlib inline once the kernel in Jupyter has run it - it persists within the kernel. You need to comment it out and restart the kernel, at which point I think you'll see the behavior you want.
Once you've modified the plots as you with, you can turn %matplotlib inline back on and render your updated plots.
If you need to turn %matplotlib inline on and off, you need to know a little about your Jupyter environment. Please see this answer
UPDATE:
I tried a few cases. It looks best if you explicitly set %matplotlib to an option other than inline. Here is minimal code to illustrate. I have kept all of your graphics-related code and made up data where your question does not provide values, and print the type for fig (which is your return value). I have also explicitly set %matplotlib notebook. Note that you should run %matplotlib --list to make sure that is one of your choices.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.ticker as ticker
df = pd.DataFrame({'Close': [1,2,3], 'Volume': [4,5,6]})
%matplotlib notebook
# matplotlib.rc_file_defaults() # We don't have your defaults file
ax1 = sns.set_style("darkgrid"); #"style must be one of white, dark, whitegrid, darkgrid, ticks"
fig, ax1 = plt.subplots(figsize=(5,2))
lineplot = sns.lineplot(data=df['Close'], sort = False, ax=ax1)
ax1.xaxis.set_major_formatter(ticker.EngFormatter())
lineplot.set_title("This is the Title")
ax2 = ax1.twinx()
ax2.grid(False)
sns.lineplot(data=df['Volume'], sort = False, ax=ax2, alpha=0.15)
print(type(fig))
Related
I am trying to do something like this to have a closer look at my data:
What is the currently correct way to dynamically update plots in Jupyter/iPython?
Or here:
https://nbviewer.jupyter.org/gist/branning/c8e63ce81be0391260b1
This is my code:
%matplotlib notebook
from matplotlib import pyplot as plt
import pandas as pd
START = DATA.index[0]
END = DATA.index[-1]
DT = "6H"
DATERANGE = pd.date_range(START, END, freq=DT)
fig, ax = plt.subplots(figsize(8, 6))
since = DATERANGE[0]
for till in DATERANGE[1:]:
data = DATA['SOME_SERIES'].loc[since:till]
if len(data) > 0:
if ax.lines:
ax.lines[0].set_xdata(data.index)
ax.lines[0].set_ydata(data)
else:
ax.plot(data.index, data)
upper, lower = data.min()*0.9, data.max()*1.1
if not (isnan(upper) or isnan(lower)):
ax.set_ylim((data.min()*0.9, data.max()*1.1))
ax.set_xlim((data.index[0], data.index[-1]))
fig.canvas.draw()
time.sleep(2)
since = till
My problem is that while the plot is updating it doesn't fill the canvas (hope I got the terminology right there) but is only about a quarter of the size.
It looks like this:
Plot while looping
Only when the loop ended the plot gets bigger:
Plot after looping
This is also the case with the exact code from the links above.
I updated jupyter and matplotlib, I tried fig.tight_layout(), I also tried %matplotlib notebook %matplotlib nbagg but that didn't do the trick either..
Does anybody have a solution for this?
Thanks,
Phillip
For me this was an issue when working on my Mac. There is an open issue on this; right now it seems that the best solution is to split the code into two cells. Shameless cross-copying of that solution:
# Cell 1
%matplotlib notebook
# OR: %matplotlib widget
import matplotlib.pyplot as plt
import numpy as np
import time
fig = plt.figure()
# Cell 2
for j in range(10):
plt.plot(range(0, j), [1/(i+1) for i in range(0, j)])
fig.canvas.draw()
time.sleep(.1)
Alternatively one could go for the "inline" backend. Of course that one is not interactive, and a bit uglier. See this thread
I'm using Python 3.6 in jupyter notebook. plt.close does not close plot. I tried with plt.ion() also and many other ways.
I want to display image, then wait for pause or input() and then remove the previous image and show the new one.
import matplotlib.pyplot as plt
from time import sleep
from scipy import eye
plt.imshow(eye(3))
plt.show()
sleep(1)
plt.close()
Here is an example that shows a sequence of plots, each for one second. Essential are the commants plt.show(block = False) and plt.pause(1) instead of sleep(1):
import numpy as np
import matplotlib.pyplot as plt
def show_image(n):
fig, ax = plt.subplots()
x = np.linspace(0,1,100)
y = x**n
ax.plot(x,y, label = 'x**{}'.format(n))
ax.legend()
plt.show(block=False)
plt.pause(1)
plt.close(fig)
for i in range(10):
show_image(i)
If I understand correctly, what you want is to show a plot, wait 1 second, then let it close automatically.
This would be achieved as follows.
import matplotlib.pyplot as plt
from scipy import eye
plt.imshow(eye(3))
def show_and_close(sec):
timer = plt.gcf().canvas.new_timer(interval=sec*1000)
timer.add_callback(lambda : plt.close())
timer.single_shot = True
timer.start()
plt.show()
show_and_close(1)
For extensive plotting scripts, I use matplotlibs rcParams to configure some standard plot settings for pandas DataFrames.
This works well for colors and font sizes but not for the default colormap as described here
Here's my current approach:
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import cm
# global plotting options
plt.rcParams.update(plt.rcParamsDefault)
matplotlib.style.use('ggplot')
plt.rcParams['lines.linewidth'] = 2.5
plt.rcParams['axes.facecolor'] = 'silver'
plt.rcParams['xtick.color'] = 'k'
plt.rcParams['ytick.color'] = 'k'
plt.rcParams['text.color'] = 'k'
plt.rcParams['axes.labelcolor'] = 'k'
plt.rcParams.update({'font.size': 10})
plt.rcParams['image.cmap'] = 'Blues' # this doesn't show any effect
# dataframe with random data
df = pd.DataFrame(np.random.rand(10, 3))
# this shows the standard colormap
df.plot(kind='bar')
plt.show()
# this shows the right colormap
df.plot(kind='bar', cmap=cm.get_cmap('Blues'))
plt.show()
The first plot does not use the colormap via colormap (which it should normally do?):
It only works if I pass it as an argument as in the second plot:
Is there any way to define the standard colormap for pandas DataFrame plots, permanently?
Thanks in advance!
There is no supported, official way to do it; you are stuck because of pandas's internal _get_standard_colors function that hardcodes the use of matplotlib.rcParams['axes.color_cycle'] and falls back to list('bgrcmyk'):
colors = list(plt.rcParams.get('axes.color_cycle',
list('bgrcmyk')))
There are various hacks you can use, however; one of the simplest, which works for all pandas.DataFrame.plot() calls, is to wrap pandas.tools.plotting.plot_frame:
import matplotlib
import pandas as pd
import pandas.tools.plotting as pdplot
def plot_with_matplotlib_cmap(*args, **kwargs):
kwargs.setdefault("colormap", matplotlib.rcParams.get("image.cmap", "Blues"))
return pdplot.plot_frame_orig(*args, **kwargs)
pdplot.plot_frame_orig = pdplot.plot_frame
pdplot.plot_frame = plot_with_matplotlib_cmap
pd.DataFrame.plot = pdplot.plot_frame
To test in a notebook:
%matplotlib inline
import pandas as pd, numpy as np
df = pd.DataFrame(np.random.random((1000,10))).plot()
...yields:
I am trying to chart this data in matplotlib and I am getting the following error message:
raise TypeError('Unrecognized argument type %s to close'%type(arg))
TypeError: Unrecognized argument type <type 'list'> to close
The data I am sending to it is not a string, it is a float as you can see from the code below:
import os
import csv
import glob as g
import pprint as p
import matplotlib.pyplot as plt
os.chdir('F:\\')
def graphWriter():
for file in g.glob('*.TXT'):
for col in csv.DictReader(open(file,'rU')):
set_ = int(col[' Set'])
iriR = float(col[' IRI R e'])
iriL = float(col['IRI LWP '])
rutL = float(col[' RUT L e'])
rutR = float(col[' RUT R e'])
start = float(col['Start-Mi'])
end = float(col[' End-Mi'])
fig = plt.plot(iriR,iriL)
plt.show()
plt.close(fig)
graphWriter()
Though the window is coming up to chart the data and the units are correct, there is also no line in the chart, probably that's stemming from the apparent data issue. So the question is whats causing the error message, and whats causing there to be no data lines in the chart. But the two are most likely related. Here is some of the input data though I am only trying to graph the two datasets to the right side which would be iriR and iriL as show above:
(194.449, 194.549, 90.0, 77.9)
(194.549, 194.649, 84.6, 81.5)
(194.649, 194.749, 88.4, 84.1)
(194.749, 194.849, 69.5, 82.9)
(194.849, 194.949, 76.2, 71.0)
The problem is that the function plt.plot returns a list of lines (that were added to the plot), and not a Figure object --- while plt.close only accepts a Figure object. There are numerous ways to work around this,
First, get the figure object ("get current figure"):
fig = plt.gcf()
plt.close(fig)
Second, call close with no arguments: plt.close() --- this will automatically close the active figure.
Third, close all figures: plt.close('all').
All of these usages are covered in the matplotlib.pyplot.close documentation.
Edit:
The next issue is that you're not storing an array of values to your variables, instead you're just storing a single floating value. You can initialize a list, and store new elements to it.
os.chdir('F:\\')
iriR = [] # Initialize a list
def graphWriter():
for file in g.glob('*.TXT'):
for col in csv.DictReader(open(file,'rU')):
set_ = int(col[' Set'])
iriR.append(float(col[' IRI R e'])) # Append new entry
Do the same thing for the other variables that you want to plot.
Maybe this will work.
import pandas as pd
import matplotlib.pyplot as plt
import glob as g
def graphWriter():
data = {}
for file in g.glob('*.TXT'):
data[file] = pd.read_csv(file)
# Removes ')' and turn it into float
data[file][3] = data[file][3].apply(lambda x:x[:-1]).astype(float)
fig, ax = plt.subplots()
for d in data.itervalues():
ax.plot(d[:,2], d[:,3])
plt.show()
plt.close(fig)
graphWriter()
The function will obtain a list of files ended in .TXT then it will load them into a dictionary in which the keys are the names of the files. Later will plot them.
Update
Since the OP posted that pandas is unavailable, its is possible to use numpy.
import numpy as np
import matplotlib.pyplot as plt
import glob as g
def graphWriter():
data = {}
for file in g.glob('*.TXT'):
data[file] = np.fromregex(file, '\d*\.\d*',
dtype=[('1', float), ('2', float),
('3', float), ('4', float)])
fig, ax = plt.subplots()
for d in data.itervalues():
ax.plot(d['3'], d['4'])
plt.show()
plt.close(fig)
graphWriter()
What happened is I followed this demo, I modified it to suit my needs had it working, changed it to use a function to draw two graphs but now it doesn't work at all using plt.show() or plt.savefig()
here's my code
import csv
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
# I converted excel to a csv file
data = [x for x in csv.reader(open('ASS1_Q1.csv'))]
question1 = {}
question1['males'] = []
question1['females'] = []
for x in data:
if x[0].lower() == "male":
question1["males"].append(float(x[1]))
elif x[0].lower() == "female":
question1['females'].append(float(x[1]))
else:
print "Not a valid dataline", x
def plot_graph(data, filename):
fig = plt.figure()
ax = fig.add_subplot(111)
n, bins, patches = ax.hist(np.array(data), bins=13, align='mid', facecolor='#888888')
ax.set_xlabel('Speed in kph')
ax.set_ylabel('Amount of Females')
ax.set_xlim(min(data, max(data)))
# plt.savefig(filename)
plt.show()
plot_graph(question1['males'], "ASS1Q1-males.eps")
#plot_graph(question1['females'], "ASSQ2-females.eps")
print summary(question1['males'])
print summary(question1['females'])
Can someone explain why this is happening? what am I doing wrong?
Try removing
import matplotlib
matplotlib.use('Agg')
The command
python -c 'import matplotlib; matplotlib.use("")'
will show you the valid string arguments that can be sent to matplotlib.use.
On my machine, 'Agg' is listed as valid, though I get no output when this is set. If you are curious, you could just keep trying various options until you find one that works.
When you find the one that your prefer, you may also find it more convenient to set something like
backend : GtkAgg
in your ~/.matplotlib/matplotlibrc instead of using matplotlib.use(...).