PYTHON中的RGB极坐标图 [英] RGB polar plot in Python

查看:34
本文介绍了PYTHON中的RGB极坐标图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用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]成功地创建了一个象限,但它的使用对于极地图显然是错误的。因为除非是完整的象限,否则不会产生预期的结果。

推荐答案

在极地图上使用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)就可以得到一个圆。

这篇关于PYTHON中的RGB极坐标图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