How to do a 3D circle in Matplotlib - python

I am a beginner in Python. I'm trying to plot a circle using matplotlib that has tangent to Z axis. I know how to draw a sphere in 3D but don't know how to draw a circle/ring in 3D plot. Can someone help me with the code? Thanks in advance!

You need the usual imports, plus the 3D toolkit
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
You need a 3D enabled axes object
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
You need a circle, contained in the plane y-z
theta = np.linspace(0, 2 * np.pi, 201)
y = 10*np.cos(theta)
z = 10*np.sin(theta)
now we can plot the original circle and, as an example, a number of circles rotated about the z-axis and whose centers are also placed at a fixed distance (equal to the c ircles'radius) from the z-axis, so that they are tangent to it
for i in range(18):
phi = i*np.pi/9
ax.plot(y*np.sin(phi)+10*np.sin(phi),
y*np.cos(phi)+10*np.cos(phi), z)
eventually we place a vertical axis and a legend
ax.plot((0,0),(0,0), (-10,10), '-k', label='z-axis')
ax.legend()
It's time to see what we got
plt.show()

mpl_toolkits.mplot3d.art3d
https://matplotlib.org/3.2.1/gallery/mplot3d/pathpatch3d.html was mentioned
in a comment, the example can be minimized to:
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import mpl_toolkits.mplot3d.art3d as art3d
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Draw a circle on the x=0 'wall'
p = Circle((5, 5), 3)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=0, zdir="x")
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_zlim(0, 10)
plt.show()
which gives:
This is a bit nicer than https://stackoverflow.com/a/56871467/895245 as it uses a higher level Circle object directly, instead of requiring you to explicitly plot the lines.
Unfortunately, 3D support in matplotlib is a bit limited as mentioned in the documentation itself, and you have to do some extra work to plot on planes not parallel to the main coordinate plane: How can matplotlib 2D patches be transformed to 3D with arbitrary normals?
Tested on matplotlib==3.2.2.

Related

How to scale and set up RGB ortogonal axes in a 3D plot?

I'm trying to plot a wave function over one dimension but it has real and imaginary parts, so I did a 3D plot animation of it. This is a screenshot:
The main thing I would like to do is to spread it along the x-axis (which now is vertical) so it doesn't look squeezed. Also, it would be nice to set it up in a set of 3 RGB axes that intersect at the point (0,0,0). In the documentation I couldn't find any straight forward way to do this. I'm attaching the part of the code I'm using to animate it:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import mpl_toolkits.mplot3d.axes3d as p3
fig = plt.figure()
ax = fig.gca(projection='3d')
line, = ax.plot(REAL[0,:],IMAG[0,:],x,"r",linewidth=0.5)
def animacio(i):
ax.collections.clear()
line.set_data(REAL[i,:],IMAG[i,:])
line.set_3d_properties(x, 'z')
return line,
ani = animation.FuncAnimation(fig,animacio,interval=50, frames=Nt,repeat=True)
nom = 'EvoluciĆ³_'
ani.save(str(nom)+'['+str(V0)+','+str(L)+','+str(l)+','+str(xi)+','+str(sigmax)+','+str(T)+']'+'.mp4', writer="ffmpeg", dpi=300)
plt.show()
print('Animation saved as: '+str(nom)+'['+str(V0)+','+str(L)+','+str(l)+','+str(xi)+','+str(sigmax)+','+str(T)+']'+'.mp4')
You can add colored lines to the plot, just by giving start and end points and assigning a color. The limits for the 'up'-axis can be set by ax.set_zlim. I created a demo curve that roughly resembles yours.
import numpy as np
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
x = np.linspace(-10, 10, 1000)
y = np.sin(10*x)/(x*x+1)
z = np.cos(10*x)/(x*x+1)
ax = plt.axes(projection='3d')
ax.plot3D([0,0], [0,0], [-10,10], color='crimson')
ax.plot3D([0,0], [-1,1], [0,0], color='limegreen')
ax.plot3D([-1,1], [0,0], [0,0], color='dodgerblue')
line, = ax.plot3D(y, z, x, color='blueviolet')
ax.set_zlim(-1, 1)
plt.show()
At the left the plot without limiting, at the right with limits:
To get a more elongated view, you could use something like:
plt.gcf().set_size_inches(4, 12)

Color code points on 3D scatter plot with 350 points

I am doing PCA, that has 350 points to plot. I have successfully plotted them on a 3D plot. I want to color them to tell which point is which on my 3D plot. Is it possible to color each one? Maybe color points 1 to 50 different shades of green, then 51 to 100 different shades of red, etc? And the lighter the shade of color the smaller the number is?
You mentioned you have been trying to do this with matplotlib and ax.scatter. I think for your example all the functionality you need is already built into ax.scater, with the c input argument. For example, does the following meet your requirements?
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
# Some dummy data
xval = np.random.randn(350)
yval = np.random.randn(350)
zval = np.exp( - (xval**2 + yval**2)/0.5)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
cax = ax.scatter(xval, yval, zval, cmap=plt.cm.viridis, c=zval)
fig.colorbar(cax)

