Matplotlib.pyplot backend qt giving ValueError of image size - matplotlib

I am using Python 3.7. I would like to change my backend temporarily so that I can zoom in/out of my data. However, generating figures using qt backend gives me a ValueError, even if the figure is minimally simple.
Here is a minimum example, where I typed the following commands in the console one by one
[Input Commands]
import matplotlib.pyplot as plt
%matplotlib qt
plt.plot([1,2],[1,2])
[Output Error]
[<matplotlib.lines.Line2D at 0x1b2efda7648>]Traceback (most recent call last):
File "C:\Users\USER_NAME\anaconda3\lib\site-packages\matplotlib\backends\backend_qt.py", line 455, in _draw_idle
self.draw()
File "C:\Users\USER_NAME\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 431, in draw
self.renderer = self.get_renderer(cleared=True)
File "C:\Users\USER_NAME\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 447, in get_renderer
self.renderer = RendererAgg(w, h, self.figure.dpi)
File "C:\Users\USER_NAME\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 93, in __init__
self._renderer = _RendererAgg(int(width), int(height), dpi)
ValueError: Image size of 213120x159840 pixels is too large. It must be less than 2^16 in each direction.
I don't think this question is a duplicate of previous questions (here and here) because the error appears even for a very simple figure with no labels.
I thought I had accidentally overwritten some kind of setting file, so I tried re-installing matplotlib using
"conda install matplotlib" at the Anaconda prompt, but it didn't help. Changing back to my default backend "%matplotlib inline" generates figures normally.
How can I fix my qt backend?

Related

How to solve **TypeError : expected str, bytes or os.PathLike object, not WindowsPath** when importing matplotlib to python

