Drawing a FacetGrid of QQ-plots with seaborn - python

I am unable to draw a FacetGrid of QQ-plots with seaborn.
I have a matrix of m rows (observations) and n columns (features), and I want to draw a QQ-plot for each feature (column) to compare it with the normal distribution.
So far, my code is like this:
import scipy.stats as ss
def qqplots(fpath, expr, title):
def quantile_plot(x, **kwargs):
x = ss.zscore(x)
qntls, xr = ss.probplot(x, dist="norm")
plt.scatter(xr, qntls, **kwargs)
expr_m = pd.melt(expr)
expr_m.columns = ["Feature", "Value"]
n_feat = len(expr_m["Feature"].value_counts().index)
n_cols = int(np.sqrt(n_feat)) + 1
g = sns.FacetGrid(expr_m, col="Feature", col_wrap=n_cols)
g.map(quantile_plot, "Value");
plt.savefig(fpath + ".pdf", bbox_inches="tight")
plt.savefig(fpath + ".png", bbox_inches="tight")
plt.close()
qqplots("lognorm_qqplot", np.log2(expr), "Log-normal qqplot")
The expr variable is a pandas DataFrame with m rows (observations) and n columns (features).
The Exception I get is the following:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-52-f9333a55702e> in <module>()
39 plt.close()
40
---> 41 qqplots("lognorm_qqplot", np.log2(expr), "Log-normal qqplot")
<ipython-input-52-f9333a55702e> in qqplots(fpath, expr, title)
34
35 g = sns.FacetGrid(expr_m, col="Feature", col_wrap=n_cols)
---> 36 g.map(quantile_plot, "Value");
37 plt.savefig(fpath + ".pdf", bbox_inches="tight")
38 plt.savefig(fpath + ".png", bbox_inches="tight")
/usr/local/lib/python3.5/site-packages/seaborn/axisgrid.py in map(self, func, *args, **kwargs)
726
727 # Draw the plot
--> 728 self._facet_plot(func, ax, plot_args, kwargs)
729
730 # Finalize the annotations and layout
/usr/local/lib/python3.5/site-packages/seaborn/axisgrid.py in _facet_plot(self, func, ax, plot_args, plot_kwargs)
810
811 # Draw the plot
--> 812 func(*plot_args, **plot_kwargs)
813
814 # Sort out the supporting information
<ipython-input-52-f9333a55702e> in quantile_plot(y, **kwargs)
25 y = ss.zscore(y)
26 qntls, xr = ss.probplot(y, dist="norm")
---> 27 plt.scatter(xr, qntls, **kwargs)
28
29 expr_m = pd.melt(expr)
/usr/local/lib/python3.5/site-packages/matplotlib/pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, hold, data, **kwargs)
3249 vmin=vmin, vmax=vmax, alpha=alpha,
3250 linewidths=linewidths, verts=verts,
-> 3251 edgecolors=edgecolors, data=data, **kwargs)
3252 finally:
3253 ax.hold(washold)
/usr/local/lib/python3.5/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
1810 warnings.warn(msg % (label_namer, func.__name__),
1811 RuntimeWarning, stacklevel=2)
-> 1812 return func(ax, *args, **kwargs)
1813 pre_doc = inner.__doc__
1814 if pre_doc is None:
/usr/local/lib/python3.5/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
3838 y = np.ma.ravel(y)
3839 if x.size != y.size:
-> 3840 raise ValueError("x and y must be the same size")
3841
3842 s = np.ma.ravel(s) # This doesn't have to match x, y in size.
ValueError: x and y must be the same size

I achieved this, and also changed the color to use the Seaborn color palette, with the following code:
def qqplots(fpath, expr, title):
def quantile_plot(x, **kwargs):
x = ss.zscore(x)
ss.probplot(x, plot=plt)
expr_m = pd.melt(expr)
expr_m.columns = ["Feature", "Value"]
n_feat = len(expr_m["Feature"].value_counts().index)
n_cols = int(np.sqrt(n_feat)) + 1
g = sns.FacetGrid(expr_m, col="Feature", col_wrap=n_cols)
g.map(quantile_plot, "Value");
for ax in g.axes:
ax.get_lines()[0].set_markerfacecolor(sns.color_palette()[0])
ax.get_lines()[1].set_color(sns.color_palette()[3])
plt.savefig(fpath + ".pdf", bbox_inches="tight")
plt.savefig(fpath + ".png", bbox_inches="tight")
plt.close()
qqplots("lognorm_qqplot", np.log2(expr), "Log-normal qqplot")

Answering your question: "I am unable to draw a FacetGrid of QQ-plots with seaborn.", I give you here an example with dataset tips from seaborn.
To draw qqplots, one of the best approaches is to use the statsmodels library which has the qqplot function built into it. This function generates a new figure if the given ax is not passed as an argument. Therefore, using FacetGrid.map() with this function generates individual figures instead of plotting everything on the grid.
To deal with this, you can use a user-defined function in which sm.qqplots retrieves the current ax thanks to plt.gca(). Here I created a new function called qqplot_new. Here the qqplots are like test for normality of data.
from matplotlib import pyplot as plt
import seaborn as sns
import statsmodels.api as sm
tips = sns.load_dataset("tips")
def qqplot_new(x, ax=None, **kwargs):
if ax is None:
ax = plt.gca()
sm.qqplot(x, ax=ax, **kwargs)
g = sns.FacetGrid(tips, col="time", row="sex")
g.map(qqplot_new, "total_bill", line='s')
Output : Figure obtained

Related

NonUniformImage: numpy example gives 'cannot unpack non-iterable NoneType object' error 2D-Histogram

