So I have a N-dimensional array of values, let's call it A. Now, I can plot this in a contour map, with coordinate axes X and Y, using
plt.contourf(X,Y,A)
Now, I have to carry out a mapping of these points to another plane, so, basically another set of coordinates. Let the transformation be
X - X1
Y - X1
Now, each point with magnitude "I" in matrix A at (X,Y) is at (X- X1, Y - Y1). I can plot this using
plt.contourf(X-X1, Y-Y1,A)
My question is, how do I index the array A such that I obtain an array B where the indexing corresponds to X-X1 and Y-Y1 instead of X and Y so that I can plot it directly using the following
plt.contourf(X,Y,B)
Thanks!
Related
I have a list f containing (N-1)*(N-1) variables which stores the values at each grid (x[i], y[i]). x and y are defined as lists storing the x and y coordinates of grid: which looks like x=[h, 2*h, ..., (N-1)*h] and y=[h, 2*h, ..., (N-1)*h] where h is constant spacing between the points.
The function values of f is defined using the elements of x and y: f[k]=f[i+j*(N-1)] where i and j are the index from x and y. Specifically, f=[g(x[0],y[0]), g(x[1],y[0]),...,g(x[N-2],y[N-2])], where g is a 2-variable function which depends on the values x[i] and y[j].
What I want to do is plotting the values g(x[i],y[j]) on the 2-D grid in the contour format. Since the list I have is 1-D, I could not figure out what function of matplotlib I must use.
Can somebody recommend me a good way to do this?
This question already has answers here:
Why does pyplot.contour() require Z to be a 2D array?
(5 answers)
Closed 5 years ago.
I plot a contour plot which indicates the seperating hyperplane of a SVC estimator in a 2D axes using the following code.
X,y= make_circles(n_samples=50,factor=.1,noise=.1)
x_fit=np.linspace(-1.5,1.5,10)
y_fit=np.linspace(-1.5,1.5,10)
Y,XX=np.meshgrid(x_fit,y_fit)
xy=np.vstack([XX.ravel(),Y.ravel()]).T
P=clf.decision_function(xy).reshape(XX.shape)
plt.contour(XX,Y,P,colors="k",levels=[-1,0,1],alpha=0.5,linestyles=["--","-","--"])
Question
Based on this question and the answer of Ilya V. Schurov there is still one issue for me. I understand, that X and Y provides the x and y values and Z provides the "depth" for each xy coordiante and thus has to be 2 dimensional. Further, the X and Y values of the plt.contour() function can be either 1D or 2D (if 1D the meshgrid gets computed internally).
BUT what is the benefit/ reason for X and Y to be 2D?
Because actually the "second dimension" of X and Y can not be plotted on a 2D axes. So has it some "algorithmic performance" reasons for X and Y to be 2D or what is the reason?
Contour plot is not designed for just plotting hyperplanes for classfier. It represents a 3-D surface with a 2-D format; or it plots elevations of a 2-D area. Therefore, plt.contour() has to somehow understand/know elevations covering the whole area. One way, or the current way, is to provide a set of elevations for a set of points covering the 2-D area. And the more you provide, the better/finer the final contour plot is. When providing a 1-D x and y, it represents a line rather than an area, which cannot be used to interpolated a 2-D area.
Another way to plot hyperplanes is to calculate the exact planes yourself. Then you can plot hyperplanes with a 1-D linespace. But I don't think this will be easier than using plt.contour() since plt.contour() did the hard calculation by simulating with interpolation for you.
Edit: How Z works with X and Y in plt.contour()?
It takes some assumption for Z works with X and Y.
If X and Y is 2-D, a value in Z is the depth for a point specified by corresponding (same location by index) values in X and Y.
If X and Y is 1-D, it will be convert to a meshgrid first, as you can see in the source code. Then the rest will work the same way as explained above.
So for your case specifically, using x_fit and y_fit can give you the same result because plt.contour() makes the meshgrid for you. As long as you understand the mechanism, either way is fine. The only thing I would say is if you end up making the meshgrid for calculating P anyway, why not using the meshgrid to avoid assumption/ambiguity?
I have a pair of 3d vectors u and v. I have another function f mapping 3d space onto real numbers (so, a scalar field). I want to draw a 2d plot f(xu + yv) using a colormap. So I need to end up with a matrix z filled with values of f, so I can go
pyplot.imshow(z)
But how can I do this? I tried
x = numpy.linspace(0, s2, 500)
y = numpy.linspace(0, 1, 500)
xs, ys = numpy.meshgrid(x, y)
z = f(u*xs + v*ys) # Not actually valid
Hoping that u*xs + v*ys would produce a matrix of 3d vectors, but that doesn't work. Also, even if I can get a matrix A of 3d vectors, what's the best way to get the matrix obtained by applying f to each element?
I have the chi-squared for all values of:
kT=linspace(0.01,0.11,10)
v=linspace(0.05,0.5,10)
where:
KT=[]
V=[]
for i in range(len(kT)):
for u in range(len(v)):
KT.append(kT[i])
V.append(v[u])
Therefore I have:
KT=asarray(KT)
V=asarray(V)
x=asarray(x)
Where x[0] is the chi-squared for (kT[0],v[0]),
and x[1] is the chi-squared for (kT[0],v[1])
etc...
So as an overview, I have 1D arrays of len=100, where kT[0] and v[0] gives x[0] (this is done in another program).
I want to plot the chi-squared as a contour plot, how do I go about this? I tried using the contour from plt.contour, but it was x as a 2D vector.
Any advice?
You can reshape your array
x2d = x.reshape(10, 10)
contour accepts vectors for grid coordinates. you can therefore omit the double loop and use kT and v directly
pyplot.contour(v, kT, x2d)
This question already has an answer here:
indexing spherical subset of 3d grid data in numpy
(1 answer)
Closed 9 years ago.
Can anybody help me - is there any way to create coordinates grid as numpy array like this?
(0,0) (0,1) (0,2) ... (0,n)
(1,0) (1,1) (1,2) ... (1,n)
...........................
(m,0) (m,1) (m,2) ... (m,n)
If yes, how can I find distance from every point to circle with center in (m/2, n/2) and radius R?
(x - m/2)^2 + (y - n/2)^2 - R^2 = ?
A standard way of doing this is with the meshgrid function. It makes two arrays, with the x and y coordinates of the points you want. To get the coordinates shown in your question you can do
import numpy as np
x = np.arange(m+1)
y = np.arange(n+1)
X, Y = np.meshgrid(x, y)
then to calculate the distance you want you can do
np.sqrt((X - m/2.)**2 + (Y - n/2.)**2) - R
For more information on meshgrid see the documentation
http://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html
Also, if you want evenly spaced values between two endpoints instead of just 0 through m or 0 through n, consider using the linspace function.