python polar contour_python - Polar contour plot in Matplotlib

在学习《NLP with Ptyhon》一中的过程中,总想用中文语料进行试验,结果在matplotlib.plot生成的统计图表中,中文总是无法正常显示。在网上也找了些资料,说是在

up vote 8 down vote favorite 4 I have a set of data that I want to use to produce a contour plot in polar co-ordinates using Matplotlib. My data is the following: theta - 1D array of angle values radius - 1D array of radius values value - 1D array of values that I want to use for the contours These are all 1D arrays that align properly - eg: theta

radius

value

30

1

2.9

30

2

5.3

35

5

9.2

That is, all of the values are repeated enough times so that each row of this 'table' of three variables defines one point. How can I create a polar contour plot from these values? I've thought about converting the radius and theta values to x and y values and doing it in cartesian co-ordinates, but the contour function seems to require 2D arrays, and I can't quite understand why. Any ideas? python matplotlib graphing

|

this question asked Jul 1 '11 at 13:26 robintw 10.4k 31 94 168  |  2 Answers

up vote 8 down vote ---Accepted---Accepted---Accepted---

Matplotlib's contour() function expects data to be arranged as a 2D grid of points and corresponding grid of values for each of those grid points. If your data is naturally arranged in a grid you can convert r, theta to x, y and use contour(r*np.cos(theta), r*np.sin(theta), values) to make your plot. If your data isn't naturally gridded, you should follow Stephen's advice and used griddata() to interpolate your data on to a grid. The following script shows examples of both. import pylab as plt

from matplotlib.mlab import griddata

import numpy as np

# data on a grid

r = np.linspace(0, 1, 100)

t = np.linspace(0, 2*np.pi, 100)

r, t = np.meshgrid(r, t)

z = (t-np.pi)**2 + 10*(r-0.5)**2

plt.subplot(121)

plt.contour(r*np.cos(t), r*np.sin(t), z)

# ungrid data, then re-grid it

r = r.flatten()

t = t.flatten()

x = r*np.cos(t)

y = r*np.sin(t)

z = z.flatten()

xgrid = np.linspace(x.min(), x.max(), 100)

ygrid = np.linspace(y.min(), y.max(), 100)

xgrid, ygrid = np.meshgrid(xgrid, ygrid)

zgrid = griddata(x,y,z, xgrid, ygrid)

plt.subplot(122)

plt.contour(xgrid, ygrid, zgrid)

plt.show()

|

this answer answered Jul 1 '11 at 17:30 matt 2,158 12 14      Thanks for the very clear answer. I've posted a follow-up question about axes - stackoverflow.com/questions/6556361/…. I wonder if you might be able to help with that? –  robintw Jul 2 '11 at 10:11  |  up vote 3 down vote I don't know if it's possible to do a polar contour plot directly, but if you convert to cartesian coordinates you can use the griddata function to convert your 1D arrays to 2D.

|

this answer answered Jul 1 '11 at 14:58 Stephen Terry 4,053 1 17 24  |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值