I'm trying to run this very simple example from numpy page regarding histogram2d:
https://numpy.org/doc/stable/reference/generated/numpy.histogram2d.html.
from matplotlib.image import NonUniformImage
import matplotlib.pyplot as plt
xedges = [0, 1, 3, 5]
yedges = [0, 2, 3, 4, 6]
x = np.random.normal(2, 1, 100)
y = np.random.normal(1, 1, 100)
H, xedges, yedges = np.histogram2d(x, y, bins=(xedges, yedges))
H = H.T
fig = plt.figure(figsize=(7, 3))
ax = fig.add_subplot(131, title='imshow: square bins')
plt.imshow(H, interpolation='nearest', origin='lower',extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])
ax = fig.add_subplot(132, title='pcolormesh: actual edges',aspect='equal')
X, Y = np.meshgrid(xedges, yedges)
ax.pcolormesh(X, Y, H)
ax = fig.add_subplot(133, title='NonUniformImage: interpolated',aspect='equal', xlim=xedges[[0, -1]], ylim=yedges[[0, -1]])
im = NonUniformImage(ax, interpolation='bilinear')
xcenters = (xedges[:-1] + xedges[1:]) / 2
ycenters = (yedges[:-1] + yedges[1:]) / 2
im.set_data(xcenters,ycenters,H)
ax.images.append(im)
plt.show()
By running this code as in the example, I receive the error
cannot unpack non-iterable NoneType object
This happens as soon as I run the line ax.images.append(im).
Does anyone know why this happens?
Tried to run an example from numpy website and doesn't work as expected.
The full error message is:
TypeError Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\IPython\core\formatters.py:339, in BaseFormatter.__call__(self, obj)
337 pass
338 else:
--> 339 return printer(obj)
340 # Finally look for special method names
341 method = get_real_method(obj, self.print_method)
File ~\anaconda3\lib\site-packages\IPython\core\pylabtools.py:151, in print_figure(fig, fmt, bbox_inches, base64, **kwargs)
148 from matplotlib.backend_bases import FigureCanvasBase
149 FigureCanvasBase(fig)
--> 151 fig.canvas.print_figure(bytes_io, **kw)
152 data = bytes_io.getvalue()
153 if fmt == 'svg':
File ~\anaconda3\lib\site-packages\matplotlib\backend_bases.py:2299, in FigureCanvasBase.print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
2297 if bbox_inches:
2298 if bbox_inches == "tight":
-> 2299 bbox_inches = self.figure.get_tightbbox(
2300 renderer, bbox_extra_artists=bbox_extra_artists)
2301 if pad_inches is None:
2302 pad_inches = rcParams['savefig.pad_inches']
File ~\anaconda3\lib\site-packages\matplotlib\figure.py:1632, in FigureBase.get_tightbbox(self, renderer, bbox_extra_artists)
1629 artists = bbox_extra_artists
1631 for a in artists:
-> 1632 bbox = a.get_tightbbox(renderer)
1633 if bbox is not None and (bbox.width != 0 or bbox.height != 0):
1634 bb.append(bbox)
File ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py:4666, in _AxesBase.get_tightbbox(self, renderer, call_axes_locator, bbox_extra_artists, for_layout_only)
4662 if np.all(clip_extent.extents == axbbox.extents):
4663 # clip extent is inside the Axes bbox so don't check
4664 # this artist
4665 continue
-> 4666 bbox = a.get_tightbbox(renderer)
4667 if (bbox is not None
4668 and 0 < bbox.width < np.inf
4669 and 0 < bbox.height < np.inf):
4670 bb.append(bbox)
File ~\anaconda3\lib\site-packages\matplotlib\artist.py:355, in Artist.get_tightbbox(self, renderer)
340 def get_tightbbox(self, renderer):
341 """
342 Like `.Artist.get_window_extent`, but includes any clipping.
343
(...)
353 The enclosing bounding box (in figure pixel coordinates).
354 """
--> 355 bbox = self.get_window_extent(renderer)
356 if self.get_clip_on():
357 clip_box = self.get_clip_box()
File ~\anaconda3\lib\site-packages\matplotlib\image.py:943, in AxesImage.get_window_extent(self, renderer)
942 def get_window_extent(self, renderer=None):
--> 943 x0, x1, y0, y1 = self._extent
944 bbox = Bbox.from_extents([x0, y0, x1, y1])
945 return bbox.transformed(self.axes.transData)
TypeError: cannot unpack non-iterable NoneType object
<Figure size 504x216 with 3 Axes>
The error occurs deep in the append call, and appears to involve trying to get information about the plot window. If I comment out the append line, and it continues on to the plt.show(), and resulting image looks like the example, except the third image is blank.
I tested this in a Windows QtConsole; I don't know if that context posses problems for this append or not. I don't think it's a problem with your code copy.

3D matplotlib TypeError: Input z must be 2D, not 1D

I have a tf-idf matrix in a dataframe. I ran it through tsne.
tsne_vecs_clarke2 = TSNE(n_components=3, perplexity=30.0, init='pca', learning_rate='auto').fit_transform(clarke)
clarke['component1'] = tsne_vecs_clarke2[:,0]
clarke['component2'] = tsne_vecs_clarke2[:,1]
clarke['component3'] = tsne_vecs_clarke2[:,2]
When I plotted clarke['component2'] against clarke['component2'] with the following code, I get this plot:
sns.scatterplot(x=clarke['component3'], y=clarke['component2'], hue=clarke['0inclusion'],
data=clarke).set(title="T-SNE projection ")
I would like to look at it in 3D to get more insights. I tried to plot it in 3D matplotlib but I ran into a TypeError stating that input z must be 2D, not 1D.
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x = clarke['component1']
y = clarke['component2']
z = clarke['component3']
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(x, y, z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_16936/3386285865.py in <module>
1 fig = plt.figure()
2 ax = plt.axes(projection='3d')
----> 3 ax.contour3D(x, y, z, 50, cmap='binary')
4 ax.set_xlabel('x')
5 ax.set_ylabel('y')
~\anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py in contour(self, X, Y, Z, extend3d, stride, zdir, offset, *args, **kwargs)
2173
2174 jX, jY, jZ = art3d.rotate_axes(X, Y, Z, zdir)
-> 2175 cset = super().contour(jX, jY, jZ, *args, **kwargs)
2176 self.add_contour_set(cset, extend3d, stride, zdir, offset)
2177
~\anaconda3\lib\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)
~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in contour(self, *args, **kwargs)
6418 def contour(self, *args, **kwargs):
6419 kwargs['filled'] = False
-> 6420 contours = mcontour.QuadContourSet(self, *args, **kwargs)
6421 self._request_autoscale_view()
6422 return contours
~\anaconda3\lib\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
~\anaconda3\lib\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)
~\anaconda3\lib\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:
~\anaconda3\lib\site-packages\matplotlib\contour.py in _check_xyz(self, args, kwargs)
1450
1451 if z.ndim != 2:
-> 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, "
TypeError: Input z must be 2D, not 1D
I am not sure how to fix this issue. Any help will be much appreciated.
If you're doing PCA you probably want a scatterplot, which you can make with ax.scatter3D(x, y, z).
If you do want this as a contour, see this answer for how to structure your data: Why does pyplot.contour() require Z to be a 2D array?

