【问题标题】:RGB polar plot in PythonPython中的RGB极坐标图
【发布时间】:2018-09-12 06:47:28
【问题描述】:

我正在尝试在 Python 中生成 RGB 极坐标图,我期待 matplotlib.pyplot.imshow 能够做到这一点。但是,每当我尝试使用这种方法绘制数据时,我都会得到一个空白输出。

import matplotlib.pyplot as plt
import numpy as np

data = np.array([[[0,0,1],[0,1,0],[1,0,0]],[[0,0,0.5],[0,0.5,0],[0.5,0,0]]])
# Sample, any N,M,3 data should work
ax = plt.subplot(111,polar=True)
ax.imshow(data,extent=[0,2*np.pi,0,1]) # Produces a white circle

有没有使用上述方法或其他方法的好方法?

谢谢。


编辑:我设法通过使用extent=[0,np.pi/2,0,1] 制作了一个象限,但它的使用对于极坐标图显然存在问题。因为除了完整的象限之外的任何东西都不会产生预期的结果。

【问题讨论】:

  • 如果您还没有看过,这里有一些演示彩色极坐标图,可以根据您的需要进行调整:matplotlib.org/api/_as_gen/matplotlib.pyplot.show.html
  • Anil_M 是的,我看过,但这些都不是 RGB 极坐标图,只是带有颜色条的极坐标图。我想绘制一个 [N,M,3] 数组。

标签: python matplotlib rgb imshow polar-coordinates


【解决方案1】:

不幸的是,在极坐标图上使用imshow 是不可能的,因为 imshow 网格的像素必然是二次方的。但是,您可以使用pcolormesh 并应用一个技巧(类似于this one),即将颜色作为color 参数提供给pcolormesh,因为它通常只需要2D 输入。

import matplotlib.pyplot as plt
import numpy as np

data = np.array([[[0,0,1],[0,1,0],[1,0,0]],
                 [[0,0,0.5],[0,0.5,0],[0.5,0,0]]])

ax = plt.subplot(111, polar=True)

#get coordinates:
phi = np.linspace(0,2*np.pi,data.shape[1]+1)
r = np.linspace(0,1,data.shape[0]+1)
Phi,R = np.meshgrid(phi, r)
# get color
color = data.reshape((data.shape[0]*data.shape[1],data.shape[2]))

# plot colormesh with Phi, R as coordinates, 
# and some 2D array of the same shape as the image, except the last dimension
# provide colors as `color` argument
m = plt.pcolormesh(Phi,R,data[:,:,0], color=color, linewidth=0)
# This is necessary to let the `color` argument determine the color
m.set_array(None)


plt.show()

结果不是一个圆圈,因为您没有足够的积分。重复数据,data = np.repeat(data, 25, axis=1) 将允许得到一个圆圈。

【讨论】:

  • 谢谢!这正是我想要的。
  • 谢谢! set_array(None) 正是我需要的。由于某种原因,默认情况下“颜色”kwarg 只会覆盖边缘颜色。奇怪的是我们需要使用一种虚拟的 2D 颜色变量然后覆盖它。
猜你喜欢
  • 2022-01-14
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多