使用matplotlib在极坐标图中阴影``细胞'' [英] Shade 'cells' in polar plot with matplotlib

查看:171
本文介绍了使用matplotlib在极坐标图中阴影``细胞''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆规则分布的点(θ= n *π/6,r = 1 ... 8),每个点的值为[0,1].我可以使用matplotlib使用它们的值来绘制它们

I've got a bunch of regularly distributed points (θ = n*π/6, r=1...8), each having a value in [0, 1]. I can plot them with their values in matplotlib using

polar(thetas, rs, c=values)

但是我只想用一个很小的小点来遮蔽相应的单元格"(即,直到相邻点中间的所有内容),其颜色对应于该点的值:

But rather then having just a meagre little dot I'd like to shade the corresponding 'cell' (ie. everything until halfway to the adjacent points) with the colour corresponding to the point's value:

(请注意,这里的值只是[0,.5,1],实际上它们的值都是0到1之间的值.是否有通过matplotlib实现此值(或足够接近的值)的简单方法?也许更容易将其视为2D直方图?

(Note that here my values are just [0, .5, 1], in really they will be everything between 0 and 1. Is there any straight-forward way of realising this (or something close enough) with matplotlib? Maybe it's easier to think about it as a 2D-histogram?

推荐答案

当然!只需在极轴上使用pcolormesh.

Sure! Just use pcolormesh on a polar axes.

例如

import matplotlib.pyplot as plt
import numpy as np

# Generate some data...
# Note that all of these are _2D_ arrays, so that we can use meshgrid
# You'll need to "grid" your data to use pcolormesh if it's un-ordered points
theta, r = np.mgrid[0:2*np.pi:20j, 0:1:10j]
z = np.random.random(theta.size).reshape(theta.shape)


fig, (ax1, ax2) = plt.subplots(ncols=2, subplot_kw=dict(projection='polar'))


ax1.scatter(theta.flatten(), r.flatten(), c=z.flatten())
ax1.set_title('Scattered Points')

ax2.pcolormesh(theta, r, z)
ax2.set_title('Cells')

for ax in [ax1, ax2]:
    ax.set_ylim([0, 1])
    ax.set_yticklabels([])

plt.show()

如果数据尚未位于常规网格中,则需要对其进行网格化以使用pcolormesh.

If your data isn't already on a regular grid, then you'll need to grid it to use pcolormesh.

尽管如此,它看起来像是位于情节的常规网格上.在这种情况下,网格化非常简单.如果已经订购,则可以像调用reshape一样简单.否则,简单循环或使用z值作为权重利用numpy.histogram2d即可满足您的需求.

It looks like it's on a regular grid from your plot, though. In that case, gridding it is quite simple. If it's already ordered, it may be as simple as calling reshape. Otherwise, a simple loop or exploiting numpy.histogram2d with your z values as weights will do what you need.

这篇关于使用matplotlib在极坐标图中阴影``细胞''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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