How to plot multiple parametric plots in a single figure? - python

I need to plot a family of parametric curves in a single figure for each alpha values as mentioned in the code
import numpy as np
from sympy import *
from sympy.plotting import plot_parametric
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
a=45
t = symbols('t')
for alpha in np.arange(0.5,3,.1):
M=a*sqrt(cos(2*t)+sqrt(pow(alpha,4)+pow(sin(2*t),2)))
x = M*cos(t)
y = M*sin(t)
plot_parametric(x, y, (t, 0, 2*pi))
The code returns a sequence of 2D plots for each alpha value. Instead, I want to plot the whole set of plots in one figure, something like this image attached
Any solution?

from numpy import arange, cos, linspace, pi, sin, sqrt
from matplotlib.pyplot import colorbar, Normalize, show, subplots
from matplotlib.cm import ScalarMappable, viridis
a=45
t= linspace(0, 2*pi, 2001)
norm = Normalize(vmin=0.5, vmax=3)
cmap = viridis
sm = ScalarMappable(cmap=cmap, norm=norm)
fig, (ax_xy, ax_tM) = subplots(1, 2, figsize=(10, 4), constrained_layout=1)
for alpha in arange(0.5,3,.1):
color = cmap(norm(alpha))
M=a*sqrt(cos(2*t)+sqrt(pow(alpha,4)+pow(sin(2*t),2)))
x = M*cos(t)
y = M*sin(t)
ax_tM.plot(t, M, color=color)
ax_xy.plot(x, y, color=color)
colorbar(sm, aspect=40)
show()

Related

How to plot a 2d structured mesh in matplotlib

I am trying to use matplotlib to plot the structured mesh (See the figure below)
import numpy as np
import matplotlib.pyplot as plt
x, y = np.meshgrid(np.linspace(0,1, 11), np.linspace(0, 0.6, 7))
plt.scatter(x, y)
plt.show()
I got a discrete points, but I have no idea how to connect them to get something like this:
The desired result is:
I appreciate any help
I'd use two linecollections for this:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
x, y = np.meshgrid(np.linspace(0,1, 11), np.linspace(0, 0.6, 7))
plt.scatter(x, y)
segs1 = np.stack((x,y), axis=2)
segs2 = segs1.transpose(1,0,2)
plt.gca().add_collection(LineCollection(segs1))
plt.gca().add_collection(LineCollection(segs2))
plt.show()
Also see How to plot using matplotlib (python) colah's deformed grid?
Because if the grid is not deformed, it would be more efficient to draw a single linecollection, like
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
x, y = np.meshgrid(np.linspace(0,1, 11), np.linspace(0, 0.6, 7))
segs1 = np.stack((x[:,[0,-1]],y[:,[0,-1]]), axis=2)
segs2 = np.stack((x[[0,-1],:].T,y[[0,-1],:].T), axis=2)
plt.gca().add_collection(LineCollection(np.concatenate((segs1, segs2))))
plt.autoscale()
plt.show()
You can np.transpose the points you already have, while using a line plot() rather than scatter().
import numpy as np
import matplotlib.pyplot as plt
x, y = np.meshgrid(np.linspace(0,1, 11), np.linspace(0, 0.6, 7))
plt.plot(x, y) # use plot, not scatter
plt.plot(np.transpose(x), np.transpose(y)) # add this here
plt.show()
You can of course have it colored in black with c='k'
IIUC, vlines and hlines would do:
plt.vlines(np.linspace(0,1,11), 0, 0.6)
plt.hlines(np.linspace(0,0.6,7), 0, 1)
If you already have mesh x,y:
plt.vlines(x[0], *y[[0,-1],0])
plt.hlines(y[:,0], *x[0, [0,-1]])
Out:

Generate 3D Surface Map from Skimage Elevation Map (2D numpy.ndarray)

In the skimage Segmentation tutorial, a 3D surface plot of the elevation map generated from the sobel function was plotted.
>>> from skimage.filters import sobel
>>> elevation_map = sobel(coins)
Question: elevation_map appears to be a 2D numpy.ndarray. How do we generate the 3D map shown using this?
This is likely produced using Paraview/VTK;
Try to play around the following:
from skimage import data
from skimage.filters import sobel
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import cm
from scipy.ndimage import zoom
coins = data.coins()
coins = zoom(coins, 10)
elevation_map = sobel(coins)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
m, n=elevation_map.shape
X, Y = np.meshgrid(np.arange(n), np.arange(m))
ax.plot_surface(X, Y, elevation_map, cmap=cm.viridis, antialiased=False)
ax.axis("off")
ax.set_facecolor('black')
plt.show()

Drawing a logarithmic spiral in three axes in Python