I am trying use matplotlib within Autodesk Revit software from the PyRevit Project
In this project, Pyrevit brings an embedded CPython interpreter (3.8.5) to Revit.
Until now, I've managed to import some packages (e.g. numpy, pandas), but unfortunately import matplotlib has been a big challenge.
When I try to import matplotlib, I got the following error:
CPython Traceback:
TypeError : expected str, bytes or os.PathLike object, not WindowsPath
File "C:\Users\carlo\pyproj\bimenv\Lib\site-packages\matplotlib\__init__.py", line 886, in <module>
rcParamsDefault = _rc_params_in_file(
File "C:\Users\carlo\pyproj\bimenv\Lib\site-packages\matplotlib\__init__.py", line 789, in _rc_params_in_file
with _open_file_or_url(fname) as fd:
File "contextlib.py", line 113, in __enter__
File "C:\Users\carlo\pyproj\bimenv\Lib\site-packages\matplotlib\__init__.py", line 765, in _open_file_or_url
fname = os.path.expanduser(fname)
File "ntpath.py", line 293, in expanduser
...\matplotlib_init_.py
File ntpath.py
Doing the same, import matplotlib, from the Python 3.8.5 in the command line it works without any problem.
enter image description here
Thank you in advance for any suggestion from your side to solve this issue.

Python Script that used to work, is now getting automatically killed in Ubuntu

I was once able to run the below python script on my Ubuntu machine without the memory errors I was getting on windows.
import pandas as pd
import numpy as np
#create a pandas dataframe for each input file
dfs1 = pd.read_csv('s1.csv', encoding='utf-8', names=list(range(0,107)),dtype='string', na_filter=False)
dfs2 = pd.read_csv('s2.csv', encoding='utf-8', names=list(range(0,107)),dtype='string', na_filter=False)
dfr = pd.read_csv('r.csv' , encoding='utf-8', names=list(range(0,107)),dtype='string', na_filter=False)
#combine them into one dataframe
dfs12r = pd.concat([dfs1, dfs2, dfr],ignore_index=True)#withour ignore index the line numbers are not adjusted
# bow is "comming
wordlist=[]
for line in range(8052):
for row in range(106) :
#print(line,row,dfs12r[row][line])
if dfs12r[row][line] not in wordlist :
wordlist.append(dfs12r[row][line])
wordlist.sort()
#print(wordlist)
print(len(wordlist)) #12350
dfBOW = pd.DataFrame(np.zeros((len(dfs12r.index), len(wordlist))),dtype='int')
#create the dictionary
wordDict = dict.fromkeys(wordlist,'default')
counter=0
for word in wordlist :
wordDict[word] = counter
counter+=1
#print(wordDict)
#will start scanning every word from dfS12R and +1 the respective cell in dfBOW
for line in range(8052):
for row in range(107):
dfBOW[wordDict[dfs12r[row][line]]][line]+=1
Unfortunately, probably after some automatic Ubuntu updates I am now getting the simple message "KIlled", after trying to run the process without any further explanation.
Through simple print statements I know that the script is interrupted inside the for loop in the end.
I understand that I should be able to make the script more memory efficient, but I am also hoping for guidance on how to get Ubuntu able to run again the same script like they used to. (Through the TOP command I can see the all of my memory including the swap is being used while inside this loop)
Could paging have been disabled somehow after the updates? Any advice is welcome.
I still have 16GB of RAM, and use Ubuntu 20.04 (Specs are the same before and after the script stopped working). I use dual boot on the same SSD.
Below is the error I am getting from teh same script on windows :
Traceback (most recent call last):
File "D:\sharedfiles\Organised\WorkSpace\ptixiaki\github\ptixiaki\code\makingthedata\2.1 Approach (Same as 2 but turning all words to lowercase)\2.1_CSVtoDataframe\CSVtoBOW.py", line 60, in <module>
dfBOW[wordDict[dfs12r[row][line]]][line]+=1
File "D:\wandowsoftware\anaconda\envs\ptixiaki\lib\site-packages\pandas\core\series.py", line 1143, in __setitem__
self._maybe_update_cacher()
File "D:\wandowsoftware\anaconda\envs\ptixiaki\lib\site-packages\pandas\core\series.py", line 1279, in _maybe_update_cacher
ref._maybe_cache_changed(cacher[0], self, inplace=inplace)
File "D:\wandowsoftware\anaconda\envs\ptixiaki\lib\site-packages\pandas\core\frame.py", line 3950, in _maybe_cache_changed
self._mgr.iset(loc, arraylike, inplace=inplace)
File "D:\wandowsoftware\anaconda\envs\ptixiaki\lib\site-packages\pandas\core\internals\managers.py", line 1141, in iset
blk.delete(blk_locs)
File "D:\wandowsoftware\anaconda\envs\ptixiaki\lib\site-packages\pandas\core\internals\blocks.py", line 388, in delete
self.values = np.delete(self.values, loc, 0) # type: ignore[arg-type]
File "<__array_function__ internals>", line 5, in delete
File "D:\wandowsoftware\anaconda\envs\ptixiaki\lib\site-packages\numpy\lib\function_base.py", line 4555, in delete
new = arr[tuple(slobj)]
MemoryError: Unable to allocate 501. MiB for an array with shape (12234, 10736) and data type int32

Cause of Tkinter error when displaying matplotlib graphs using PySimpleGUI

I am currently developing a GUI in Python using PySimpleGUI and am working on plotting some graphs in frames contained by the main window. I succeeded in doing this using this example code1 and it looks fine. However, when I close the main window after having plotted a graph, I get the following list of errors related to Tkinter:
Exception in Tkinter callback\
Traceback (most recent call last):\
File "/usr/lib64/python3.6/tkinter/__init__.py", line 1705, in __call__\
return self.func(*args)\
File "/usr/lib64/python3.6/tkinter/__init__.py", line 3072, in set\
self.tk.call(self._w, 'set', first, last)\
_tkinter.TclError: invalid command name\ ".!toplevel.!frame.!notebook.!frame4.!frame4.!tkscrollableframe.!scrollbar2"\
Exception in Tkinter callback\
Traceback (most recent call last):\
File "/usr/lib64/python3.6/tkinter/__init__.py", line 1705, in __call__\
return self.func(*args)\
File "/usr/lib64/python3.6/tkinter/__init__.py", line 3072, in set\
self.tk.call(self._w, 'set', first, last)\
_tkinter.TclError: invalid command name\ ".!toplevel.!frame.!notebook.!frame4.!frame4.!tkscrollableframe.!scrollbar"\
Exception in Tkinter callback\
Traceback (most recent call last):\
File "/usr/lib64/python3.6/tkinter/__init__.py", line 1705, in __call__\
return self.func(*args)\
File "/usr/lib64/python3.6/tkinter/__init__.py", line 3072, in set\
self.tk.call(self._w, 'set', first, last)\
_tkinter.TclError: invalid command name\ ".!toplevel.!frame.!notebook.!frame4.!frame4.!tkscrollableframe.!scrollbar2"\
Exception in Tkinter callback\
Traceback (most recent call last):\
File "/usr/lib64/python3.6/tkinter/__init__.py", line 1705, in __call__\
return self.func(*args)\
File "/usr/lib64/python3.6/tkinter/__init__.py", line 3072, in set\
self.tk.call(self._w, 'set', first, last)\
_tkinter.TclError: invalid command name\ ".!toplevel.!frame.!notebook.!frame4.!frame4.!tkscrollableframe.!scrollbar"\
I am new to matplotlib and haven't used Tkinter before, but would like to know the cause of these errors, if anyone knows. My code is quite big, but I can add it to the post if necessary.
I have tried commenting different parts of my code to find the culprit and when I remove the line corresponding to the following in the demo I used, the errors stopped:
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
Have anyone experienced this problem before? I am running this on a machine running Centos and have tried running a simpler program in Windows, which closes without any problem.
Thanks in advance.
EDIT 1: Upon request I have just added the simplest Python code I could produce that reproduces this kind of error:
import PySimpleGUI as sg
import matplotlib as mp
from matplotlib import figure
from matplotlib.backends import backend_tkagg
import numpy as np
layout1 = [
[
sg.Frame("Frame",
[
[
sg.Column(
[
[sg.Canvas(key="-CANVAS-")]
],
size=(1000,1000)
)
]
]
)
]
]
layout2 = [
[sg.Column(layout1, scrollable=True)]
]
window = sg.Window("Window", layout2)
window.finalize()
x = np.array(list(range(10)))
fig = figure.Figure()
ax = fig.add_axes((0,0,1,1))
ax.plot(x, x)
figure_canvas_tkagg = backend_tkagg.FigureCanvasTkAgg(fig, master=window["-CANVAS-"].TKCanvas)
figure_canvas_tkagg.draw()
figure_canvas_tkagg.get_tk_widget().pack()
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
window.close()
It appears that it might have something to do with the fact that my GUI code consists of many levels of container-objects within cointainer-objects. In my full code I have a couple of layers on top of this, but the error seemed to appear once I made the following hierarchy:
Column
Frame
Column
Canvas
It should be noted that using the debugger of Visual Studio Code I have found out that the error does not occur exactly when the window closes (when the window.close() method is called), but some time before.
EDIT 2: I just tested it and for some reason this error only occurs when I run the attached Python script on my Centos machine and not in Windows.
EDIT 3: I found out that if I close all the figures I have created before I close the window, these errors will not occur. This is not a complete fix however, since the error occur before the window.close() command of PySimpleGUI is run, so I don't know where to put the code that would close the figures in case the window is closed.

Match projection of shapefile in cartopy

I am trying to make a Choropleth map using matplotlib and cartopy for which I obviously need to plot a shapefile first. However, I did not manage to do so, even though a similar question has been asked here and here. I suspect either the projection or the bounds to be misspecified.
My shapefile has the projection
PROJCS["WGS_1984_UTM_Zone_32Nz",
GEOGCS["GCS_WGS_1984",
DATUM["WGS_1984",
SPHEROID["WGS_84",6378137,298.257223563]],
PRIMEM["Greenwich",0],
UNIT["Degree",0.017453292519943295]],
PROJECTION["Transverse_Mercator"],
PARAMETER["False_Easting",32500000],
PARAMETER["False_Northing",0],
PARAMETER["Central_Meridian",9],
PARAMETER["Scale_Factor",0.9996],
PARAMETER["Latitude_Of_Origin",0],
UNIT["Meter",1]]
and can be downloaded here where I am talking about vg250_2010-01-01.utm32w.shape.ebenen/vg250_ebenen-historisch/de1001/vg250_gem.shp
My code is
#!/usr/local/bin/python
# -*- coding: utf8 -*-
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
import matplotlib.pyplot as plt
fname = 'path/vg250_gem.shp'
proj = ccrs.TransverseMercator(central_longitude=0.0,central_latitude=0.0,
false_easting=32500000.0,false_northing=0.0,
scale_factor=0.9996)
municipalities = list(shpreader.Reader(fname).geometries())
ax = plt.axes(projection=proj)
plt.title('Deutschland')
ax.add_geometries(municipalities,proj,edgecolor='black',facecolor='gray',alpha=0.5)
ax.set_extent([32458044.649189778*0.9, 5556418.748046352*1.1, 32465287.307457082*0.9, 5564153.5456742775*1.1],proj)
plt.show()
where I obtained the bounds using the corresponding method from fiona. Python throws an error
Traceback (most recent call last):
File "***/src/analysis/test.py", line 16, in <module>
ax.set_extent([32458044.649189778, 5556418.748046352, 32465287.307457082, 5564153.5456742775],proj)
File "/usr/local/lib/python2.7/site-packages/cartopy/mpl/geoaxes.py", line 652, in set_extent
ylim=self.projection.y_limits))
ValueError: Failed to determine the required bounds in projection coordinates. Check that the values provided are within the valid range (x_limits=[-20000000.0, 20000000.0], y_limits=[-10000000.0, 10000000.0]).
[Finished in 53.9s with exit code 1]
This doesn't make sense to me. Also, experimenting with ccrs.UTM() gives me a plot showing a white area. I'd appreciate it if anyone can tell me how to fix this. Thank you!
I have found two issues. One is an incorrect specification of limits in your call to set_extent, the documentation specifies [x0,x1,y0,y1] should be the input, you seem to have given [x0,y0,x1,y1].
The other issue seems to be a limitation in cartopy, as best I can tell. It looks like projections outside the limits listed in the error message will always fail, and those limits are hardcoded. You can just edit the source (this line in their latest release), changing -2e7 to -4e7, likewise for the upper bound. After these fixes, your plot is generated without issue:
The new set_extent line:
ax.set_extent([32458044.649189778*0.975, 32465287.307457082*1.025,5556418.748046352*0.9, 556415,3.5456742775*1.1],proj)
You may also want to set central_longitude=9.0 in your TransverseMercator, that seems to be what's specified in your shapefile.
I would recommend contacting the developers about this, they might have a good reason for setting those bounds, or they might have a better workaround, or maybe they'll widen the bounds in a later release!
Update
Your bounds also seem to have been set based on only the first of the municipalities:
In [34]: municipalities[0].bounds
Out[34]: (32458044.649189778, 5556418.748046352, 32465287.307457082, 5564153.5456742775)
But the other elements have different bounds. You can get limits flushed to the actual drawing based on min/max values of the bounds of all municipalities.
bnd = np.array([i.bounds for i in municipalities])
x0,x1 = np.min(bnd[:,0]),np.max(bnd[:,2])
y0,y1 = np.min(bnd[:,1]),np.max(bnd[:,3])
ax.set_extent([x0,x1,y0,y1],proj)

