How to retrieve conflicting location in shapely TopologicalError? - python

When trying to plot a 2D array in cartopy projected axes using contourf, I am receiving a TopologicalError. This is the code:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np
from shapely.geos import TopologicalError
data = np.array(
[[ 8, 10, 2, 5, 3, 6, 5, 3, 0, 0, 0, 0, 0, 0, 0],
[ 7, 5, 3, 6, 5, 4, 2, 1, 1, 0, 0, 0, 0, 0, 0],
[ 8, 10, 2, 7, 1, 2, 1, 0, 0, 1, 0, 0, 0, 0, 0],
[14, 8, 12, 9, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[15, 8, 10, 5, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 7, 7, 9, 5, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[ 4, 5, 1, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 2, 4, 1, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 2, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
])
x = np.array([-9.05365, -9.05266, -9.05167, -9.05068, -9.04969,
-9.0487, -9.04771, -9.04672, -9.04573, -9.04474,
-9.04375, -9.04276, -9.04177, -9.04078, -9.03979])
y = np.array([53.2024, 53.203, 53.2036, 53.2042, 53.2048,
53.2054, 53.206, 53.2065, 53.2071, 53.2077])
data = np.ma.masked_where(data==0, data)
ax = plt.axes(projection=ccrs.Mercator(central_longitude=-9))
try:
ax.contourf(x, y, data, transform=ccrs.PlateCarree())
except TopologicalError as exc:
pass # Do something to get the conflicting location (x, y)
When running this code, I am getting the TopologyException message below, followed by a rather long traceback (see below). The TopologyException is:
TopologyException: side location conflict at -9.0506799999999998 53.206000000000003
Now, under the except block, I would like to fix the array in the location indicated by the TopologyException (-9.0506799999999998 53.206000000000003), and try to plot it again. I would use a while loop to fix the array as many times as needed, retrieving the conflicting location and fixing the array at that location. I have to repeat this procedure for many other arrays like this.
Even though the message is displayed in the screen, I don't know how to get the location -9.0506799999999998 53.206000000000003 from the script, since I can't find it in exc. I would be very grateful if somebody could explain how to get those numbers as new variables lon and lat.
This error can be avoided if not using a masked array, but I am masking the 0's because I don't want to plot them.
Below is the traceback. Many thanks.
TopologyException: side location conflict at -9.0506799999999998 53.206000000000003
TopologyException: side location conflict at -9.0506799999999998 53.206000000000003
Error in callback <function install_repl_displayhook.<locals>.post_execute at 0x00000204E9F74280> (for post_execute):
Traceback (most recent call last):
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\shapely\predicates.py", line 15, in __call__
return self.fn(this._geom, other._geom, *args)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\shapely\geos.py", line 584, in errcheck_predicate
raise PredicateError("Failed to evaluate %s" % repr(func))
PredicateError: Failed to evaluate <_FuncPtr object at 0x00000204EB13ED40>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\pyplot.py", line 139, in post_execute
draw_all()
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\_pylab_helpers.py", line 137, in draw_all
manager.canvas.draw_idle()
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\backend_bases.py", line 2055, in draw_idle
self.draw(*args, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\backends\backend_agg.py", line 406, in draw
self.figure.draw(self.renderer)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\artist.py", line 74, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\figure.py", line 2790, in draw
mimage._draw_list_compositing_images(
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\cartopy\mpl\geoaxes.py", line 558, in draw
return matplotlib.axes.Axes.draw(self, renderer=renderer, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\_api\deprecation.py", line 431, in wrapper
return func(*inner_args, **inner_kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\axes\_base.py", line 2921, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\collections.py", line 1012, in draw
super().draw(renderer)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\collections.py", line 352, in draw
transform, transOffset, offsets, paths = self._prepare_points()
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\collections.py", line 329, in _prepare_points
paths = [transform.transform_path_non_affine(path)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\collections.py", line 329, in <listcomp>
paths = [transform.transform_path_non_affine(path)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\transforms.py", line 2396, in transform_path_non_affine
return self._a.transform_path_non_affine(path)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\cartopy\mpl\geoaxes.py", line 186, in transform_path_non_affine
geoms = cpatch.path_to_geos(src_path,
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\cartopy\mpl\patch.py", line 178, in path_to_geos
collection[-1][0].contains(geom.exterior)):
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\shapely\geometry\base.py", line 747, in contains
return bool(self.impl['contains'](self, other))
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\shapely\predicates.py", line 18, in __call__
self._check_topology(err, this, other)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\shapely\topology.py", line 35, in _check_topology
raise TopologicalError(
TopologicalError: The operation 'GEOSContains_r' could not be performed. Likely cause is invalidity of the geometry <shapely.geometry.polygon.Polygon object at 0x000002048290D820>
TopologyException: side location conflict at -9.0506799999999998 53.206000000000003
Traceback (most recent call last):
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\shapely\predicates.py", line 15, in __call__
return self.fn(this._geom, other._geom, *args)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\shapely\geos.py", line 584, in errcheck_predicate
raise PredicateError("Failed to evaluate %s" % repr(func))
PredicateError: Failed to evaluate <_FuncPtr object at 0x00000204EB13ED40>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\IPython\core\formatters.py", line 341, in __call__
return printer(obj)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\IPython\core\pylabtools.py", line 253, in <lambda>
png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\IPython\core\pylabtools.py", line 137, in print_figure
fig.canvas.print_figure(bytes_io, **kw)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\backend_bases.py", line 2230, in print_figure
self.figure.draw(renderer)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\artist.py", line 74, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\figure.py", line 2790, in draw
mimage._draw_list_compositing_images(
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\cartopy\mpl\geoaxes.py", line 558, in draw
return matplotlib.axes.Axes.draw(self, renderer=renderer, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\_api\deprecation.py", line 431, in wrapper
return func(*inner_args, **inner_kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\axes\_base.py", line 2921, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\collections.py", line 1012, in draw
super().draw(renderer)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\collections.py", line 352, in draw
transform, transOffset, offsets, paths = self._prepare_points()
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\collections.py", line 329, in _prepare_points
paths = [transform.transform_path_non_affine(path)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\collections.py", line 329, in <listcomp>
paths = [transform.transform_path_non_affine(path)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\matplotlib\transforms.py", line 2396, in transform_path_non_affine
return self._a.transform_path_non_affine(path)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\cartopy\mpl\geoaxes.py", line 186, in transform_path_non_affine
geoms = cpatch.path_to_geos(src_path,
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\cartopy\mpl\patch.py", line 178, in path_to_geos
collection[-1][0].contains(geom.exterior)):
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\shapely\geometry\base.py", line 747, in contains
return bool(self.impl['contains'](self, other))
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\shapely\predicates.py", line 18, in __call__
self._check_topology(err, this, other)
File "C:\Users\dpereiro\Miniconda3\envs\opendrift\lib\site-packages\shapely\topology.py", line 35, in _check_topology
raise TopologicalError(
TopologicalError: The operation 'GEOSContains_r' could not be performed. Likely cause is invalidity of the geometry <shapely.geometry.polygon.Polygon object at 0x00000204E87FDD90>
<Figure size 432x288 with 1 Axes>

Related

Conflict between matplotlib and pandas

When I run the following code below I get the below error message. The error does not occur when I comment the "pd.read_csv" line. So it seems there is a conflict between matplotlib (3.5.1) and pandas (1.4.1) but no clue what it could be. I use Pycharm Community 2021.3.2. Any ideas?
import matplotlib.pyplot as plt
import pandas as pd
f = open("mycsv.csv", "w")
f.write("0,1,2,3,4\n1,4,2,5,3")
f.close()
pd.read_csv("mycsv.csv",sep=",") # Commenting this line removes the error
data = [[0, 1, 2, 3, 4], [1, 4, 2, 5, 3]]
plt.plot( data[0], data[1])
plt.show()
Error message:
C:\Users\woute\AppData\Local\Programs\Python\Python310\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2021.3.2\plugins\python-ce\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 55165 --file C:/Users/woute/Documents/GitHub/GolfSwing/PoseDemoWebcam/Test.py
Connected to pydev debugger (build 213.6777.50)
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\getlimits.py", line 459, in __new__
dtype = numeric.dtype(dtype)
TypeError: 'NoneType' object is not callable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 839, in callit
func(*args)
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\backends\_backend_tk.py", line 251, in idle_draw
self.draw()
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
super().draw()
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\backends\backend_agg.py", line 436, in draw
self.figure.draw(self.renderer)
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\artist.py", line 73, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
return draw(artist, renderer)
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\figure.py", line 2810, in draw
mimage._draw_list_compositing_images(
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
return draw(artist, renderer)
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 3020, in draw
self._unstale_viewLim()
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 776, in _unstale_viewLim
self.autoscale_view(**{f"scale{name}": scale
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 2932, in autoscale_view
handle_single_axis(
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 2895, in handle_single_axis
x0, x1 = locator.nonsingular(x0, x1)
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\ticker.py", line 1654, in nonsingular
return mtransforms.nonsingular(v0, v1, expander=.05)
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\transforms.py", line 2880, in nonsingular
if maxabsvalue < (1e6 / tiny) * np.finfo(float).tiny:
File "C:\Users\woute\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\getlimits.py", line 462, in __new__
dtype = numeric.dtype(type(dtype))
TypeError: 'NoneType' object is not callable

Unable to save Figure in iPython Notebook throwing KeyError

I am trying to save an eps file using savefig method of matplotlib, but I am getting an unusual exception (KeyError), and the message isn't actionable. Let me know what can be done thanks.
Regards,
Harsh
data_for_x_axis = [0,30,60,90,120,150,180]
data_for_y_axis = [0, 0.1, 0.2, 0.3, 0.2, 0.1, 0]
AX1 = plt.subplot(221)
plt.margins(1)
AX1.cla()
AX1.plot(data_for_x_axis, data_for_y_axis) # some data
AX1.set_xlim([0,180])
AX1.set_xticks([0,30,60,90,120,150,180])
AX1.set_yticks([0.5,1])
AX1.set_title(r'Normalized Stokes $I$')
AX1.set_xlabel('CQWP Rotation Angle (Degrees)')
AX1.set_ylabel(r'$I/I_{mean}$')
plt.savefig('.\\Plots\\4_ModDemod.eps', format='eps', dpi=2400)
This throws the following exception
xception in Tkinter callback
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/backends/_backend_tk.py", line 256, in resize
self._tkcanvas.create_image(
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/backends/backend_tkagg.py", line 9, in draw
super(FigureCanvasTkAgg, self).draw()
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 388, in draw
self.figure.draw(self.renderer)
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/figure.py", line 1709, in draw
renderer, self, artists, self.suppressComposite)
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/image.py", line 135, in _draw_list_compositing_images
a.draw(renderer)
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 2605, in draw
artists.remove(spine)
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 2554, in _update_title_position
# this happens for an empty bb
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/text.py", line 890, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/text.py", line 298, in _get_layout
clean_line, self._fontproperties, ismath=ismath)
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 206, in get_text_width_height_descent
self.mathtext_parser.parse(s, self.dpi, prop)
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/mathtext.py", line 3362, in parse
font_output = fontset_class(prop, backend)
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/mathtext.py", line 883, in __init__
self.cm_fallback = StixSansFonts(*args, **kwargs)
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/mathtext.py", line 984, in __init__
fullpath = findfont(name)
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/font_manager.py", line 1238, in findfont
rc_params)
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/font_manager.py", line 1270, in _findfont_cached
+ self.score_size(prop.get_size(), font.size))
File "/Volumes/Elements/SSP Data Reduction/env/lib/python3.7/site-packages/matplotlib/font_manager.py", line 1151, in score_weight
w1 = weight1 if isinstance(weight1, Number) else weight_dict[weight1]
KeyError: '1'

How to plot 3D hist in python

I have dataset that contains accidents in a region with a year.
> Accident_ID Region Year
> 213 1 2003
> 234 2 2001
> 334 2 2004
> ..
years= [0.0, 1661.0, 1665.0, 1706.0, 1729.0, 1765.0, 1779.0, 1780.0, 1785.0, 1798.0, 1799.0, 1800.0, 1801.0, 1802.0, 1804.0, 1805.0, 1812.0, 1814.0, 1816.0, 1821.0, 1822.0, 1824.0, 1825.0, 1826.0, 1827.0, 1829.0, 1830.0, 1831.0, 1832.0, 1833.0, 1834.0, 1835.0, 1836.0, 1837.0, 1838.0, 1839.0, 1840.0, 1841.0, 1842.0, 1843.0, 1844.0, 1845.0, 1846.0, 1847.0, 1848.0, 1849.0, 1850.0, 1851.0, 1852.0, 1853.0, 1854.0, 1855.0, 1856.0, 1857.0, 1858.0, 1859.0, 1860.0, 1861.0, 1862.0, 1863.0, 1864.0, 1865.0, 1866.0, 1867.0, 1868.0, 1869.0, 1870.0, 1871.0, 1872.0, 1873.0, 1874.0, 1875.0, 1876.0, 1877.0, 1878.0, 1879.0, 1880.0, 1881.0, 1882.0, 1883.0, 1884.0, 1885.0, 1886.0, 1887.0, 1888.0, 1889.0, 1890.0, 1891.0, 1892.0, 1893.0, 1894.0, 1895.0, 1896.0, 1897.0, 1898.0, 1899.0, 1900.0, 1901.0, 1902.0, 1903.0, 1904.0, 1905.0, 1906.0, 1907.0, 1908.0, 1909.0, 1910.0, 1911.0, 1912.0, 1913.0, 1914.0, 1915.0, 1916.0, 1917.0, 1918.0, 1919.0, 1920.0, 1921.0, 1922.0, 1923.0, 1924.0, 1925.0, 1926.0, 1927.0, 1928.0, 1929.0, 1930.0, 1931.0, 1932.0, 1933.0, 1934.0, 1935.0, 1936.0, 1937.0, 1938.0, 1939.0, 1940.0, 1941.0, 1942.0, 1943.0, 1944.0, 1945.0, 1946.0, 1947.0, 1948.0, 1949.0, 1950.0, 1951.0, 1952.0, 1953.0, 1954.0, 1955.0, 1956.0, 1957.0, 1958.0, 1959.0, 1960.0, 1961.0, 1962.0, 1963.0, 1964.0, 1965.0, 1966.0, 1967.0, 1968.0, 1969.0, 1970.0, 1971.0, 1972.0, 1973.0, 1974.0, 1975.0, 1976.0, 1977.0, 1978.0, 1979.0, 1980.0, 1981.0, 1982.0, 1983.0, 1984.0, 1985.0, 1986.0, 1987.0, 1988.0, 1989.0, 1990.0, 1991.0, 1992.0, 1993.0, 1994.0, 1995.0, 1996.0, 1997.0, 1998.0, 1999.0, 2000.0, 2001.0, 2002.0, 2003.0, 2004.0, 2005.0, 2006.0, 2007.0, 2008.0, 2009.0, 2010.0, 2011.0, 2012.0, 2013.0]
Frequency_accidents_years= [44815, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 556, 1, 1, 1, 1, 1, 1, 1, 1, 3, 4, 2, 5, 3, 20, 11, 6, 5, 1, 7, 6, 6, 2, 4, 6, 19, 9, 11, 10, 18, 18, 8, 9, 13, 20, 43, 7, 13, 11, 6, 13, 12, 6, 7, 9, 34, 3, 2, 3, 1, 7, 6, 4, 8, 11, 56, 18, 5, 4, 4, 16, 2, 1, 3, 3, 146, 49, 10, 7, 10, 22, 18, 14, 18, 17, 397, 46, 12, 14, 12, 53, 39, 18, 28, 25095, 9663, 26717, 131, 180, 268, 7660, 754, 641, 354, 873, 47024, 705, 720, 578, 598, 16547, 653, 516, 255, 296, 92079, 1161, 1175, 1634, 2111, 71121, 3158, 3289, 4355, 2136, 77654, 33007, 1253, 983, 365, 25554, 651, 665, 762, 968, 38485, 745, 326, 199, 176, 25048, 343, 368, 604, 753, 46674, 775, 683, 562, 645, 26992, 768, 959, 816, 922, 37271, 796, 915, 1101, 945, 19687, 618, 614, 620, 509, 17169, 497, 623, 853, 854, 9755, 662, 725, 999, 593, 5469, 554, 778, 1163, 1342, 3470, 3755, 3810, 3597, 3613, 3504, 2263, 3173, 2465, 2135, 2558, 3476, 3164, 2755, 3715, 4187, 4540, 4203, 4445, 6541, 5994, 4873, 4085, 2899, 1806, 1157, 1331, 1246, 424]
regions = xrange(1,100) // Can be generated this way eg: region1, region2 ..
I wanna plot these data in a 3D histogram to better analyse the dataset.
I wanna plot frequency of accident via region/Year
from collections import Counter
data.pandas("file.csv")
..
..
#Make 3D Hist
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot(xs=years, ys=Frequency_accidents_years, zs=regions, marker='o', linestyle='--', color='r',label="name")
plt.show()
I ended up having this problem.
I got this error:
line 49 ..
File "/Users/macbook/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 561, in savefig
return fig.savefig(*args, **kwargs)
File "/Users/macbook/anaconda/lib/python2.7/site-packages/matplotlib/figure.py", line 1421, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/Users/macbook/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 2220, in print_figure
**kwargs)
File "/Users/macbook/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 1962, in print_png
return agg.print_png(*args, **kwargs)
File "/Users/macbook/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 505, in print_png
FigureCanvasAgg.draw(self)
File "/Users/macbook/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 451, in draw
self.figure.draw(self.renderer)
File "/Users/macbook/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/Users/macbook/anaconda/lib/python2.7/site-packages/matplotlib/figure.py", line 1034, in draw
func(*args)
File "/Users/macbook/anaconda/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 270, in draw
Axes.draw(self, renderer)
File "/Users/macbook/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/Users/macbook/anaconda/lib/python2.7/site-packages/matplotlib/axes.py", line 2086, in draw
a.draw(renderer)
File "/Users/macbook/anaconda/lib/python2.7/site-packages/mpl_toolkits/mplot3d/art3d.py", line 117, in draw
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
File "/Users/macbook/anaconda/lib/python2.7/site-packages/mpl_toolkits/mplot3d/proj3d.py", line 194, in proj_transform
return proj_transform_vec(vec, M)
File "/Users/macbook/anaconda/lib/python2.7/site-packages/mpl_toolkits/mplot3d/proj3d.py", line 153, in proj_transform_vec
vecw = np.dot(M, vec)
ValueError: operands could not be broadcast together with shapes (210) (858312)
I've tried to come up with an example of how to plot a Matplotlib bar3d 3d histogram given your dataset structure using Panda's groupby as pointed out by Davidmh.
I used the following dataset based in what you posted above (accidents.csv):
Accident_ID,Region,Year
213,1,2003
214,1,2003
214,2,2008
213,2,2007
210,2,2007
210,3,2004
210,1,2004
213,1,2004
210,1,2004
This script reads the dataset, groups the data and builds a 3d histogram plot:
import matplotlib
import matplotlib.pyplot as plt
from pandas import read_csv
from mpl_toolkits.mplot3d import Axes3D
# Read CSV dataset file
df = read_csv('accidents.csv')
# Group by year and region
group_year_region = df.groupby(['Year', 'Region'])
group_keys = group_year_region.groups.keys()
# Get the years and regions series
xpos = map(lambda k: k[0] - 0.5, group_keys)
ypos = map(lambda k: k[1] - 0.5, group_keys)
zpos = [0] * len(xpos)
# Count number of accidents by (year, region) group
acc_by_year_region = group_year_region.count()['Accident_ID']
dx = 1
dy = 1
dz = [acc_by_year_region[key] for key in group_keys]
# Plot bar3d histogram
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')
# Years should not be presented in exponential notation
x_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
ax.xaxis.set_major_formatter(x_formatter)
# Set labels and show plot
ax.set_xticks([k[0] for k in group_keys])
ax.set_yticks([k[1] for k in group_keys])
ax.set_zticks(dz)
ax.set_xlabel('Years')
ax.set_ylabel('Regions')
ax.set_zlabel('# Accidents')
plt.show()
The plot looks like this:
EDIT
There was a problem with the way the axes were built and the values were unordered. It is fixed now and the plot image has been updated.
The problem is that the shape of your data does not add up. You have three vectors, and they have to have the same length.
Running your example, years and Frequency_... have both 210 elements, but regions have only 99. I think what you want to do is to filter the accidents by region, for what you can use Panda's groupby.

Attribute error when using simple plotting script

I am trying to plot a simple function in Python. I use the following code:
# lineplot.py
import numpy as np
import pylab as pl
# Make an array of x values
x = [1, 2, 3, 4, 5]
# Make an array of y values for each x value
y = [1, 4, 9, 16, 25]
# use pylab to plot x and y
pl.plot(x, y)
# show the plot on the screen
pl.show()
However, the last line in which I want to see the output results in the following error:
AttributeError: 'NoneType' object has no attribute 'get_default_size'
Does anybody have any idea where I'm going wrong?
Full traceback of the error:
>>> pl.show()
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 394,\ in expose_event
self._render_figure(self._pixmap, w, h)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_figure
FigureCanvasAgg.draw(self)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 394, in draw
self.figure.draw(self.renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 798, in draw
func(*args)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 1946, in draw
a.draw(renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 1017, in draw
tick.draw(renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 234, in draw
self.label1.draw(renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 526, in draw
bbox, info = self._get_layout(renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 300, in _get_layout
ismath=False)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 180, in get_text_width_height_descent
font = self._get_agg_font(prop)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 226, in _get_agg_font
size = prop.get_size_in_points()
File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 798, in get_size_in_points
default_size = fontManager.get_default_size()
AttributeError: 'NoneType' object has no attribute 'get_default_size'
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 394, in expose_event
self._render_figure(self._pixmap, w, h)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_figure
FigureCanvasAgg.draw(self)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 394, in draw
self.figure.draw(self.renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 798, in draw
func(*args)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 1946, in draw
a.draw(renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 1017, in draw
tick.draw(renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 234, in draw
self.label1.draw(renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 526, in draw
bbox, info = self._get_layout(renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 300, in _get_layout
ismath=False)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 180, in get_text_width_height_descent
font = self._get_agg_font(prop)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 226, in _get_agg_font
size = prop.get_size_in_points()
File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 798, in get_size_in_points
default_size = fontManager.get_default_size()
AttributeError: 'NoneType' object has no attribute 'get_default_size'
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 394, in expose_event
self._render_figure(self._pixmap, w, h)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_figure
FigureCanvasAgg.draw(self)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 394, in draw
self.figure.draw(self.renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 798, in draw
func(*args)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 1946, in draw
a.draw(renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 1017, in draw
tick.draw(renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 234, in draw
self.label1.draw(renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 526, in draw
bbox, info = self._get_layout(renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 300, in _get_layout
ismath=False)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 180, in get_text_width_height_descent
font = self._get_agg_font(prop)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 226, in _get_agg_font
size = prop.get_size_in_points()
File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 798, in get_size_in_points
default_size = fontManager.get_default_size()
AttributeError: 'NoneType' object has no attribute 'get_default_size'
The problem was the version (1.0.1) of matplotlib used by the OP. Upgrading it solved the problem (as the discussion in chat clarified).
This is how we solved the problem (it should work on Linux):
Download the latest matplotlib tar file from the download page
extract the tar.gz archive
cd into the matplotlib directory
run
python setup.py build
If it fails complaining about some header file (e.g. png.h) install the relevant -devel packages from the system repository. For sure python-devel and libpng12-devel are needed
run
python setup.py install --user
It will install matplotlib in $HOME/.local/lib/pythonX.X/site-packages. This directory is loaded in the python path before any of the system wide ones. Plus there are no risks of messing up the system wide installations.

"Unexpected SeqBase<T> length" from matplotlib's backend_agg.py

I'm trying to use pylab and networkx to draw a directed-graph.
Below is a small demo script and the error it produces (and, the window created by pylab is empty).
Googling shows that this error was supposed to be fixed in mathplotlib's svn already in 2008.
Anyone could point me in the right direction?
This script:
import pylab
import networkx as nx
def Working_with_graphs_using_networkx():
fig = pylab.figure()
g = nx.Graph(name = "graph")
pylab.title(g.name)
g.add_node("John")
g.add_node("Maria")
g.add_edge("John", "Maria")
pos = nx.spring_layout(g)
nx.draw(g, pos)
if __name__ == "__main__":
Working_with_graphs_using_networkx()
pylab.show()
Produces this error (on Python 2.6 under cygwin):
$ python demo_SeqBase.py
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1410, in __call__
return self.func(*args)
File "/usr/lib/python2.6/site-packages/matplotlib/backends/backend_tkagg.py", line 236, in resize
self.show()
File "/usr/lib/python2.6/site-packages/matplotlib/backends/backend_tkagg.py", line 239, in draw
FigureCanvasAgg.draw(self)
File "/usr/lib/python2.6/site-packages/matplotlib/backends/backend_agg.py", line 401, in draw
self.figure.draw(self.renderer)
File "/usr/lib/python2.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/matplotlib/figure.py", line 884, in draw
func(*args)
File "/usr/lib/python2.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/matplotlib/axes.py", line 1983, in draw
a.draw(renderer)
File "/usr/lib/python2.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/matplotlib/collections.py", line 238, in draw
self._linewidths, self._linestyles, self._antialiaseds, self._urls)
File "/usr/lib/python2.6/site-packages/matplotlib/backends/backend_agg.py", line 84, in draw_path_collection
return self._renderer.draw_path_collection(*kl, **kw)
IndexError: Unexpected SeqBase<T> length.

Categories

Resources