Plotting around a circunference - python

Is there any possibilitie using matplotlib to get something like this:
signlar with circular path
The plot is a signal acquired but wrapped or plotted through a circular path. Any idea?
original signal:
Original signal

You could try a polar plot:
import numpy as np
import matplotlib.pyplot as plt
circum = np.linspace(0,2*np.pi, 1000)
rad = 2+np.random.random(1000)
fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
ax.plot(circum, rad)
ax.set_rlim(0,5)
fig.show()
You would need to convert your x-y data into r-theta data though (this isn't too hard).

Related

Matplotlib Polar scatter plot doesn't show all points

I'm trying to do a simple polar scatter plot in Matplotlib, here is my code:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, polar = True)
c = ax.scatter([-1.3,.4,-.2],[1,1.5,2])
For some reason, the plot doesn't include all of the points, it only shows me the point with radius 1. How do I make it automatically show all of the points, or how can I fix the radius of the plot myself? I am running this in a Jupyter notebook, if that makes any difference.
This is what I suggest if I understand your question correctly this code plots all the points automatically
Here I am just using for loop to loop through all the points
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, polar = True)
a = [1,1.5,2]
for idx, i in enumerate([-1.3,.4,-.2]):
c = ax.scatter([i],[a[idx]])
plt.show()

Mayavi - hide "diagonal" lines in wireframe

I've just started using mayavi and was wondering if there's a way to plot the wireframe representation of the surface that looks like the one I'm used from matplotlib 3d.
Minimal example:
# sphere example
import numpy as np
from mayavi import mlab
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# azimut and polar angle
phi = np.linspace(0,2*np.pi,10)
theta = np.linspace(0,np.pi,10)
phi, theta = np.meshgrid(phi,theta)
# cartesian coordinates
x = np.cos(phi)*np.sin(theta)
y = np.sin(phi)*np.sin(theta)
z = np.cos(theta)
#plotting using matplotlib
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(x,y,z)
#plotting using mayavi
mlab.mesh(x,y,z,representation='wireframe', color=(0,0,1))
As you can see the outputs differ: matplotlib prints the lines with constant phi resp theta. Mayavi however, also prints diagonal lines (highlighted in yellow) connecting those paths.
I prefer the matplotlib version. Is there a way to achieve the same wireframe with mayavi?
TIA
Use mlab.surface() instead of mlab.mesh()

mpldatacursor Scatter Plot point colour information

I have a scatter plot with a colour scaling where each plotted point is associated with another value. This is a lazy workaround to make a "countour plot" style image without having to regularise data points. To make analysis easier I am using mpldatacursor to generate interactive annotations on the plot, and I have a custom formatter which is displaying co-ordinates just fine:
datacursor(scatter,
formatter='$T=${x:.2f}$^\circ$C\n$I=${y:.2f}$\,$mA\n$\Delta F=$$\,$THz'.format,
draggable=True)
but what I really want is for that third line, $\Delta F=$$\,$THz, to include a statement that returns the value associated with the colour map at that point. Does anyone know what kwargs I should use to achieve this?
EDIT: MWE
from mpldatacursor import datacursor
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
scatter = ax.scatter(np.random.random(100),
np.random.random(100),
c=np.random.random(100),
s=0.5)
cb = plt.colorbar(scatter, label="Colour")
datacursor(scatter,
formatter='$T=${x:.2f}$^\circ$C\n$I=${y:.2f}$\,$mA\n$\Delta F=$$\,$THz'.format,
draggable=True)
You will need to convert the index of the picked point to the value to be shown. Therefore the scatter's colors should be publicly available, such that the ind of the pick_event can index it and return the value at the picked point.
from mpldatacursor import datacursor
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.random.random(100)
y = np.random.random(100)
c = np.random.random(100)
scatter = ax.scatter(x, y, c=c, s=1)
cb = plt.colorbar(scatter, label="Colour")
def fmt(**dic):
tx = '$T=${x:.2f}$^\circ$C\n$I=${y:.2f}$\,$mA\n$\Delta F=${z:.2f}$\,$THz'
dic.update({"z" : c[dic["ind"][0]]})
return tx.format(**dic)
datacursor(scatter, formatter=fmt, draggable=True)
plt.show()

matplotlib contours labels in 3D

Is there any way to put contours labels in 3D plots? Clabel is apparently not implemented in 3D
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
x,y = np.meshgrid(np.linspace(-1,1,10),np.linspace(-1,1,10))
z=-(x**2+y**2)
fig,ax = plt.subplots()
C=ax.contour(x,y,z)
ax.clabel(C)
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
C=ax.contour(x,y,z,zdir='z',offset=-2)
ax.clabel(C)
As clabel is not implemented and the return value is None, there is indeed no point in calling clabel() for the time being. You can use the C.levels attribute to manually add labels to the graph.
It won't have the nice inline feature that hides the contour under the labels though.

How to make a non-square contourf-plot

I'm using contourf in pyplot to plot some scalar data, but when my domain is non-square i feel like the data is misrepresented because it always plots it in a square (though the axis values will increase faster on one side.) How can i force the axis scaling to be equal, such that if my domain is twice as long in the x-direction the image is actually plotted in a rectangle with this property?
I'm doing something like this:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
contour = ax.contourf(X,Y,Z)
fig.colorbar(contour)
fig.canvas.draw()
Using ax.set_aspect:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
x=np.r_[-10:10:100j]
y=np.r_[-20:20:100j]
z= np.add.outer(x*x, y*y)
contour=ax.contour(x,y,z)
fig.colorbar(contour)
ax.set_aspect('equal')
# ax.axis('equal')
plt.show()
yields
while changing ax.set_aspect('equal') to
ax.axis('equal')
yields
This might help:
ax = fig.add_subplot(111, aspect="equal")
You need to change the axis setting:
axis('equal')
See all of the axis settings here:
http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axis

Categories

Resources