简体   繁体   English

在极坐标中显示

[英]Imshow in polar coordinates

I have disk simulation data with snapshots in a.dat file.我在 .dat 文件中有带有快照的磁盘模拟数据。 I want to plot only one but in polar coordinates.我想要 plot 只有一个但在极坐标中。

I have:我有:

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

在此处输入图像描述

I want something like that:我想要这样的东西:

在此处输入图像描述

Ignore colors and cmap.忽略 colors 和 cmap。 They are not same simulation.它们不是相同的模拟。 Only the disk form is sought.只寻找磁盘形式。

Preliminary初步的

To help establish this answer, I'm going to first make a generator for the data.为了帮助确定这个答案,我将首先为数据制作一个生成器。

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

Here, z_vals is an NxM array, where N and M are the lengths of the r and theta values.这里, z_vals是一个NxM数组,其中NM是 r 和 theta 值的长度。 In your question, this would correspond to rho , but I'd like to generalize this answer.在您的问题中,这对应于rho ,但我想概括一下这个答案。

We can see that this produces a plot similar to your original plot,我们可以看到这会产生一个 plot 类似于你原来的 plot,

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

with an output of output 的

显示变化的径向图案的图,以笛卡尔坐标绘制

The Simple Way简单的方法

To make this work in polar coordinates, we're going to use pcolormesh , along with the known r and theta values.为了使它在极坐标中工作,我们将使用pcolormesh以及已知的 r 和 theta 值。 If you don't have those, you will need to generate them similar to how I generated them in the first code snippet.如果您没有这些,您将需要生成它们,类似于我在第一个代码片段中生成它们的方式。 Then, it's fairly easy:然后,这很容易:

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

which produces:产生:

显示具有给定角度偏好的盘状结构的极坐标图

Decorative choices (such as removing the tick labels) are omitted for simplicity.为简单起见,省略了装饰性选择(例如删除刻度标签)。

Polar on a Cartesian Grid笛卡尔网格上的极坐标

Conversely, the question as asked depicted a polar disk plotted onto rectangular grid.相反,所问的问题描述了绘制在矩形网格上的极盘。 Assuming this is the desired output, we instead convert the r , theta , z to x , y , z .假设这是所需的 output,我们改为将rthetaz转换为xyz Here again, we used meshgrid to make a useful x and y and pcolormesh to handle the plotting.在这里,我们再次使用meshgrid来制作有用的xy ,并pcolormesh来处理绘图。

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

Here, the output is在这里,output 是

上面显示的极坐标图,现在投影到笛卡尔网格上。

Note that, for a more complicated data set, you may need to see this previous question .请注意,对于更复杂的数据集,您可能需要查看上一个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM