Related
I am having some trouble with a polar chart I am working on. The figure I should get is an eight-shape (some friends of mine plotted the data in Origin and Excel and it does work), but it looks like the code is not properly written. By looking at the figure, I see that the code is not taking into account the angles I am writing (theta array), but I don't know why it happens. I've already tried some more codes and writing the angles in radians, but nothing seems to work.
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
import matplotlib.pyplot as plt
from pylab import *
import numpy as np
r = np.array([11.7,12.1,10.1,6.6,3.1,1.5,2.3,5.2,
8.7,11.5,12,10.1,6.6,3.3,1.5,2.3,5.3,9.2,11.9])
theta =np.array([0,20,40,60,80,100,120,140,160,180,
200,220,240,260,280,300,320,340,360])
ax = plt.subplot(111, projection='polar')
ax.plot(theta,r)
ax.set_rmax(13)
ax.set_rticks([2,4,6,8,10,12]) # less radial ticks
ax.set_rlabel_position(-40) # get radial labels away from plotted line
ax.grid(True)
ax.set_title("A line plot on a polar axis", va='bottom')
plt.show()
I've also tried this:
r3 = np.array([11.7,12.1,10.1,6.6,3.1,1.5,2.3,5.2,
8.7,11.5,12,10.1,6.6,3.3,1.5,2.3,5.3,9.2,11.9])
theta3 =np.array([0,20,40,60,80,100,120,140,160,180,
200,220,240,260,280,300,320,340,360])
fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
c = ax.scatter(theta3, r3)
Counter-intuitively, while the polar plot shows its theta axis in degrees, it actually expects the coordinates to be in radians:
import numpy as np
import matplotlib.pyplot as plt
r = np.array([11.7,12.1,10.1,6.6,3.1,1.5,2.3,5.2,
8.7,11.5,12,10.1,6.6,3.3,1.5,2.3,5.3,9.2,11.9])
theta =np.array([0,20,40,60,80,100,120,140,160,180,
200,220,240,260,280,300,320,340,360], dtype=float) # making sure it is float
# convert to radians
# theta *= np.pi/180.
theta = np.deg2rad(theta)
ax = plt.subplot(111, projection='polar')
ax.plot(theta,r)
ax.grid(True)
ax.set_title("A line plot on a polar axis", va='bottom')
plt.show()
I have not managed to find any place in the documentation, where this is explicitly stated (only examples where it is done correctly).
The weird pictures you got came from the fact that all values above 2pi are folded back into the range 0...2pi. So e.g. 20 % 2pi = 1.15, which is about 65 degrees when converted, which is where the second value is actually located in your plot.
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).
I am looking to make a plot similar to the one found here, with the simple difference that I would like to to set the distance from the center for each point. Ie, given a slice of the plot is a circle, where I would like each of the points to be at a definable distance from the center.
What I a starting with, given a simple modification of the previously mentioned answer:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib
import numpy as np
from scipy.interpolate import interp1d
from matplotlib import cm
from matplotlib import pyplot as plt
step = 0.04
maxval = 1.0
fig = plt.figure()
ax = Axes3D(fig)
# u here would define the desired distance from radial axis
# u=np.array([0,1,2,1,0,2,4,6,4,2,1])
v=np.array([4,4,6,3,6,4,1,4,4,4,4])
r=np.array([0,1,2,3,4,5,6,7,8,9,10])
f=interp1d(r,u)
# walk along the circle
p = np.linspace(0,2*np.pi,len(r))
R,P = np.meshgrid(r,p)
# transform them to cartesian system
X,Y = R*np.cos(P),R*np.sin(P)
Z=f(R)
ax.scatter(X, Y, Z)#, rstride=1, cstride=1, cmap=cm.jet)
ax.set_xticks([])
fig.savefig(str(output_prefix + '3d..png'), dpi=(200))
What I would like to plot (apologies for the blurred sketch):
I have tried using interp2d to add the u variable commented out above, but no luck. Changing Z to the array u threw the error that X, Y, and Z must be the same size ("Argument 'zs' must be of same size as 'xs' ", understandably as X and Y are now interpolated) What do I need to do? Any tips would be appreciated!
I don't know exactly what you meant in your question.
I made v to be the offset of the center of the circles in x-axis.
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from scipy.interpolate import interp1d
from matplotlib import pyplot as plt
step = 0.04
maxval = 1.0
fig = plt.figure()
ax = Axes3D(fig)
# v here would define the desired distance from radial axis
u=np.array([0,1,2,1,0,2,4,6,4,2,1])
v=np.array([4,4,6,3,6,4,1,4,4,4,4])
r=np.array([0,1,2,3,4,5,6,7,8,9,10])
f=interp1d(r,u)
# walk along the circle
V = np.tile(v, (len(u), 1))
p = np.linspace(0,2*np.pi,len(r))
R,P = np.meshgrid(r,p)
# transform them to cartesian system
X,Y = V + R*np.cos(P),R*np.sin(P)
Z=f(R)
ax.scatter(X, Y, Z)#, rstride=1, cstride=1, cmap=cm.jet)
ax.set_xticks([])
plt.show()
I have a 3d line plot of the solar spectrum, which I plotted using the command,
from mpl_toolkits.mplot3d.axes3d import Axes3D
from matplotlib.collections import PolyCollection, LineCollection
from matplotlib.colors import colorConverter, ListedColormap, BoundaryNorm
import matplotlib.cm as cm
fig = plt.figure(figsize(15, 8))
ax = fig.gca(projection='3d')
x = SpectrumDF['Wavelength']
z = SpectrumDF['DNI']
y = SpectrumDF['TESTNUM']
ax.plot(x, y, z)
ax.set_xlabel('Wavelength')
ax.set_ylabel('Test Number')
ax.set_zlabel('Intensity')
The resultant plot is solid blue and takes whichever individual color I give in the function: plot( ).
I have been trying to create a color gradient along the z-axis, intensity, without any success.
I have around 500 test numbers, each has 744 data points.
Thank you for the help!
This wouldn't let me post images because I don't have enough reputation. Anyway, here's the link to the plot I get using this code https://plus.google.com/106871046257785761571/posts/fMYsDF5wAQa
Using the example - Line colour of 3D parametric curve in python's matplotlib.pyplot - I got a scatter plot with color gradient along the z axis - here's the link to the image of that plot - https://plus.google.com/u/0/106871046257785761571/posts/SHTsntgQxTw?pid=6133159284332945618&oid=106871046257785761571
I used the following command:
fig = plt.figure(figsize(15,8))
ax = fig.gca(projection='3d')
x = FilteredDF['Wavelength']
z = FilteredDF['DNI']
y = FilteredDF['TESTNUM']
ax.scatter(x, y, z, c=plt.cm.jet(z/max(z)))
ax.set_xlabel('Wavelength')
ax.set_ylabel('Test Number')
ax.set_zlabel('Intensity')
plt.show()
I am still working on getting a colored line plot because I have a lot of points, which makes scatter plot very slow to work with.
Thank you
I am using matplotlib for doing this
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')
plt.show()
Now this builds a graph that is horizontal in the 3d space. How do I make the graph vertical so that it faces the user?
What I want to do is build multiple such vertical graphs that are separated by some distance and are facing the user.
bp's answer might work fine, but there's a much simpler way.
Your current graph is 'flat' on the z-axis, which is why it's horizontal. You want it to be vertical, which means that you want it to be 'flat' on the y-axis. This involves the tiniest modification to your code:
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
# put 0s on the y-axis, and put the y axis on the z-axis
ax.plot(xs=x, ys=[0]*len(x), zs=y, zdir='z', label='ys=0, zdir=z')
plt.show()
Then you can easily have multiple such graphs by using different values for the ys parameter (for example, ys=[2]*len(x) instead would put the graph slightly behind).
Mayavi, in particular the mlab module, provides powerful 3D plotting that will work on large and or complex data, and should be easy to use on numpy arrays.
You can set the view angle of the 3d plot with the view_init() function. The example below is for version 1.1 of matplotlib.
from mpl_toolkits.mplot3d import axes3d
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')
ax.view_init(90, -90)
plt.show()
According to the documentation you want to use the ax.plot_surface(x,y,z) method. More information and chart types here.
The following should work:
x = [1,2,3]
y = [4,5,6]
z = [7,8,9]
data = zip(x,y,z)
#map data on the plane
X, Y = numpy.meshgrid(arange(0, max(x), 1), arange(0, max(y), 1))
Z = numpy.zeros((len(Y), len(X)), 'Float32')
for x_,y_,z_ in data:
Z[x_, y_] = z_ #this should work, but only because x and y are integers
#and arange was done with a step of 1, starting from 0
fig = p.figure()
ax = p3.Axes3D(fig)
ax.plot_surface(X, Y, Z)