Running a python script with cron, matplotlib error - python

I am trying to run a python script using cron. The script runs without issue from the command line but has problems with matplotlib when run from cron. The error is below.
Traceback (most recent call last):
File "/home/ubuntu/python/spread.py", line 154, in <module>
plot_spread(lat, lon, vals, mean, maxs, mins, stdp, stdm, ens_members)
File "/home/ubuntu/python/spread.py", line 81, in plot_spread
plt.fill_between(x, maxs, stdp, color='r', alpha=0.2)
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 2785, in fill_between
ax = gca()
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 928, in gca
return gcf().gca(**kwargs)
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 578, in gcf
return figure()
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 527, in figure
**kwargs)
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py", line 46, in new_figure_manager
return new_figure_manager_given_figure(num, thisFig)
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py", line 53, in new_figure_manager_given_figure
canvas = FigureCanvasQTAgg(figure)
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py", line 76, in __init__
FigureCanvasQT.__init__(self, figure)
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4.py", line 68, in __init__
_create_qApp()
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt5.py", line 138, in _create_qApp
raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable

I got a similar error when I tried to import matplotlib.pyplot in python using cron. You can force matplotlib to not use any Xwindows backend. Do this:
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
These links might be also helpful:
Generating a PNG with matplotlib when DISPLAY is undefined
Generating matplotlib graphs without a running X server
Hope this is helpful!

Related

pandas plot, vars() argument must have __dict__ attribute?

It was working perfectly earlier but for some reason now I am getting strange errors.
pandas version: 1.2.3
matplotlib version: 3.7.0
sample dataframe:
df
cap Date
0 1 2022-01-04
1 2 2022-01-06
2 3 2022-01-07
3 4 2022-01-08
df.plot(x='cap', y='Date')
plt.show()
df.dtypes
cap int64
Date datetime64[ns]
dtype: object
I get a traceback:
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "/Volumes/coding/venv/lib/python3.8/site-packages/pandas/plotting/_core.py", line 955, in __call__
return plot_backend.plot(data, kind=kind, **kwargs)
File "/Volumes/coding/venv/lib/python3.8/site-packages/pandas/plotting/_matplotlib/__init__.py", line 61, in plot
plot_obj.generate()
File "/Volumes/coding/venv/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py", line 279, in generate
self._setup_subplots()
File "/Volumes/coding/venv/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py", line 337, in _setup_subplots
fig = self.plt.figure(figsize=self.figsize)
File "/Volumes/coding/venv/lib/python3.8/site-packages/matplotlib/_api/deprecation.py", line 454, in wrapper
return func(*args, **kwargs)
File "/Volumes/coding/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 813, in figure
manager = new_figure_manager(
File "/Volumes/coding/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 382, in new_figure_manager
_warn_if_gui_out_of_main_thread()
File "/Volumes/coding/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 360, in _warn_if_gui_out_of_main_thread
if _get_required_interactive_framework(_get_backend_mod()):
File "/Volumes/coding/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 208, in _get_backend_mod
switch_backend(rcParams._get("backend"))
File "/Volumes/coding/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 331, in switch_backend
manager_pyplot_show = vars(manager_class).get("pyplot_show")
TypeError: vars() argument must have __dict__ attribute
In fact, this problem may cause if you running your code like default script (or in PyCharm interactive console), not in Jupyter.
If it is true, you can fix this error by setting up backend directly in your file with use function:
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.use('TkAgg') # !IMPORTANT
fig, ax = plt.subplots()
res = ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) # Plt some data on the axes.o
# plt.show() # optionally show the result.
In some cases TkAgg may not be available. When firstly check, which backend you use. for this, run this simple code:
import matplotlib as mpl
print(mpl.get_backend())
BUT! this must be runned just by your hands in default terminal, outside of PyCharm. (e.g create simple test.py file, paste code, and run python test.py)
Why? Because PyCharm (at least in scientific projects) runs all files with interactive console, where backend module://backend_interagg is used. And this backend causes same error as you have.
So. add mpl.use('TkAgg') in head of your file, or checkout which backend you can use and past those name in this function.

Cartopy not running even with basic introductory example

I have installed Cartopy and am now trying to run the basic example code snippet on the tutorial website just to check everything is working:
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
plt.savefig('coastlines.pdf')
plt.savefig('coastlines.png')
plt.show()
I get the following ValueError back:
Traceback (most recent call last):
File "/Users/mattbright/Desktop/SCripts/lattice_geo.py", line 6, in <module>
ax = plt.axes(projection=ccrs.Mollweide())
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 947, in axes
return subplot(111, **kwargs)
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 1125, in subplot
ax = fig.add_subplot(*args, **kwargs)
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/matplotlib/figure.py", line 1402, in add_subplot
ax = subplot_class_factory(projection_class)(self, *args, **kwargs)
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/matplotlib/axes/_subplots.py", line 42, in __init__
self._axes_class.__init__(self, fig, self.figbox, **kwargs)
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/cartopy/mpl/geoaxes.py", line 355, in __init__
super(GeoAxes, self).__init__(*args, **kwargs)
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 511, in __init__
self.cla()
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/cartopy/mpl/geoaxes.py", line 528, in cla
self._boundary()
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/cartopy/mpl/geoaxes.py", line 1425, in _boundary
trans_path = proj_to_data.transform_path(path)
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/matplotlib/transforms.py", line 1527, in transform_path
return self.transform_path_affine(self.transform_path_non_affine(path))
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/cartopy/mpl/geoaxes.py", line 181, in transform_path_non_affine
src_path.vertices, self.source_projection)
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/cartopy/crs.py", line 649, in quick_vertices_transform
if (x.min() >= x_limits[0] and x.max() <= x_limits[1] and
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/numpy/core/_methods.py", line 43, in _amin
return umr_minimum(a, axis, None, out, keepdims, initial, where)
ValueError: zero-size array to reduction operation minimum which has no identity
I tried the installation with conda and with pip3 plus the list of dependencies given on the website.
There's one previous question about this issue which has gone unanswered since the questioner claimed that an Anaconda reinstall solved the problem. A similar issue with Seaborn has been raised here and solved by reverting to an earlier Matplotlib release (3.3.0)
None of these solutions have worked for me. Has anybody else encountered this and found a way to fix it?

Error with TCL while testing a python code

I'm trying to test a code in python (from internet)
from numpy import corrcoef, sum, log, arange
from numpy.random import rand
from pylab import pcolor, show, colorbar, xticks, yticks
# generating some uncorrelated data
data = rand(10,100) # each row of represents a variable
# creating correlation between the variables
# variable 2 is correlated with all the other variables
data[2,:] = sum(data,0)
# variable 4 is correlated with variable 8
data[4,:] = log(data[8,:])*0.5
# plotting the correlation matrix
R = corrcoef(data)
pcolor(R)
colorbar()
yticks(arange(0.5,10.5),range(0,10))
xticks(arange(0.5,10.5),range(0,10))
show()
but when I execute this script an error message appear
Traceback (most recent call last):
File "test.py", line 20, in <module>
pcolor(R)
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2921, in pcolor
ax = gca()
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 803, in gca
ax = gcf().gca(**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 450, in gcf
return figure()
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 423, in figure
**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 79, in new_figure_manager
return new_figure_manager_given_figure(num, figure)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 87, in new_figure_manager_given_figure
window = Tk.Tk()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
/usr/local/lib/tcl8.6 /usr/lib/tcl8.6 /lib/tcl8.6 /usr/library /library /tcl8.6.4/library /tcl8.6.4/library
/usr/local/lib/tcl8.6/init.tcl: version conflict for package "Tcl": have 8.6.4, need exactly 8.6.1
version conflict for package "Tcl": have 8.6.4, need exactly 8.6.1
while executing
"package require -exact Tcl 8.6.1"
(file "/usr/local/lib/tcl8.6/init.tcl" line 19)
invoked from within
"source /usr/local/lib/tcl8.6/init.tcl"
("uplevel" body line 1)
invoked from within
"uplevel #0 [list source $tclfile]"
This probably means that Tcl wasn't installed properly.
I tried to reinstall tcl, but I cant install the specific version, can anyone help me please with any idea??

RuntimeError: Invalid DISPLAY variable

I am running my python script in another machine by using ssh command in linux. I have also run this command :
source ~/.bashrc
after logging in the other machine, in order to define the proper paths in the new machine. I was getting the error message for running the following python code lines even I have tried to follow the instruction in this question by defining the backend.
>>> import matplotlib
>>> import pylab as plt
>>> matplotlib.use('Agg')
>>> import numpy as np
>>> x=np.arange(0,2,0.001)
>>> y=np.sin(x)**2+4*np.cos(x)
>>> fig = plt.figure()
>>> plt.plot(x,y,'r.')
The error message
This probably means that Tcl wasn't installed properly.
Traceback (most recent call last):
File "Systematic_Optimised.py", line 513, in <module>
fig = plt.figure()
File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 435, in figure
**kwargs)
File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 47, in new_figure_manager
return new_figure_manager_given_figure(num, thisFig)
File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 54, in new_figure_manager_given_figure
canvas = FigureCanvasQTAgg(figure)
File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 72, in __init__
FigureCanvasQT.__init__(self, figure)
File "/vol/aibn84/data2/zahra/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4.py", line 68, in __init__
_create_qApp()
File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 138, in _create_qApp
raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable
any suggestion how to fix the problem
You must declare matplotlib.use('agg') before import pylab as plt.
Reference
Add
plt.switch_backend('agg')
after
import matplotlib.pyplot as plt

Using Matplotlib when DISPLAY is undefined [duplicate]

This question already has answers here:
How to save a figure remotely with pylab? [duplicate]
(2 answers)
Closed 8 years ago.
When using Matplotlib on a remote machine (e.g. on Travis CI), I run into frequent runtime errors related to the DISPLAY environmental variable not being set. As recommended, I have set the Agg backend via matplotlib.use at the beginning of my test scripts, and have ensured there are no calls to show(). Nevertheless, I still get errors such the following:
Traceback (most recent call last):
File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/home/travis/build/pymc-devs/pymc/pymc/tests/test_plots.py", line 36, in test_multichain_plots
forestplot(ptrace, vars=['early_mean', 'late_mean'])
File "/home/travis/build/pymc-devs/pymc/pymc/plots.py", line 325, in forestplot
interval_plot = subplot(gs[0])
File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 896, in subplot
fig = gcf()
File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 450, in gcf
return figure()
File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 423, in figure
**kwargs)
File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 31, in new_figure_manager
return new_figure_manager_given_figure(num, thisFig)
File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 38, in new_figure_manager_given_figure
canvas = FigureCanvasQTAgg(figure)
File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 70, in __init__
FigureCanvasQT.__init__( self, figure )
File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/backends/backend_qt4.py", line 207, in __init__
_create_qApp()
File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/backends/backend_qt4.py", line 62, in _create_qApp
raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable
Are there other recommendations to averting this?
Using xvfb works, see Travis's
GUI & Headless browser testing documentation.
p.s. The matplotlib.use('Agg') trick works for me.

Categories

Resources