Python Matplotlib errors with savefig (newbie).

All parts of Python on my computer were recently installed from the Enthought academic package, but use Pyscripter for editing and running code. I'm very early in my learning curve, and so could very well be overlooking some obvious things here.
When I try to create a plot and save it like so:
import matplotlib.pylab as pl
pl.hist(myEst, bins=20, range=(.1,.60))
pl.ylabel("Freq")
pl.xlabel("Success Probability")
pl.title('Histogram of Binomial Estimator')
pl.axis([0, 1, 0, 500])
pl.vlines (.34,0,500)
pl.savefig('TestHist.png')
pl.show()
I get these errors:
Traceback (most recent call last):
File "<editor selection>", line 9, in <module>
File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 1172, in savefig
self.canvas.print_figure(*args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_wxagg.py", line 100, in print_figure
FigureCanvasAgg.print_figure(self, filename, *args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 2017, in print_figure
**kwargs)
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.py", line 450, in print_png
filename_or_obj = file(filename_or_obj, 'wb')
IOError: [Errno 13] Permission denied: 'TestHist.png'
If I take out the pl.savefig('TestHist') line everything works fine, and I can see the plot I want, but when that line is in there I get the errors.
I've checked my backend version using pl.get_backend(), it returns 'WXAgg', which according to documentation should be able to use .png format.
I've also tried including an explicit format='png' and format=png within the savefig command, but still get errors.
Can anyone give me advice on how to proceed, or another approach for saving a plot?
There's nothing wrong with your code. I just tested it locally on my machine. The issue is this error:
IOError: [Errno 13] Permission denied: 'TestHist.png'
You are most likely trying to save the file somewhere that the Python process doesn't have permission to access. What OS are you on? Where are you trying to save the file?
If it helps others, I made the silly mistake of not actually designating a file name and as a result had returned the same error message that lead me to this question for review.
Here is the code that was generating the error:
plt.savefig('C:\\Users\\bwarn\\Canopy', format='png')
Here is my correction that resolved (you'll see I designated the actual file and name)
plt.savefig('C:\\Users\\bwarn\\Canopy\\myplot.png', format='png')
The following worked for me when I was running a neural network on my windows machine:
image_path = 'A:/DeepLearning/Padhai/MLFlow/images/%s.png' % (expt_id)
plt.savefig(image_path)
Or otherwise refer:
Using 'r' in front of the path