Question

I need to generate a stack of 2D polar plots (a 3D cylindrical plot) so that I can view a distorted cylinder. I want to use matplotlib since I already have it installed and want to distribute my code to others who only have matplotlib. For example, say I have a bunch of 2-D arrays. Is there any way I can do this without having to download an external package? Here's my code.

#!usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-180.0,190.0,10)
theta = (np.pi/180.0 )*x    # in radians

A0 = 55.0
offset = 60.0

R = [116.225,115.105,114.697,115.008,115.908,117.184,118.61,119.998,121.224,122.216,\
122.93,123.323,123.343,122.948,122.134,120.963,119.575,118.165,116.941,116.074,115.66\
,115.706,116.154,116.913,117.894,119.029,120.261,121.518,122.684,123.594,124.059,\
123.917,123.096,121.661,119.821,117.894,116.225]

fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8],polar=True)     # Polar plot
ax.plot(theta,R,lw=2.5)
ax.set_rmax(1.5*(A0)+offset)
plt.show()

I have 10 more similar 2D polar plots and I want to stack them up nicely. If there's any better way to visualize a distorted cylinder in 3D, I'm totally open to suggestions. Any help would be appreciated. Thanks!

Était-ce utile?

La solution

If you want to stack polar charts using matplotlib, one approach is to use the Axes3D module. You'll notice that I used polar coordinates first and then converted them back to Cartesian when I was ready to plot them.

from numpy import *
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

n = 1000

fig = plt.figure()
ax = fig.gca(projection='3d')

for k in linspace(0, 5, 5):
    THETA = linspace(0, 2*pi, n)
    R     = ones(THETA.shape)*cos(THETA*k)

    # Convert to Cartesian coordinates
    X = R*cos(THETA)
    Y = R*sin(THETA)

    ax.plot(X, Y, k-2)

plt.show()

enter image description here

If you play with the last argument of ax.plot, it controls the height of each slice. For example, if you want to project all of your data down to a single axis you would use ax.plot(X, Y, 0). For a more exotic example, you can map the height of the data onto a function, say a saddle ax.plot(X, Y, -X**2+Y**2 ). By playing with the colors as well, you could in theory represent multiple 4 dimensional datasets (though I'm not sure how clear this would be). Examples below:

enter image description here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top