I am using xarray to plot a contour graph. Is there a way to not include the original colorbar but still retain the contour plot?

This is my data (just some sample data from xarray) and a contour plot is made. However I want to make my own colorbar and not use xarray's embedded colorbar. How do I make xarray do that?
ds = xr.tutorial.open_dataset("air_temperature.nc").rename({"air": "Tair"})
# we will add a gradient field with appropriate attributes
ds["dTdx"] = ds.Tair.differentiate("lon") / 110e3 / np.cos(ds.lat * np.pi / 180)
ds["dTdy"] = ds.Tair.differentiate("lat") / 105e3
ds.dTdx.attrs = {"long_name": "$∂T/∂x$", "units": "°C/m"}
ds.dTdy.attrs = {"long_name": "$∂T/∂y$", "units": "°C/m"}
ds.Tair.isel(time=1).plot()
plt.show()
I tried doing plt.colorbar(False) but that did not work and I got this error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-53-bcb5ae6a218e> in <module>
8
9 ds.Tair.isel(time=1).plot()
---> 10 plt.colorbar(False)
11 plt.show()
~/miniconda3/envs/py3_std_maps/lib/python3.8/site-packages/matplotlib/pyplot.py in colorbar(mappable, cax, ax, **kw)
2176 if ax is None:
2177 ax = gca()
-> 2178 ret = gcf().colorbar(mappable, cax=cax, ax=ax, **kw)
2179 return ret
2180 colorbar.__doc__ = matplotlib.colorbar.colorbar_doc
~/miniconda3/envs/py3_std_maps/lib/python3.8/site-packages/matplotlib/figure.py in colorbar(self, mappable, cax, ax, use_gridspec, **kw)
2341 'panchor']
2342 cb_kw = {k: v for k, v in kw.items() if k not in NON_COLORBAR_KEYS}
-> 2343 cb = cbar.colorbar_factory(cax, mappable, **cb_kw)
2344
2345 self.sca(current_ax)
~/miniconda3/envs/py3_std_maps/lib/python3.8/site-packages/matplotlib/colorbar.py in colorbar_factory(cax, mappable, **kwargs)
1729 cb = ColorbarPatch(cax, mappable, **kwargs)
1730 else:
-> 1731 cb = Colorbar(cax, mappable, **kwargs)
1732
1733 cid = mappable.callbacksSM.connect('changed', cb.update_normal)
~/miniconda3/envs/py3_std_maps/lib/python3.8/site-packages/matplotlib/colorbar.py in __init__(self, ax, mappable, **kwargs)
1197 # Ensure the given mappable's norm has appropriate vmin and vmax set
1198 # even if mappable.draw has not yet been called.
-> 1199 if mappable.get_array() is not None:
1200 mappable.autoscale_None()
1201
AttributeError: 'bool' object has no attribute 'get_array'
You can control quite a lot with the xarray.DataArray.plot method plus it accepts kwargs which are passed to matplotlib.
So you can directly tell plot to use a specific color map with the cmap argument, and you can suppress the colorbar itself by setting add_colorbar to False:
ds.Tair.isel(time=1).plot(cmap="RdYlBu_r", add_colorbar=False)
gives me:

