Error with TCL while testing a python code - python

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??

Related

Can't use pyplot due to _tkinter.TclError: unknown color name

I've got a simple code:
from matplotlib import pyplot as plt
plt.plot([1,2,5])
plt.show()
It works fine in jupyter notebook, however when I try to run it using command line:
$ python3 main.py
It throws an error:
_tkinter.TclError: unknown color name "[97]#282a36"
The whole trackeback:
Traceback (most recent call last):
File "main.py", line 2, in <module>
plt.plot([1,2,5])
File "/home/user/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2811, in plot
return gca().plot(
File "/home/user/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 935, in gca
return gcf().gca(**kwargs)
File "/home/user/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 578, in gcf
return figure()
File "/home/user/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 525, in figure
**kwargs)
File "/home/user/.local/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 3218, in new_figure_manager
return cls.new_figure_manager_given_figure(num, fig)
File "/home/user/.local/lib/python3.6/site-packages/matplotlib/backends/_backend_tk.py", line 1008, in new_figure_manager_given_figure
window = Tk.Tk(className="matplotlib")
File "/usr/lib/python3.6/tkinter/__init__.py", line 2023, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: unknown color name "[97]#282a36"
I already tried changing matplotlib's backend:
import matplotlib
matplotlib.use('pdf') # Or using other arguments matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.plot([1,2,5])
plt.show()
It gives me the same error message.
I also tried installing matplotlib using pip and my distribution package manager, both giving me the same error.
tkinter has been installed from my distribution repositories.
Any suggest would be helpful, I couldn't find any solution on similar questions.
After reading this issue on matplotlib's Github page, I emptied out my .Xresources and it fixed the issue.
So I looked a little bit more into the .Xresources file and I found out a line:
*background: [97]#282a36
Which was the cause of matplotlib complaining about a color nameed: [97]#282a36:
_tkinter.TclError: unknown color name "[97]#282a36"
Removing [97] from the line fixed the issue. remember that you have to run:
xrdb -merge .Xresources
To make the changes take place.

Using matplotlib to create a histogram on Windows Subsystem for Linux Ubuntu