I try to draw a logarithmic spiral in the form of a spring in three axes.
Using the parametric equations:
x=a*exp(b*th)*cos(th)
y=a*exp(b*th)*sin(th)
Using the code:
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
from math import exp,sin,cos
from pylab import *
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
n=100
a=0.5
b=0.20
th=np.linspace(0, 500, 10000)
x=a*exp(b*th)*cos(th)
y=a*exp(b*th)*sin(th)
ax.plot(x, y)
ax.legend()
plt.show()
I get:
However, I would like to stretch the spiral along the Z axis to get a result similar to the following, but using the logarithmic spiral as the basis:
How can you do it? How do you modify the function by adding a condition to the Z axis?
Which z to take it's a bit up to you. From the plot itself it's hard to say but my guess is that it's linear (the simplest option).
Taking your code and adding the z axis you can do something like this
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
from math import exp,sin,cos
from pylab import *
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
a=0.05
b=0.10
# took the liberty of reducing the max value for th
# as it was giving you values of the order of e42
th=np.linspace(0, 50, 10000)
x=a*exp(b*th)*cos(th)
y=a*exp(b*th)*sin(th)
z=np.linspace(0,2, 10000) # creating the z array with the same length as th
ax.plot(x, y, z) # adding z as an argument for the plot
ax.legend()
plt.show()
You can play with your a and b parameters to get the elliptical shape you want. You can also play with the definition of z to make it exponential, or logarithmic in growth.. or something else entirely.
BTW, your imports are a bit redundant and probably some funtions from one package are being shadowed by another package.
Since 95% of the points of the spiral are condensed in a single point in the middle of the plot it would make sense to restrict the plotted range to something like
th=np.linspace(475, 500, 10000)
Then using a linear range of z values would directly give you the desired curve in the plot, by simply specifying that range in the plot function, plot(x,y,z).
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
a=0.5
b=0.20
th=np.linspace(475, 500, 10000)
x=a*np.exp(b*th)*np.cos(th)
y=a*np.exp(b*th)*np.sin(th)
z = np.linspace(0,2, len(th))
ax.plot(x, y, z)
#ax.legend()
plt.show()
Note that I cleaned up the imports here. E.g. if you import cos from math but later import everything (*) from pylab into the namespace, the function cos that is used is the numpy cos function, not the one from math (the math cos function would not work here anyways). In general: don't use pylab at all.

The plot3d figure in matplotlib is somewhat canted

I am using matplotlib to get a water fall figure, but the results look very strange. Anyone have any idea what could be wrong with it?
Here I attached the figures. The second one is the same data but in an ordinary plot. In the waterfall figure, why the color is not fully filled?
Here is the code:
def water_fall_1(x,y,Z):
#x=[...]
#y=[...]
#Z=[[z1],[z2],...z[ny]]
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
from matplotlib.colors import colorConverter
from mpl_toolkits.mplot3d import Axes3D
figs=[]
for jc in range(len(y)):
figs.append(list(zip(x,Z[jc])))
x=np.array(x)
y=np.array(y)
Z=np.array(Z)
xmin=np.floor(np.min((x.astype(np.float))))
xmax=np.ceil(np.max((x.astype(np.float))))
ymin=np.min((y.astype(np.float)))
ymax=np.max((y.astype(np.float)))
zmin=(np.min((Z.astype(np.float))))
zmax=np.max((Z.astype(np.float)))
fig=plt.figure()
ax = Axes3D(fig)
poly = PolyCollection(figs, facecolors=colorConverter.to_rgba("r", alpha=0.5))
ax.add_collection3d(poly, zs=y.astype(np.float), zdir='y')
ax.set_xlim(xmin,xmax)
ax.set_ylim(ymin,ymax)
ax.set_zlim(zmin,zmax)
ax.set_xlabel('$\omega$')
ax.set_ylabel('$T$')
#ax.set_zlabel('$\\frac{1}{2}$')
plt.show()
The curve is fully filled. I.e. the surface in between the points of the curve is red.
Consider the following example:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
from mpl_toolkits.mplot3d import Axes3D
bottom=-0.3
x = np.linspace(0,6, num=50)
z = np.sinc(x-4)
verts = zip(x,z)
#verts=verts + [(x.max(),bottom),(x.min(),bottom)]
fig=plt.figure()
ax = Axes3D(fig)
poly = PolyCollection([verts], facecolors="r", alpha=0.5)
ax.add_collection3d(poly, zs=1, zdir='y')
ax.set_xlim(x.min(),x.max())
ax.set_ylim(0,2)
ax.set_zlim(bottom,z.max())
plt.show()
which produces the following plot, where everything between the points of the curve is filled as expected.
If we now want to have the area between the curve and some bottom line filled, we would need to add some points,
verts=verts + [(x.max(),bottom),(x.min(),bottom)]
such that the bottom line is part of the curve and can thus be filled as well.

Python 2D plots as 3D (Matplotlib)

Python plot in Matplotlib: I have a number of samples taken daily at the same time which shows a change in measurement (of something). This may be shown as a 2D plot (below left), but as the sample number increases I'd like to display this data as a 3D plot which is stacked (below right image) - this image is for illustration only.
For a starting point my code is below, how may I achieve this?
import numpy as np
import pylab as plt
t = np.arange(1024)*1e-6
y1 = np.sin(t*2e3*np.pi)
y2 = 0.5*y1
y3 = 0.25*y1
plt.plot(t,y1,'k-', label='12/03/14')
plt.plot(t,y2,'r-', label='13/03/14')
plt.plot(t,y3,'b-', label='14/03/14')
plt.xlabel('Time/sample no.')
plt.ylabel('Pk-pk level (arbitrary units)')
plt.legend()
plt.grid()
plt.show()
Would it be something like this?
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
from matplotlib.colors import colorConverter
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
zs = [0.0, 1.0, 2.0]
t = np.arange(1024)*1e-6
ones = np.ones(1024)
y1 = np.sin(t*2e3*np.pi)
y2 = 0.5*y1
y3 = 0.25*y1
verts=[list(zip(t, y1)), list(zip(t, y2)), list(zip(t, y3))]
poly = PolyCollection(verts, facecolors = ['r','g','b'])
poly.set_alpha(0.7)
ax.add_collection3d(poly, zs=zs, zdir='y')
ax.set_xlabel('X')
ax.set_xlim3d(0, 1024e-6)
ax.set_ylabel('Y')
ax.set_ylim3d(-1, 3)
ax.set_zlabel('Z')
ax.set_zlim3d(-1, 1)
plt.show()

Categories