How to do 4D plot in matplotlib without np.meshgrid? - python

I know that I can do a 4D plot in matplotlib with the following code, with the fourth dimension shown as a colormap:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
fig = plt.figure()
ax = fig.add_subplot(111,projection= '3d' )
x = np.arange(100)/ 101
y = np.sin(x) + np.cos(x)
X,Y = np.meshgrid(x,y)
Z = (X**2) / (Y**2)
A = np.sin(Z)
ax.plot_surface(X,Y, Z, facecolors=cm.Oranges(A))
plt.show()
But what if my data is not a function of the other data? How do I do this without np.meshgrid? (In other words, my Z series cannot be a function of the output of the X,Y which is the output of np.meshgrid(x,y), because Z is not a function of X and Y.)

A surface plot is a mapping of 2D points to a 1D value, i.e. for each pair of (x,y) coordinates you need exactly one z coordinate. So while it isn't strictly necessary to have Z being a function of X and Y, those arrays to plot need to have the same number of elements.
For a plot_surface the restriction is to have X and Y as gridded 2D data. Z does not have to be 2D but needs to have the same number of elements.
This requirement can be weakened using a plot_trisurf where the only requirement is that there is a strict mapping of x,y,z, i.e. the ith value in X and Y corresponds to the ith value in Z.
In any case, even if there is no analytic function to map X and Y to Z, Z still needs to be some kind of mapping. Otherwise it is even questionable what information the resulting plot would convey.

Related

colormap from a matrix in python

I have a 2D output matrix (say, Z) which was calculated as a function of two variables x,y.
x varies in a non-uniform manner like [1e-5,5e-5,1e-4,5e-4,1e-3,5e-3,1e-2]
y varies in a uniform manner like [300,400,500,600,700,800]
[ say, Z = np.random.rand(7,6) ]
I was trying to plot a colormap of the matrix Z by first creating a meshgrid for x,y and then using the pcolormesh. Since, my x values are non-uniform, I do not kn ow how to proceed. Any inputs would be greatly appreciated.
No need for meshgrids; regarding the non-uniform axes: In your case a log-scale works fine:
import numpy as np
from matplotlib import pyplot as plt
x = [1e-5,5e-5,1e-4,5e-4,1e-3,5e-3,1e-2]
y = [300,400,500,600,700,800]
# either enlarge x and y by one number (right-most
# endpoint for those bins), or make Z smaller as I did
Z = np.random.rand(6,5)
fig = plt.figure()
ax = fig.gca()
ax.pcolormesh(x,y,Z.T)
ax.set_xscale("log")
fig.show()

Plot and function with three variables in python

An equation which is represent as below
sin(x)*sin(y)*sin(z)+cos(x)*sin(y)*cos(z)=0
I know the code to plot function for z=f(x,y) using matplotlib but to plot above function I don’t know the code, but I tried MATLAB MuPad code which is as follows
Plot(sin(x)*sin(y)*sin(z)+cos(x)*sin(y)*cos(z),#3d)
This will be much easier if you can isolate z. Your equation is the same as sin(z)/cos(z) = -cos(x)*sin(y)/(sin(x)*sin(y)) so z = atan(-cos(x)*sin(y)/(sin(x)*sin(y))).
Please don't mistake me, but I think your given equation to plot can be reduced to a simple 2D plot.
sin(x)*sin(y)*sin(z)+cos(x)*sin(y)*cos(z) = 0
sin(y)[sin(x)*sin(z)+cos(x)*cos(z)] = 0
sin(y)*cos(x-z) = 0
Hence sin(y) = 0 or cos(x-z)=0
Hence y = n*pi (1) or x-z=(2*n + 1)pi/2
Implies, x = z + (2*n + 1)pi/2 (2)
For (1), it will be a straight line (the plot of y vs n) and in second case, you will get parallel lines which cuts x-axis at (2*n + 1)pi/2 and distance between two parallel lines would be pi. (Assuming you keep n constant).
Assuming, your y can't be zero, you could simplify the plot to a 2D plot with just x and z.
And answering your original question, you need to use mplot3d to plot 3D plots. But as with any graphing tool, you need values or points of x, y, z. (You can compute the possible points by programming). Then you feed those points to the plot, like below.
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection="3d")
xs = [] # X values
ys = [] # Y values
zs = [] # Z values
ax.plot3D(xs, ys, zs)
plt.show()

Scale a 2D array logarithmically in Python

