Value Error: x and y must have the same first dimension - python

Let me quickly brief you first, I am working with a .txt file with 5400 data points. Each is a 16 second average over a 24 hour period (24 hrs * 3600 s/hr = 86400...86400/16 = 5400). In short this is the average magnetic strength in the z direction for an inbound particle field curtsy of the Advanced Composition Experiment satellite. Data publicly available here. So when I try to plot it says the error
Value Error: x and y must have the same first dimension
So I created a numpy lin space of 5400 points broken apart by 16 units. I did this because I thought that my dimensions didn't match with my previous array that I had defined. But now I am sure these two array's are of the same dimension and yet it still gives back that Value Error. The code is as follows:
First try (without the linspace):
import numpy as np
import matplotlib as plt
Bz = np.loadtxt(r"C:\Users\Schmidt\Desktop\Project\Data\ACE\MAG\ACE_MAG_Data_20151202_GSM.txt", dtype = bytes).astype(float)
Start_ACE = dt.date(2015,12,2)
Finish_ACE = dt.date(2015,12,2)
dt_Mag = 16
time_Mag = np.arange(Start_ACE, Finish_ACE, dt_Mag)
plt.subplot(3,1,1)
plt.plot(time_Mag, Bz)
plt.title('Bz 2015 12 02')
Second Try (with linspace):
import numpy as np
import matplotlib as plt
Bz = np.loadtxt(r"C:\Users\Schmidt\Desktop\Project\Data\ACE\MAG\ACE_MAG_Data_20151202_GSM.txt", dtype = bytes).astype(float)
Mag_time = np.linspace(0,5399,16, dtype = float)
plt.subplot(3,1,1)
plt.plot(Mag_time, Bz)
plt.title('Bz 2015 12 02')
Other than it being a dimensional problem I don't know what else could be holding back this plotting procedure back.
Full traceback:
ValueError Traceback (most recent call last)
<ipython-input-68-c5dc0bdf5117> in <module>()
1 plt.subplot(3,1,1)
----> 2 plt.plot(Mag_time, Bz)
3 plt.title('Bz 2015 12 02')
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(*args, **kwargs)
3152 ax.hold(hold)
3153 try:
-> 3154 ret = ax.plot(*args, **kwargs)
3155 finally:
3156 ax.hold(washold)
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
1809 warnings.warn(msg % (label_namer, func.__name__),
1810 RuntimeWarning, stacklevel=2)
-> 1811 return func(ax, *args, **kwargs)
1812 pre_doc = inner.__doc__
1813 if pre_doc is None:
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, *args, **kwargs)
1422 kwargs['color'] = c
1423
-> 1424 for line in self._get_lines(*args, **kwargs):
1425 self.add_line(line)
1426 lines.append(line)
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _grab_next_args(self, *args, **kwargs)
384 return
385 if len(remaining) <= 3:
--> 386 for seg in self._plot_args(remaining, kwargs):
387 yield seg
388 return
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
362 x, y = index_of(tup[-1])
363
--> 364 x, y = self._xy_from_xy(x, y)
365
366 if self.command == 'plot':
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _xy_from_xy(self, x, y)
221 y = _check_1d(y)
222 if x.shape[0] != y.shape[0]:
--> 223 raise ValueError("x and y must have same first dimension")
224 if x.ndim > 2 or y.ndim > 2:
225 raise ValueError("x and y can be no greater than 2-D")
ValueError: x and y must have same first dimension

The problem was the selection of array creation. Instead of linspace, I should have used arange.
Mag_time = np.arange(0,86400, 16, dtype = float)

Related

How to convert auxiliary coordinates to dimension coordinates in a cube in iris?

I am pretty new to using NetCDF Jules output files. I am trying to write a routine to create multiple plots visualising the output from my simulations.
After reading in the cubes I have the following format:
filename = '/home/users/---------.nc'
cubelist = iris.load(filename)
cons = iris.Constraint(cube_func=lambda x: x.var_name == 'gpp_gb')
cube = cubelist.extract_cube(cons)
print (cube)
type(cube)
print (cube.shape)
print (cube.ndim)
first_timestep = cube[0,...]
print (first_timestep)
output:
Gridbox gross primary productivity / (kg m-2 s-1) (time: 12; -- : 1; -- : 7247)
Dimension coordinates:
time x - -
Auxiliary coordinates:
latitude - x x
longitude - x x
Cell methods:
mean time
(12, 1, 7247)
3
Gridbox gross primary productivity / (kg m-2 s-1) (-- : 1; -- : 7247)
Auxiliary coordinates:
latitude x x
longitude x x
Scalar coordinates:
time 2009-02-01 00:00:00, bound=(2009-01-01 00:00:00, 2009-02-01 00:00:00)
Cell methods:
mean time
However when i try to map the data with:
qplt.contourf(first_timestep, 50)
I get the following error message:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_2386/1836137079.py in <module>
16
17
---> 18 qplt.contourf(first_timestep, 50)
/opt/jaspy/lib/python3.8/site-packages/iris/quickplot.py in contourf(cube, *args, **kwargs)
206 coords = kwargs.get("coords")
207 axes = kwargs.get("axes")
--> 208 result = iplt.contourf(cube, *args, **kwargs)
209 _label_with_points(cube, result, coords=coords, axes=axes)
210 return result
/opt/jaspy/lib/python3.8/site-packages/iris/plot.py in contourf(cube, *args, **kwargs)
1057 coords = kwargs.get("coords")
1058 kwargs.setdefault("antialiased", True)
-> 1059 result = _draw_2d_from_points("contourf", None, cube, *args, **kwargs)
1060
1061 # Matplotlib produces visible seams between anti-aliased polygons.
/opt/jaspy/lib/python3.8/site-packages/iris/plot.py in _draw_2d_from_points(draw_method_name, arg_func, cube, *args, **kwargs)
495
496 if _can_draw_map(plot_defn.coords):
--> 497 result = _map_common(
498 draw_method_name,
499 arg_func,
/opt/jaspy/lib/python3.8/site-packages/iris/plot.py in _map_common(draw_method_name, arg_func, mode, cube, plot_defn, *args, **kwargs)
1007 axes = kwargs.pop("axes", None)
1008 plotfn = getattr(axes if axes else plt, draw_method_name)
-> 1009 return plotfn(*new_args, **kwargs)
1010
1011
/opt/jaspy/lib/python3.8/site-packages/matplotlib/pyplot.py in contourf(data, *args, **kwargs)
2743 #_copy_docstring_and_deprecators(Axes.contourf)
2744 def contourf(*args, data=None, **kwargs):
-> 2745 __ret = gca().contourf(
2746 *args, **({"data": data} if data is not None else {}),
2747 **kwargs)
/opt/jaspy/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py in wrapper(self, *args, **kwargs)
308
309 kwargs['transform'] = transform
--> 310 return func(self, *args, **kwargs)
311 return wrapper
312
/opt/jaspy/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py in contourf(self, *args, **kwargs)
1506 sub_trans.force_path_ccw = True
1507
-> 1508 result = matplotlib.axes.Axes.contourf(self, *args, **kwargs)
1509
1510 # We need to compute the dataLim correctly for contours.
/opt/jaspy/lib/python3.8/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
1359 def inner(ax, *args, data=None, **kwargs):
1360 if data is None:
-> 1361 return func(ax, *map(sanitize_sequence, args), **kwargs)
1362
1363 bound = new_sig.bind(ax, *args, **kwargs)
/opt/jaspy/lib/python3.8/site-packages/matplotlib/axes/_axes.py in contourf(self, *args, **kwargs)
6432 def contourf(self, *args, **kwargs):
6433 kwargs['filled'] = True
-> 6434 contours = mcontour.QuadContourSet(self, *args, **kwargs)
6435 self._request_autoscale_view()
6436 return contours
/opt/jaspy/lib/python3.8/site-packages/matplotlib/contour.py in __init__(self, ax, levels, filled, linewidths, linestyles, hatches, alpha, origin, extent, cmap, colors, norm, vmin, vmax, extend, antialiased, nchunk, locator, transform, *args, **kwargs)
775 self._transform = transform
776
--> 777 kwargs = self._process_args(*args, **kwargs)
778 self._process_levels()
779
/opt/jaspy/lib/python3.8/site-packages/matplotlib/contour.py in _process_args(self, corner_mask, *args, **kwargs)
1364 self._corner_mask = corner_mask
1365
-> 1366 x, y, z = self._contour_args(args, kwargs)
1367
1368 _mask = ma.getmask(z)
/opt/jaspy/lib/python3.8/site-packages/matplotlib/contour.py in _contour_args(self, args, kwargs)
1422 args = args[1:]
1423 elif Nargs <= 4:
-> 1424 x, y, z = self._check_xyz(args[:3], kwargs)
1425 args = args[3:]
1426 else:
/opt/jaspy/lib/python3.8/site-packages/matplotlib/contour.py in _check_xyz(self, args, kwargs)
1452 raise TypeError(f"Input z must be 2D, not {z.ndim}D")
1453 if z.shape[0] < 2 or z.shape[1] < 2:
-> 1454 raise TypeError(f"Input z must be at least a (2, 2) shaped array, "
1455 f"but has shape {z.shape}")
1456 Ny, Nx = z.shape
TypeError: Input z must be at least a (2, 2) shaped array, but has shape (1, 7247)
I think it could be because my coordinate data is not in a format the iris package can understand as it is in the auxiliary fields when i print the cube. I think the code should work if I could put the latitude and longitude data into the dimension coordinates. Any help or ideas would be much appreciated.
Thank you for considering the problem.
some functions to which helped my specific problem.
https://code.metoffice.gov.uk/trac/utils/browser/smstress_jpeg/trunk/jules.py#L629

Why am I getting 'x and y must have same first dimension, but have shapes (1,) and (319,)' when the x and y shapes are same?

I am using the lmfit package to fit a double exponential function to my data. I am first taking in the x and y coordinate values from a CSV file and plotting them. It works fine. Secondly, I am defining a double_exponential function, and using the lmfit package, I am plotting the double_exponential function against my data.
I have printed out the length of my x and y values (length=319) and also its shapes to see if they match (319,). They do!. But I am still getting an 'x and y must have the same first dimension, but have shapes (1,) and (319,) ' error. I have attached the code below. Any suggestions in resolving this problem would help.
xData=[]
yData=[]
for file in files:
basename = os.path.basename(file)
file_name = os.path.splitext(basename)[0]
# print(file_name)
#File_List.append(file_name)
with open(file, "r") as f_in:
reader = csv.reader(f_in)
next(reader)
next(reader)
for line in reader:
try:
float_1, float_2 = float(line[0]), float(line[1])
xData=np.append(xData,float_1)
yData=np.append(yData,float_2)
except ValueError:
continue
xData = np.array(xData)
yData = np.array(yData)
plt.plot(xData,yData)
plt.show()
def double_exp(a,t,T):
return a*np.exp(-t/T)
print(xData.shape)
print(yData.shape)
mod = Model(double_exp)
result = mod.fit(yData,x=xData, a=1, t=1, T=1)
result.plot()
xData=[]
yData=[]
My traceback error is:
(319,)
(319,)
C:\ANACONDA\lib\site-packages\lmfit\model.py:968: UserWarning: The keyword argument x does not match any arguments of the model function. It will be ignored.
warnings.warn("The keyword argument %s does not " % name +
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-29-085a1a8ec72a> in <module>
35 mod = Model(double_exp)
36 result = mod.fit(yData,x=xData, a=1, t=1, T=1)
---> 37 result.plot()
38
39
C:\ANACONDA\lib\site-packages\lmfit\model.py in wrapper(*args, **kws)
48 #wraps(function)
49 def wrapper(*args, **kws):
---> 50 return function(*args, **kws)
51 return wrapper
52
C:\ANACONDA\lib\site-packages\lmfit\model.py in plot(self, datafmt, fitfmt, initfmt, xlabel, ylabel, yerr, numpoints, fig, data_kws, fit_kws, init_kws, ax_res_kws, ax_fit_kws, fig_kws, show_init, parse_complex)
2115 ax_fit = fig.add_subplot(gs[1], sharex=ax_res, **ax_fit_kws)
2116
-> 2117 self.plot_fit(ax=ax_fit, datafmt=datafmt, fitfmt=fitfmt, yerr=yerr,
2118 initfmt=initfmt, xlabel=xlabel, ylabel=ylabel,
2119 numpoints=numpoints, data_kws=data_kws,
C:\ANACONDA\lib\site-packages\lmfit\model.py in wrapper(*args, **kws)
48 #wraps(function)
49 def wrapper(*args, **kws):
---> 50 return function(*args, **kws)
51 return wrapper
52
C:\ANACONDA\lib\site-packages\lmfit\model.py in plot_fit(self, ax, datafmt, fitfmt, initfmt, xlabel, ylabel, yerr, numpoints, data_kws, fit_kws, init_kws, ax_kws, show_init, parse_complex)
1890 fmt=datafmt, label='data', **data_kws)
1891 else:
-> 1892 ax.plot(x_array, reduce_complex(self.data),
1893 datafmt, label='data', **data_kws)
1894
C:\ANACONDA\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
1741 """
1742 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1743 lines = [*self._get_lines(*args, data=data, **kwargs)]
1744 for line in lines:
1745 self.add_line(line)
C:\ANACONDA\lib\site-packages\matplotlib\axes\_base.py in __call__(self, data, *args, **kwargs)
271 this += args[0],
272 args = args[1:]
--> 273 yield from self._plot_args(this, kwargs)
274
275 def get_next_color(self):
C:\ANACONDA\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
397
398 if x.shape[0] != y.shape[0]:
--> 399 raise ValueError(f"x and y must have same first dimension, but "
400 f"have shapes {x.shape} and {y.shape}")
401 if x.ndim > 2 or y.ndim > 2:
ValueError: x and y must have same first dimension, but have shapes (1,) and (319,)
the error message tells you exactly what the problem is:
UserWarning: The keyword argument x does not match any arguments of the model function. It will be ignored.
You supply a value for x=xData to the fit method, but your function doubl_exp does not contain this variable. Here, as in the various other questions you asked about lmfit, the best advice is: read the documentation and look at the provided examples on the website. They'll likely cover >95% of your use-case and after that reading the error message Python gives you is always helpful ;)

What does it mean to have a TypeError: float() argument must be a string or a number, not 'pandas._libs.interval.Interval'?

I made this function to find the distribution of all the columns in my pandas dataframe and it works when I have each individual line in separate cells, but when I put it into this one function I get the error message
TypeError: float() argument must be a string or a number, not 'pandas._libs.interval.Interval'
I'm relatively new to coding so I don't know what that means or how to fix it. Please help! thank you
def function():
for x in range(df.shape[1]):
columns = df.columns
if len(df[columns[x]].unique()) > 10:
samp = pd.qcut(df[columns[x]], 4)
bins = samp.unique
sampdf = pd.DataFrame(samp)
ex1 = sampdf.groupby(columns[x]).agg({columns[x]:'count'})
ex2 = ex1.reset_index(drop=True)`
else:
samp = df[columns[x]]
sampdf = pd.DataFrame(samp)
ex1 = sampdf.groupby(columns[x]).agg({columns[x]:'count'})
plt.style.use('ggplot')
plt.bar(ex1.index, ex1[columns[x]], color= 'purple')
plt.ylabel('Customers')
plt.xlabel(columns[x])
plt.title('Distribution of ' + columns[x])
plt.show()
The Traceback was:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-28-c0b3cafe2087> in <module>
----> 1 function1()
<ipython-input-27-bcfb6d749f86> in function1()
15
16 plt.style.use('ggplot')
---> 17 plt.bar(ex1.index, ex1[columns[0]], color= 'purple')
18 plt.ylabel('Customers')
19 plt.xlabel(columns[0])
/opt/anaconda3/lib/python3.8/site-packages/matplotlib/pyplot.py in bar(x, height, width, bottom, align, data, **kwargs)
2485 x, height, width=0.8, bottom=None, *, align='center',
2486 data=None, **kwargs):
-> 2487 return gca().bar(
2488 x, height, width=width, bottom=bottom, align=align,
2489 **({"data": data} if data is not None else {}), **kwargs)
/opt/anaconda3/lib/python3.8/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
1436 def inner(ax, *args, data=None, **kwargs):
1437 if data is None:
-> 1438 return func(ax, *map(sanitize_sequence, args), **kwargs)
1439
1440 bound = new_sig.bind(ax, *args, **kwargs)
/opt/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_axes.py in bar(self, x, height, width, bottom, align, **kwargs)
2492 elif orientation == 'horizontal':
2493 r.sticky_edges.x.append(l)
-> 2494 self.add_patch(r)
2495 patches.append(r)
2496
/opt/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_base.py in add_patch(self, p)
2031 if p.get_clip_path() is None:
2032 p.set_clip_path(self.patch)
-> 2033 self._update_patch_limits(p)
2034 self.patches.append(p)
2035 p._remove_method = self.patches.remove
/opt/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_base.py in _update_patch_limits(self, patch)
2051 vertices = patch.get_path().vertices
2052 if vertices.size > 0:
-> 2053 xys = patch.get_patch_transform().transform(vertices)
2054 if patch.get_data_transform() != self.transData:
2055 patch_to_data = (patch.get_data_transform() -
/opt/anaconda3/lib/python3.8/site-packages/matplotlib/patches.py in get_patch_transform(self)
790
791 def get_patch_transform(self):
--> 792 self._update_patch_transform()
793 return self._rect_transform
794
/opt/anaconda3/lib/python3.8/site-packages/matplotlib/patches.py in _update_patch_transform(self)
769 """
770 x0, y0, x1, y1 = self._convert_units()
--> 771 bbox = transforms.Bbox.from_extents(x0, y0, x1, y1)
772 rot_trans = transforms.Affine2D()
773 rot_trans.rotate_deg_around(x0, y0, self.angle)
/opt/anaconda3/lib/python3.8/site-packages/matplotlib/transforms.py in from_extents(*args)
820 The *y*-axis increases upwards.
821 """
--> 822 return Bbox(np.reshape(args, (2, 2)))
823
824 def __format__(self, fmt):
/opt/anaconda3/lib/python3.8/site-packages/matplotlib/transforms.py in __init__(self, points, **kwargs)
772 """
773 BboxBase.__init__(self, **kwargs)
--> 774 points = np.asarray(points, float)
775 if points.shape != (2, 2):
776 raise ValueError('Bbox points must be of the form '
/opt/anaconda3/lib/python3.8/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
81
82 """
---> 83 return array(a, dtype, copy=False, order=order)
84
85
TypeError: float() argument must be a string or a number, not 'pandas._libs.interval.Interval'

pandas Series' object has no attribute 'find'

I am trying to do simple plot of data and getting the following error.. any help is very much appreciated
AttributeError: 'Series' object has no attribute 'find'
Versions :
python3 ,
matplotlib (2.0.2) ,
pandas (0.20.3) ,
jupyter (1.0.0).
Code:
import pandas as pd
import matplotlib.pyplot as plt
pd_hr_data = pd.read_csv("/Users/pc/Downloads/HR_comma_sep.csv")
#print(pd_hr_data['average_montly_hours'],pd_hr_data['sales'])
take_ten_data = pd_hr_data[0:19]
x = take_ten_data['average_montly_hours'].astype(int)
y = take_ten_data['sales'].astype(str)
print(type(x[0]))
print(type(y[0]))
#print(x,y) ---- this gives me all the 20 values
#print(type(y[0]))
plt.plot(x,y)
plt.show()
Out Put / Error:
-
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in ()
9 #print(type(y[0]))
10
---> 11 plt.plot(x,y)
12 plt.show()
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/pyplot.py
in plot(*args, **kwargs)
3315 mplDeprecation)
3316 try:
-> 3317 ret = ax.plot(*args, **kwargs)
3318 finally:
3319 ax._hold = washold
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/__init__.py
in inner(ax, *args, **kwargs)
1896 warnings.warn(msg % (label_namer, func.__name__),
1897 RuntimeWarning, stacklevel=2)
-> 1898 return func(ax, *args, **kwargs)
1899 pre_doc = inner.__doc__
1900 if pre_doc is None:
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_axes.py
in plot(self, *args, **kwargs)
1404 kwargs = cbook.normalize_kwargs(kwargs, _alias_map)
1405
-> 1406 for line in self._get_lines(*args, **kwargs):
1407 self.add_line(line)
1408 lines.append(line)
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_base.py
in _grab_next_args(self, *args, **kwargs)
405 return
406 if len(remaining) <= 3:
--> 407 for seg in self._plot_args(remaining, kwargs):
408 yield seg
409 return
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_base.py
in _plot_args(self, tup, kwargs)
355 ret = []
356 if len(tup) > 1 and is_string_like(tup[-1]):
--> 357 linestyle, marker, color = _process_plot_format(tup[-1])
358 tup = tup[:-1]
359 elif len(tup) == 3:
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_base.py
in _process_plot_format(fmt)
92 # handle the multi char special cases and strip them from the
93 # string
---> 94 if fmt.find('--') >= 0:
95 linestyle = '--'
96 fmt = fmt.replace('--', '')
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/generic.py
in __getattr__(self, name)
3079 if name in self._info_axis:
3080 return self[name]
-> 3081 return object.__getattribute__(self, name)
3082
3083 def __setattr__(self, name, value):
AttributeError: 'Series' object has no attribute 'find'
I think you can use DataFrame.plot with define x and y by columns names, because it better support plotting non numeric values:
take_ten_data = pd_hr_data[0:19]
x = take_ten_data['average_montly_hours'].astype(int)
y = take_ten_data['sales'].astype(str)
take_ten_data.plot(x='average_montly_hours', y='sales')
#working without x,y also, but less readable
#take_ten_data.plot('average_montly_hours','sales')
plt.show()
Sample:
take_ten_data = pd.DataFrame({'average_montly_hours':[3,10,12], 'sales':[10,20,30]})
x = take_ten_data['average_montly_hours'].astype(int)
y = take_ten_data['sales'].astype(str)
take_ten_data.plot(x='average_montly_hours', y='sales')
plt.show()
But if all values are numeric it works nice:
take_ten_data = pd.DataFrame({'average_montly_hours':[3,10,12], 'sales':['10','20','30']})
x = take_ten_data['average_montly_hours'].astype(int)
#convert to int if necessary
y = take_ten_data['sales'].astype(int)
plt.plot(x,y)
plt.show()
Following worked for me and hope it helps.... Issue was mixing differnt data types for plotting.
import pandas as pd
import matplotlib.pyplot as plt
pd_hr_data = pd.read_csv("/Users/pc/Downloads/HR_comma_sep.csv")
take_ten_data = pd_hr_data[0:4]
y = take_ten_data['average_montly_hours'].astype(int)
x = [1,2,3,4] ----this is can be autogenerated based on the series/matrix size
names = take_ten_data['sales']
plt.bar(x,y, align='center')
#plt.plot(x,y) ---- use this if you want
plt.xticks(x, names)
plt.show()

AssertionError using Basemap and Pandas

I'm trying to follow the tutorial here:
http://nbviewer.ipython.org/github/ehmatthes/intro_programming/blob/master/notebooks/visualization_earthquakes.ipynb#install_standard
However, I am using pandas instead of the built in csv module for python. My code is as follows:
import pandas as pd
eq_data = pd.read_csv('earthquake_data.csv')
map2 = Basemap(projection='robin'
, resolution='l'
, area_thresh=1000.0
, lat_0=0
, lon_0=0)
map2.drawcoastlines()
map2.drawcountries()
map2.fillcontinents(color = 'gray')
map2.drawmapboundary()
map2.drawmeridians(np.arange(0, 360, 30))
map2.drawparallels(np.arange(-90, 90, 30))
x,y = map2(eq_data['longitude'].values, eq_data['latitude'].values)
map2.plot(x,y, marker='0', markercolor='red', markersize=6)
This produces an AssertionError but with no description:
AssertionError Traceback (most recent call last)
<ipython-input-64-d3426e1f175d> in <module>()
14 x,y = map2(range(20), range(20))#eq_data['longitude'].values, eq_data['latitude'].values)
15
---> 16 map2.plot(x,y, marker='0', markercolor='red', markersize=6)
c:\Python27\lib\site-packages\mpl_toolkits\basemap\__init__.pyc in with_transform(self, x, y, *args, **kwargs)
540 # convert lat/lon coords to map projection coords.
541 x, y = self(x,y)
--> 542 return plotfunc(self,x,y,*args,**kwargs)
543 return with_transform
544
c:\Python27\lib\site-packages\mpl_toolkits\basemap\__init__.pyc in plot(self, *args, **kwargs)
3263 ax.hold(h)
3264 try:
-> 3265 ret = ax.plot(*args, **kwargs)
3266 except:
3267 ax.hold(b)
c:\Python27\lib\site-packages\matplotlib\axes.pyc in plot(self, *args, **kwargs)
4135 lines = []
4136
-> 4137 for line in self._get_lines(*args, **kwargs):
4138 self.add_line(line)
4139 lines.append(line)
c:\Python27\lib\site-packages\matplotlib\axes.pyc in _grab_next_args(self, *args, **kwargs)
315 return
316 if len(remaining) <= 3:
--> 317 for seg in self._plot_args(remaining, kwargs):
318 yield seg
319 return
c:\Python27\lib\site-packages\matplotlib\axes.pyc in _plot_args(self, tup, kwargs)
303 ncx, ncy = x.shape[1], y.shape[1]
304 for j in xrange(max(ncx, ncy)):
--> 305 seg = func(x[:, j % ncx], y[:, j % ncy], kw, kwargs)
306 ret.append(seg)
307 return ret
c:\Python27\lib\site-packages\matplotlib\axes.pyc in _makeline(self, x, y, kw, kwargs)
255 **kw
256 )
--> 257 self.set_lineprops(seg, **kwargs)
258 return seg
259
c:\Python27\lib\site-packages\matplotlib\axes.pyc in set_lineprops(self, line, **kwargs)
198 raise TypeError('There is no line property "%s"' % key)
199 func = getattr(line, funcName)
--> 200 func(val)
201
202 def set_patchprops(self, fill_poly, **kwargs):
c:\Python27\lib\site-packages\matplotlib\lines.pyc in set_marker(self, marker)
851
852 """
--> 853 self._marker.set_marker(marker)
854
855 def set_markeredgecolor(self, ec):
c:\Python27\lib\site-packages\matplotlib\markers.pyc in set_marker(self, marker)
231 else:
232 try:
--> 233 Path(marker)
234 self._marker_function = self._set_vertices
235 except ValueError:
c:\Python27\lib\site-packages\matplotlib\path.pyc in __init__(self, vertices, codes, _interpolation_steps, closed, readonly)
145 codes[-1] = self.CLOSEPOLY
146
--> 147 assert vertices.ndim == 2
148 assert vertices.shape[1] == 2
149
AssertionError:
I thought I had the problem due to the update to pandas which no longer allows passing Series like you used to be able to as described here:
Runtime error using python basemap and pyproj?
But as you can see, I adjusted my code for this and it didn't fix the problem. At this point I am lost.
I am using Python 2.7.6, pandas 0.15.2, and basemap 1.0.7 on windows server 2012 x64.
There are two problems with my code. First, the plot function for the map2 object is inherited from matplotlib. Thus the marker attribute cannot be '0' it needs to be 'o'. Additionally, there is no markercolor attribute. It is called color. The below code should work.
import pandas as pd
eq_data = pd.read_csv('earthquake_data.csv')
map2 = Basemap(projection='robin'
, resolution='l'
, area_thresh=1000.0
, lat_0=0
, lon_0=0)
map2.drawcoastlines()
map2.drawcountries()
map2.fillcontinents(color = 'gray')
map2.drawmapboundary()
map2.drawmeridians(np.arange(0, 360, 30))
map2.drawparallels(np.arange(-90, 90, 30))
x,y = map2(eq_data['longitude'].values, eq_data['latitude'].values)
map2.plot(x,y, marker='o', color='red', markersize=6, linestyle='')

Categories

Resources