【问题标题】:Imshow in polar coordinates在极坐标中显示
【发布时间】:2022-02-17 08:36:10
【问题描述】:

我在 .dat 文件中有带有快照的磁盘模拟数据。我只想在极坐标中绘制一个。

我有:

rho = np.fromfile(filename).reshape(128,384)
plt.imshow(np.log10(rho),origin='lower',cmap="Oranges",aspect='auto')
plt.colorbar()
plt.show()

我想要这样的东西:

忽略颜色和 cmap。它们不是相同的模拟。只查找磁盘形式。

【问题讨论】:

  • rho 是什么?像素值?
  • 您要将矩形映射到磁盘上吗?
  • 我们需要至少有一个输入/输出匹配示例

标签: python python-3.x imshow polar-coordinates


【解决方案1】:

初步

为了帮助确定这个答案,我将首先为数据制作一个生成器。

import numpy as np
from matplotlib import pyplot as plt

def make_r_theta_vals():
    thetas_radians = np.arange(0,2.01*np.pi,np.pi/100.)
    radii = np.arange(0,101,1)
    #meshgrid these to make a fuller array
    tr,rr = np.meshgrid(thetas_radians,radii)
    #generate fake z values
    z_vals = (75. * rr**(1./2.2)\
        + 50.*np.random.normal()*np.sin(tr) \
        + 20. * np.cos(tr) * np.sqrt(rr) \
        + np.cos(rr * np.pi / 100.) * np.sin(rr * np.pi/50.) * 6.)\
        * (np.sin(rr * np.pi/ 100.)**3. + 0.85)
    return thetas_radians, radii, z_vals

这里,z_vals 是一个NxM 数组,其中NM 是r 和theta 值的长度。在您的问题中,这对应于rho,但我想概括一下这个答案。

我们可以看到,这会产生与您的原始情节相似的情节,

def make_cartesian_plot():
    plt.clf()
    fig = plt.figure(figsize=[5,2])
    ax = fig.add_axes([0.15,0.18,0.8,0.8])
    thetas_radians, radii, z = make_r_theta_vals()
    ax.imshow(z,origin='lower',extent=[np.degrees(thetas_radians[0]),
                                        np.degrees(thetas_radians[-1]),
                                                   radii[0],radii[-1]])
    plt.savefig('cartesian.png')

输出为

简单的方法

为了在极坐标中进行这项工作,我们将使用pcolormesh,以及已知的 r 和 theta 值。如果您没有这些,您将需要生成它们,类似于我在第一个代码 sn-p 中生成它们的方式。然后,就很简单了:

def make_polar_plot():
    plt.clf()
    fig = plt.figure(figsize=[5,5])
    ax = fig.add_axes([0.1,0.1,0.8,0.8],polar=True)
    thetas_radians, radii, z = make_r_theta_vals()
    ax.pcolormesh(thetas_radians,radii,z,edgecolors='face')
    #ec='face' to avoid annoying gridding in pdf
    plt.savefig('polar.png')

产生:

为简单起见,省略了装饰选择(例如删除刻度标签)。

笛卡尔网格上的极坐标

相反,所问的问题描绘了一个绘制在矩形网格上的极盘。假设这是所需的输出,我们将rthetaz 转换为xyz。在这里,我们再次使用meshgrid 来制作有用的xypcolormesh 来处理绘图。

def make_cartepolar_plot():
    plt.clf()
    fig = plt.figure(figsize=[5,5])
    ax = fig.add_axes([0.15,0.15,0.8,0.8])
    thetas_radians, radii, z = make_r_theta_vals()
    tr,rr = np.meshgrid(thetas_radians,radii)
    x_vals = rr * np.cos(tr)
    y_vals = rr * np.sin(tr)
    ax.pcolormesh(x_vals,y_vals,z,edgecolors='face')
    #ec='face' to avoid annoying gridding in pdf
    plt.savefig('carte_polar.png')
    

这里的输出是

请注意,对于更复杂的数据集,您可能需要see this previous question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多