I have a 2D array of spectrogram data which I am scaling with scikit-image for display in a web browser. I would like to scale the array logarithmically in the y-axis."
I can plot the data logarithmically in the y-axis using Matplotlib, but I want access to the 2D array representation of this newly scaled image, but Matplotlib only provides the original, unscaled array. Scikit-image scales 2D arrays linearly, but not logarithmically.
# plot a log-scaled z
w, h = z.shape
fig, ax = plt.subplots()
ax.set_yscale('symlog')
mesh = ax.pcolormesh(np.arange(h+1), np.arange(w+1), z)
# get the array of z
logimg = mesh.get_array().reshape(mesh._meshHeight, mesh._meshWidth)
Let's start with some example data:
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pylab as plt
# some 2D example data
x, y = np.arange(30)+1, np.arange(20)+1
x_grid, y_grid = np.meshgrid(x, y)
z_grid = np.cos(2*np.pi*x_grid/10) + np.sin(2*np.pi*y_grid/4)
# Graph 1
plt.pcolormesh(x, np.log(y), z_grid);
plt.xlabel('x'); plt.ylabel('log(y) (non regular spacing)');
Here is a graph with log(y) as vertical axis. The sampling along y is then non uniform (the data are unchanged, they are only drawn on a deformed grid):
In order to get deformed data on a regular grid, an interpolation is performed between log(y) and a new regular y grid:
# Interpolation of the transformed data on a regular new y axis
ylog_interpolation = interp1d(np.log(y), z_grid.T)
new_y = np.linspace(min(np.log(y)), max(np.log(y)), len(y))
new_z_grid = ylog_interpolation(new_y).T
# axis
plt.pcolormesh(x, new_y, new_z_grid);
plt.xlabel('x'); plt.ylabel('new y (regular spacing)');
Now, the grid is regular but the data are deformed, new_z_grid can be exported as an image.

Matplotlib - using imshow for transformed coordinates within meshgrid arrays

I am using a python script to calculate the stress fields (Z meshgrid array) for a defects within a metal. In order to do this, I have to transform the X and Y meshgrids (xtrans and ytrans). I am then using imshow to plot colour along with the contour lines. The problem I am having is that imshow assumes that the Z array contains values at points running linearly between two ranges. E.g it assumes Z[0,:] are the values in the Y direction that go from -5, -4,-3, -2...3, 4, 5 say. After the coordinates are transformed Y isn't spaced linearly between -5 and 5.
Is there a way to use imshow or a function like it, that has Xtrans, and ytrans meshgrids as input parameters?
import numpy as np
import scipy
import matcompat
from scipy.integrate import quad, nquad
from CalcQSB import CalcQSB
from dislstress import dislstress
import matplotlib.pyplot as plt
from matplotlib import cm
import scipy.interpolate
...
# evaluate over a grid
num = 50
npts =200
xi, yi = np.linspace(-num, num, npts), np.linspace(-num, num, npts)
X, Y = np.meshgrid(xi, yi)
Z = np.zeros(matcompat.size(X))
xtrans, ytrans = np.meshgrid(xi, yi)
... more code for determining correct values in the Z array.
v = np.linspace(np.amin(Z), np.amax(Z), 15, endpoint=True)
plt.imshow(Z, vmin=np.amin(Z), vmax=np.amax(Z), origin='lower',
extent=[np.amin(xtrans), np.amax(xtrans), np.amin(ytrans), np.amax(ytrans)])
plt.colorbar(ticks=v)
CS = plt.contour(xtrans, ytrans, Z,20,colors='k')
plt.show()
If someone can help, I would be most grateful.
Thanks
Luke

Griding with python

I am trying to plot a picture like this in python.
I have three parameters for ploting.
x:
[ 0.03570416 0.05201517 0.05418171 0.01868341 0.07116423 0.07547471]
y:
[-0.32079484 -0.53330218 -1.02866859 -0.94808545 -0.51682506 -0.26788337]
z:
[-0.32079484 -0.53330218 -1.02866859 -0.94808545 -0.51682506 -0.26788337]
so x is x-axis and y is y-axis. however z is the intensity of the pixel.
I come up with this code:
z = np.array(reals)
x = np.array(ra)
y = np.array(dec)
nrows, ncols = 10, 10
grid = z.reshape((nrows, ncols))
plt.imshow(grid, extent=(x.min(), x.max(), y.max(), y.min()), interpolation='nearest', cmap=cm.gist_rainbow)
plt.title('This is a phase function')
plt.xlabel('ra')
plt.ylabel('dec')
plt.show()
However I get this error:
grid = z.reshape((nrows, ncols))
ValueError: total size of new array must be unchanged
ra, dec and reals are normal arrays with the same size. I calculated them before and then I create the numpy arrays with them
The data you show is not consistent with making an image, but you could make a scatter plot with it.
The two basic types of plots for z values at (x,y) coordinate pairs are:
scatter plots, where for each (x,y) pair, a z-value is specified.
image (imshow, pcolor, pcolormesh, contour), where an x-axis with m regularly spaced values, and a y-axis with n regularly spaced values are specified, and then an array of z-values with size (m,n) is given.
Your data looks more like the former type, so I'm suggesting a scatter plot.
Here's what a scatter plot looks like (btw, your y and z values are the same, which if probably a mistake):
import numpy as np
import matplotlib.pyplot as plt
x = np.array([ 0.03570416, 0.05201517, 0.05418171, 0.01868341, 0.07116423, 0.07547471])
y = np.array([-0.32079484, -0.53330218, -1.02866859, -0.94808545, -0.51682506, -0.26788337])
z = np.array([-0.32079484, -0.53330218, -1.02866859, -0.94808545, -0.51682506, -0.26788337])
plt.scatter(x, y, c=z, s =250)
plt.show()

Categories