I am trying to create a simple histogram for a csv file named "count_backers.csv". The code that I have so far is as follows:
import matplotlib.pyplot as plt
# import numpy as np
import plotly.plotly as py
import csv
def make_hist(csv_filepath):
data = open(csv_filepath)
reader = csv.reader(data)
column = []
for row in data:
column.append(row)
plt.hist(column)
plt.title("Number of Backers")
plt.xlabel("Frequency")
plt.ylabel("Value")
plt.show()
# fig = plt.gcf()
# plot_url = py.plot_mpl(fig, filename='backers_histogram')
backer_data = r"C:/Users/user/Documents/user/Programming/count_backers.csv"
make_hist(backer_data)
I began learning python using Learn Python the Hard Way, and I am used to executing python files from PowerShell. However, I was struggling to install and build matplotlib on Windows and for the sake of time I decided to install it on Windows Subsystem for Linux Ubuntu. After I was able to get that working I am now getting the following error:
Traceback (most recent call last):
File "histogram.py", line 22, in <module>
make_hist(backer_data)
File "histogram.py", line 7, in make_hist
data = open(csv_filepath)
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/user/Documents/user/Programming/count_backers.csv'
I have tried several different versions of the filepath including:
backer_data = r"C:\Users\user\Documents\user\Programming\count_backers.csv"
backer_data = "mnt/c/Users/user/Documents/user/Programming/count_backers.csv"
backer_data = r"mnt\c\Users\user\Documents\user\Programming\count_backers.csv"
These produce the following errors respectively:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\user\\Documents\\user\\Programming\\count_backers.csv'
FileNotFoundError: [Errno 2] No such file or directory: 'mnt/c/Users/user/Documents/user/Programming/count_backers.csv'
FileNotFoundError: [Errno 2] No such file or directory: 'mnt\\c\\Users\\user\\Documents\\user\\Programming\\count_backers.csv'
When I use ls on the specified directory though it does show the file being there:
smidem#JealousHippo:/mnt/c/Users/user/Documents/user/Programming$ ls
column_avg.py count_backers.csv histogram.py pledged.csv
I'm pretty confused as to why it is unable to locate this file and any help would be greatly appreciated.
With l0b0's help I changed backer_data to backer_data = "/mnt/c/Users/user/Documents/user/Programming/count_backers.csv" but I am now receiving this in response:
Traceback (most recent call last):
File "histogram.py", line 22, in <module>
make_hist(backer_data)
File "histogram.py", line 12, in make_hist
plt.hist(column)
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 2947, in hist
ax = gca()
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 928, in gca
return gcf().gca(**kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 578, in gcf
return figure()
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 527, in figure
**kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 84, in new_figure_manager
return new_figure_manager_given_figure(num, figure)
File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 92, in new_figure_manager_given_figure
window = Tk.Tk()
File "/usr/lib/python3.5/tkinter/__init__.py", line 1871, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
"mnt/c/Users/user/Documents/user/Programming/count_backers.csv" is a relative path starting at the current working directory, which most likely is not the filesystem root. Try starting the path with a slash.

Running a python script with cron, matplotlib error

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!

Plotting error python in linux environment pandas...possible to just save? [duplicate]

This question already has answers here:
Generating matplotlib graphs without a running X server [duplicate]
(2 answers)
Closed 8 years ago.
I am trying to plot a pandas series in python. However, rather than working from my home computer I am working via grid computing on a linux shell. When I type:
series.plot()
I get this error:
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pandas-0.15.0-py2.7-linux-x86_64.egg/pandas/tools/plotting.py", line 2487, in plot_series
**kwds)
File "/usr/local/lib/python2.7/dist-packages/pandas-0.15.0-py2.7-linux-x86_64.egg/pandas/tools/plotting.py", line 2293, in _plot
plot_obj.generate()
File "/usr/local/lib/python2.7/dist-packages/pandas-0.15.0-py2.7-linux-x86_64.egg/pandas/tools/plotting.py", line 919, in generate
self._setup_subplots()
File "/usr/local/lib/python2.7/dist-packages/pandas-0.15.0-py2.7-linux-x86_64.egg/pandas/tools/plotting.py", line 952, in _setup_subplots
fig = self.plt.figure(figsize=self.figsize)
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 343, in figure
**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
window = Tk.Tk()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1688, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
Does anyone know what I can do or what I should ask my sys admin to do? Is it possible to just save the plot to a file without having it in some sort of display environment first?
You could reset the matplotib backend to one that does not require a display, such as AGG, PNG, SVG, PDF, PS, Cairo, or GDK:
import numpy as np
import pandas as pd
def reset_backend(backend):
import sys
del sys.modules['matplotlib.backends']
del sys.modules['matplotlib.pyplot']
import matplotlib as mpl
mpl.use(backend) # do this before importing pyplot
import matplotlib.pyplot as plt
return plt
reset_backend('agg')
ser = pd.Series(np.random.random(5))
ser.plot()
plt.savefig('/path/to/file.png')
Note that what backends you have available depends on how your installation of matplotlib was built. If you call reset_backend with a backend that is not available, you will see an error message which lists the backends which are available on your installation.

Python: error problems to import module matplotlib

This afternoon after installing progressbar I have this problem importing matplolib:
>>> import matplotlib
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 772, in <module>
rcParams = rc_params()
File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 690, in rc_params
fname = matplotlib_fname()
File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 602, in matplotlib_fname
fname = os.path.join(get_configdir(), 'matplotlibrc')
File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 253, in wrapper
ret = func(*args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 475, in _get_configdir
raise RuntimeError("'%s' is not a writable dir; you must set %s/.matplotlib to be a writable dir. You can also set environment variable MPLCONFIGDIR to any writable directory where you want matplotlib data stored "% (h, h))
RuntimeError: 'C:\Users\ALEM' is not a writable dir; you must set C:\Users\ALEM/.matplotlib to be a writable dir. You can also set environment variable MPLCONFIGDIR to any writable directory where you want matplotlib data stored
I had tried to reinstall matplolib and python 27 several times on my windows 7 64 bit, but I have always the same error.
The Error message says:
please run the chkdsk utility
But I don't understand where is this utility and how I can run it.

Categories

Resources