python绘制极坐标心形图_创建使用Matplotlib / Python的一堆极坐标图的

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!

解决方案

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()

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值