我有一个2D矩阵,其中包含一个函数的幅度。该矩阵包含极点处的这些振幅,而不是通常的笛卡尔几何。例如,第一行包含固定半径但角度增加的振幅。列具有固定角度的径向值。
我正在使用以下python代码绘制3D图形。
import numpy as np
import matplotlib.pyplot as plt
from os.path import exists
from os import sys
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
N=int(sys.argv[1]);
filling=int(sys.argv[2]);
trial=int(sys.argv[3]);
filename = 'density_N%d_filling%d_trial%d' % (N,filling,trial);
filename2 = 'init_%d_%d_%d' % (N,filling,trial);
if exists(filename2):
a=np.loadtxt(filename2)
NUMBINS=int(np.round(a[0]));
dR2=a[1]
dtheta=a[2]
rmax = a[3];
def read_file(filename):
with open(filename,'r') as data:
x = []
y = []
for line in data:
p = line.split()
for i in range(NUMBINS):
x.append(float(p[i]))
y.append(np.array(x))
x=[]
return y
density = np.array(read_file(filename))
density = density*NUMBINS/(np.pi*dR2)
localfillingfraction=2*np.pi*density;
r = np.sqrt(np.arange(0,rmax*rmax,dR2) )[:NUMBINS]
theta = np.array([dtheta for point in r])
mpl.rcParams['legend.fontsize'] = 10
sdensity = np.transpose(localfillingfraction)
fig = plt.figure()
ax = fig.gca(projection='3d')
for n, armonic in enumerate(sdensity):
ax.plot(r,theta*n,armonic)
plt.savefig("N%d_FILLING%d_trial%d.pdf" % (N,filling,trial));
plt.title('filling fraction, v = 1/' + str(filling))
plt.xlabel('Radius (units of lB)')
plt.ylabel('angle(Rad)')
plt.show()
因此 filename2 包含四个我需要进行计算的参数。 filename 是带有极性数据的2D矩阵。我编写的 read_file()函数是将2D矩阵读入列表的功能。尽管矩阵包含径向数据作为列,但代码却以相反的方式对其进行了绘制。这就是为什么我必须转置矩阵。最后,我得到了下面的情节:Filling fraction as function of R, theta
尽管此图可以完成任务,但半径和角度仍像笛卡尔坐标一样绘制。我可以让它看起来像极性吗?
答案 0 :(得分:0)
因此,我尝试了此代码的变体,它使用了热图类型的极坐标图,该极坐标图非常简单明了,可以显示角度+径向分布。
#! /usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
from os.path import exists
from os import sys
import matplotlib as mpl
from matplotlib import rc
from mpl_toolkits.mplot3d import Axes3D
#=========================================
N=int(sys.argv[1]);
filling=int(sys.argv[2]);
trial=int(sys.argv[3]);
#=====================================================================
filename = 'density_N%d_filling%d_trial%d' % (N,filling,trial);
filename2 = 'init_%d_%d_%d' % (N,filling,trial);
if exists(filename2):
a=np.loadtxt(filename2)
NUMBINS=int(np.round(a[0]));
print(NUMBINS)
dR2=a[1]
dtheta=a[2]
rmax = a[3];
#===========================================================================
'''This part reads the 2D array of NUMBIN*NUMBIN, row by row.
Thus, the returned list y consists of a linear array of dimension
[NUMBIN,0] with each element is itself a list containing each row.'''
def read_file(filename):
with open(filename,'r') as data:
x = []
y = []
for line in data:
p = line.split()
for i in range(NUMBINS):
x.append(float(p[i]))
y.append(np.array(x))
x=[]
return y
density = np.array(read_file(filename))
#==========================================================================
density = density*NUMBINS/(np.pi*dR2)
localfillingfraction=2*np.pi*density;
r = np.sqrt(np.arange(0,rmax*rmax,dR2) )[:NUMBINS]
theta = np.linspace(0,2*np.pi,NUMBINS)
mpl.rcParams['legend.fontsize'] = 10
#===============================================================
'''the matrix needed to be trasposed, to get the radial and angular
data to be read correctly'''
sdensity = np.transpose(localfillingfraction)
fig = plt.figure()
ax = Axes3D(fig)
rad, th = np.meshgrid(r,theta)
plt.subplot(projection="polar")
plt.pcolormesh(th,rad,sdensity)
plt.thetagrids([theta * 15 for theta in range(360//15)])
plt.plot(theta,rad,color='k',ls='none')
plt.grid()
plt.savefig("N%d_FILLING%d_trial%d.jpg" % (N,filling,trial));
plt.show()
情节如下: