I have data in a list of lists format.
It looks like this:
[(x_1, y_1, Z_1),...(x_i, y_j, z_k),...(x_p, y_q, z_r)]
For every x and y there is one z. Lengths of X, Y and Z are p, q and r(=p*q) respectively.
I intend to plot a contour plot with X and Y as mesh and Z as the value to be plotted.
I have following code (just representative):
import csv
import sys
import statistics
import numpy as np
from scipy.interpolate import UnivariateSpline
from matplotlib import pyplot as plt
...........
#format of data = [(x, y, z)......]
#x, y, z are lists
X = [X1,..........,Xp] #length, p
Y = [Y1,..........,Yq] #length, q
Z = [Z1,..........,Zpq] #length, pq
#np.mesh
X1, Y1 = np.meshgrid(X, Y)
plt.figure()
CS = plt.contour(X1, Y1, Z)
plt.clabel(CS, inline=1, fontsize=10)
I get following error:
Traceback (most recent call last):
File "C:/Users/uname/PycharmProjects/waferZoning/contour.py", line 49, in <module>
CS = plt.contour(X1, Y1, Z)
File "C:\Users\uname\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\pyplot.py", line 2766, in contour
ret = ax.contour(*args, **kwargs)
File "C:\Users\uname\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\__init__.py", line 1811, in inner
return func(ax, *args, **kwargs)
File "C:\Users\uname\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\axes\_axes.py", line 5640, in contour
return mcontour.QuadContourSet(self, *args, **kwargs)
File "C:\Users\uname\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\contour.py", line 1428, in __init__
ContourSet.__init__(self, ax, *args, **kwargs)
File "C:\Users\uname\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\contour.py", line 873, in __init__
self._process_args(*args, **kwargs)
File "C:\Users\uname\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\contour.py", line 1445, in _process_args
x, y, z = self._contour_args(args, kwargs)
File "C:\Users\uname\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\contour.py", line 1532, in _contour_args
x, y, z = self._check_xyz(args[:3], kwargs)
File "C:\Users\uname\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\contour.py", line 1566, in _check_xyz
raise TypeError("Input z must be a 2D array.")
TypeError: Input z must be a 2D array.
I understand what the error is, but I am not able to rectify it.
I can't give a MWE but I guess I have made my problem quite clear.
https://www.dropbox.com/s/33jmfcjzikl4w5g/contour_synthetic.txt?dl=0
You need to have a z
that has length equal to the product of the length of x
and y
:
assert len(z) == (len(x) * len(y))
Make z
a 2D array:
z = np.array(z)
z = z.reshape((len(x), len(y)))
Here a MCVE:
x = np.arange(5)
y = np.arange(5)
z = np.arange(25).reshape(5, 5)
x1, y1 = np.meshgrid(x, y)
plt.contour(x1, y1, z)
Make sure your data is structured like this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With