Plotting cross section through 3d scatter plot in python

I have a simple script, that plots lat,lon and depth of points in 3D.
Is there a python module or some simple solution I could use to get a cross section over this points?
Cross section should be a vertical plane, for which I would set two coordinates (in plane view), depth and thickness (how many km in each direction perpendicular to the cross sections I want the data).
So something like point1(lat1,lon1), point2(lat2,lon2), depthmin, depthmax, thicknes
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
lats=[]
lons=[]
depts=[]
file_open=open("catZMAPVpVs")
for fo in file_open:
element=fo.split("\t")
lons.append(float(element[0]))
lats.append(float(element[1]))
depts.append(-float(element[6]))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(lons,lats, depts, c='r', marker='o')
ax.set_xlabel('lon')
ax.set_ylabel('lat')
ax.set_zlabel('depth')
plt.show()
thanks!

mplot3D fill_between extends over axis limits

I have questions related to creating a simple lineplot in Python with mplot3D where the area under the plot is filled. I am using Python 2.7.5 on RedHatEnterprise 7.2, matplotlib 1.2.0 and numpy 1.7.2.
Using the code below, I am able to generate a line plot. This is displayed as expected with the beginning / end of the plot set by the limits of the imported data set.
I am then trying to fill the area between the line plot and -0.1 using the answer given by Bart from Plotting a series of 2D plots projected in 3D in a perspectival way. This works, however, the filled area is continued beyond the limits of the data set. This is also the case when running the example from the link.
This screen shot shows the plot generated with filled area extending beyond the set axis limits.
How do I achieve that the filled area is only the range of the data set or the axis limits whichever is smaller?
How do I add a legend for those plots onto the figure?
Code as follows:
from numpy import *
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import Axes3D
x,y = genfromtxt("data.dat",unpack=True)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.add_collection3d(plt.fill_between(x,y,-0.1, color='orange', alpha=0.3,label="filled plot"),1, zdir='y')
ax.plot(x,y,1,zdir="y",label="line plot")
ax.legend()
ax.set_xlim3d(852.353,852.359)
ax.set_zlim3d(-0.1,5)
ax.set_ylim3d(0,2)
ax.get_xaxis().get_major_formatter().set_useOffset(False)
plt.show()
I don't know how to put fill_between working the way you want it to, but I can provide an alternative using a 3D polygon:
from numpy import *
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection # New import
#x,y = genfromtxt("data.dat",unpack=True)
# Generated some random data
w = 3
x,y = np.arange(100), np.random.randint(0,100+w,100)
y = np.array([y[i-w:i+w].mean() for i in range(3,100+w)])
z = np.zeros(x.shape)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
#ax.add_collection3d(plt.fill_between(x,y,-0.1, color='orange', alpha=0.3,label="filled plot"),1, zdir='y')
verts = [(x[i],z[i],y[i]) for i in range(len(x))] + [(x.max(),0,0),(x.min(),0,0)]
ax.add_collection3d(Poly3DCollection([verts],color='orange')) # Add a polygon instead of fill_between
ax.plot(x,z,y,label="line plot")
ax.legend()
ax.set_ylim(-1,1)
plt.show()
The code above generates some random data. Builds vertices from it and plots a polygon with those vertices. This will give you the plot you wish (but does not use fill_between). The result is:

How to make a centered bubble chart in python/matplot lib

I'm trying to make a centered bubble chart in matplotlib / python similar to this.
Some people have called it a "bottom aligned bubble chart", So far, I've basically found a way to do a concentric circle scatter plot.
%matplotlib inline
import matplotlib.pyplot as plt
s = [ 50000.,10478.2, 4733.4,3185.3,2484.7,2310.9]
x = [1]*len(s)
y = [0]*len(s);
plt.scatter(x,y,s=s);
plt.show()
Any ideas on how to line up the bottom edges of these concentric cirlces?
I would interact with matplotlib artists directly. I would also set the radius -- and therefore center -- of each circle the square root of the populations.
This is because, for a circle, A ~ r^2, so you'll heavily distort the size differences if r ~ population.
So all that said:
%matplotlib inline
import numpy
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import seaborn
seaborn.set(style='white')
populations = numpy.sqrt([50000., 10478.2, 4733.4, 3185.3, 2484.7, 2310.9])
cp = seaborn.color_palette('Blues_r', n_colors=len(populations))
fig, ax = plt.subplots()
for n, p in enumerate(populations):
circle = plt.Circle((1, p), radius=p, facecolor=cp[n])
ax.add_artist(circle)
ax.set_xlim(-max(populations), max(populations))
ax.set_ylim(0, 2 * max(populations))
ax.set_aspect('equal')
plt.show()
Gives me this:

Categories