How could I solve these errors below?

I'm new at python and I'm trying to run this piece of code that found in this link below:
http://benalexkeen.com/gradient-boosting-in-python-using-scikit-learn/
When I run the first two snippets I got a bunch of errors, could anyone please correct it for me, please?. I have data and I try to draw them like this in these two snippets.
These are the two piece of code
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import ensemble
from sklearn import linear_model
# Mock data
x = np.arange(0, 60)
y = map(lambda x: x / 2 + (x // 10) % 2 * 20 * x / 5 + np.random.random() * 10, x)
x = pd.DataFrame({'x': x})
# Plot mock data
plt.figure(figsize=(10, 5))
plt.scatter(x, y)
plt.show()
I got the errors that below:
RuntimeError Traceback (most recent call last)
<ipython-input-2-7f1d946a4092> in <module>
6 # Plot mock data
7 plt.figure(figsize=(10, 5))
----> 8 plt.scatter(x, y)
9 plt.show()
~\Anaconda3\lib\site-packages\matplotlib\pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, data, **kwargs)
2862 vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths,
2863 verts=verts, edgecolors=edgecolors, **({"data": data} if data
-> 2864 is not None else {}), **kwargs)
2865 sci(__ret)
2866 return __ret
~\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
1808 "the Matplotlib list!)" % (label_namer, func.__name__),
1809 RuntimeWarning, stacklevel=2)
-> 1810 return func(ax, *args, **kwargs)
1811
1812 inner.__doc__ = _add_data_doc(inner.__doc__,
~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
4170 edgecolors = 'face'
4171
-> 4172 self._process_unit_info(xdata=x, ydata=y, kwargs=kwargs)
4173 x = self.convert_xunits(x)
4174 y = self.convert_yunits(y)
~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _process_unit_info(self, xdata, ydata, kwargs)
2134
2135 kwargs = _process_single_axis(xdata, self.xaxis, 'xunits', kwargs)
-> 2136 kwargs = _process_single_axis(ydata, self.yaxis, 'yunits', kwargs)
2137 return kwargs
2138
~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _process_single_axis(data, axis, unit_name, kwargs)
2116 # We only need to update if there is nothing set yet.
2117 if not axis.have_units():
-> 2118 axis.update_units(data)
2119
2120 # Check for units in the kwargs, and if present update axis
~\Anaconda3\lib\site-packages\matplotlib\axis.py in update_units(self, data)
1465 """
1466
-> 1467 converter = munits.registry.get_converter(data)
1468 if converter is None:
1469 return False
~\Anaconda3\lib\site-packages\matplotlib\units.py in get_converter(self, x)
185 if converter is None:
186 try:
--> 187 thisx = safe_first_element(x)
188 except (TypeError, StopIteration):
189 pass
~\Anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in safe_first_element(obj)
1633 except TypeError:
1634 pass
-> 1635 raise RuntimeError("matplotlib does not support generators "
1636 "as input")
1637 return next(iter(obj))
RuntimeError: matplotlib does not support generators as input
The results that I'm expecting to get below in this picture
Replace plt.scatter(x, y) with plt.scatter(x, list(y)).
The value of y represents a generator function, but matplotlib needs a list here. That worked for me on python 3.6
Convert map object to list, because in python 3 is returned iterator:
y = list(map(lambda x: x / 2 + (x // 10) % 2 * 20 * x / 5 + np.random.random() * 10, x))

Setting axis values of a 2D contour plot without using xticks

I'm attempting to set the values of the x and y-axes of a plot generated using contour() but am currently unable to read specific values off of the axes as desried.
fid_list = []
for fidN in arange(frames):
offset = fidN * fid_pts
current_fid = cmplx_data[offset:offset+fid_pts]
fid_list.append(current_fid)
fid_mat = fid_list
jres_spec = abs(fftshift(fft2(fid_mat)))
max_val = jres_spec.max()/15
min_val = max_val*0.15
steps = 40
figure()
CS=contour(jres_spec,arange(min_val,max_val,(max_val-min_val)/steps))
show()
Which generates a plot like this
Previously I've been using xticks and yticks to set the values of the axes, but now the exact position on the plot has become important, so being able to read values off the axes would be very helpful which I can't doing with x/yticks.
When plotting a 1D spectrum, I use the following formula to enable me to read off the x-axis
bins = arange(828, -196, -1) #change this so that 0 value occurs at value it's meant to
x = (2000 * bins / 1024.0)/128.0
plot(x, fftshift(fft(fid_list[0])))
plt.gca().invert_xaxis()
show()
And would similarly use this for the y-axis of my 2D contour plot
ybins = arange(15, -15, -1)
y = ybins * ((1/(15*10^(-3)))/ 30.0)
But I'm having trouble integrating this into my code...
I've tried using something like this
ybins = arange(15, -15, -1)
y = ybins * ((1/(15*10^(-3)))/ 30.0)
xbins = arange(828, -196, -1)
x = (2000 * xbins / 1024.0)/128.0
fid_mat = fid_list
jres_spec = abs(fftshift(fft2(fid_mat)))
max_val = jres_spec.max()/15
min_val = max_val*0.15
steps = 40
figure()
CS=contour((x, y, jres_spec),arange(min_val,max_val,(max_val-min_val)/steps))
show()
which returned
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/dominicc/<ipython-input-34-28b34c6c069d> in <module>()
7 bins = arange(828, -196, -1) #change this so that 0 value occurs at value it's meant to
8 x = (2000 * bins / 1024.0)/128.0
----> 9 CS=contour((x_list, jres_spec),arange(min_val,max_val,(max_val-min_val)/steps))
10 show()
/usr/lib/pymodules/python2.7/matplotlib/pyplot.pyc in contour(*args, **kwargs)
2195 ax.hold(hold)
2196 try:
-> 2197 ret = ax.contour(*args, **kwargs)
2198 draw_if_interactive()
2199 finally:
/usr/lib/pymodules/python2.7/matplotlib/axes.pyc in contour(self, *args, **kwargs)
7379 if not self._hold: self.cla()
7380 kwargs['filled'] = False
-> 7381 return mcontour.QuadContourSet(self, *args, **kwargs)
7382 contour.__doc__ = mcontour.QuadContourSet.contour_doc
7383
/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
1110 are described in QuadContourSet.contour_doc.
1111 """
-> 1112 ContourSet.__init__(self, ax, *args, **kwargs)
1113
1114 def _process_args(self, *args, **kwargs):
/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
701 if self.origin == 'image': self.origin = mpl.rcParams['image.origin']
702
--> 703 self._process_args(*args, **kwargs)
704 self._process_levels()
705
/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in _process_args(self, *args, **kwargs)
1123 self.zmax = args[0].zmax
1124 else:
-> 1125 x, y, z = self._contour_args(args, kwargs)
1126
1127 x0 = ma.minimum(x)
/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in _contour_args(self, args, kwargs)
1167 if Nargs <= 2:
1168 z = ma.asarray(args[0], dtype=np.float64)
-> 1169 x, y = self._initialize_x_y(z)
1170 args = args[1:]
1171 elif Nargs <=4:
/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in _initialize_x_y(self, z)
1230 '''
1231 if z.ndim != 2:
-> 1232 raise TypeError("Input must be a 2D array.")
1233 else:
1234 Ny, Nx = z.shape
TypeError: Input must be a 2D array.
And I'm now struggling to think of other ways I could do this.
Any ideas/suggestions?
You can get rid of the error by taking out the extra parenthesis in the call to contour:
CS=contour(x, y, jres_spec,arange(min_val,max_val,(max_val-min_val)/steps))
If that doesn't give you the plot you want, try xlim and ylim to set the axis limits directly.

Categories