Python中的圆形/极坐标直方图 [英] Circular / polar histogram in python

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

问题描述

我有周期数据,围绕它的分布最好以圆形显示.现在的问题是,如何使用matplotlib进行可视化?如果没有,可以在Python中轻松完成吗?

在这里,我生成了一些样本数据,希望通过圆形直方图进行可视化:

import matplotlib.pyplot as plt
import numpy as np

# Generating random data
a = np.random.uniform(low=0, high=2*np.pi, size=50)

在SX上的一个问题中,有几个例子是 Mathematica .

我想生成一个类似于以下内容之一的图:

解决方案

基于

a>画廊中的示例,您可以

import numpy as np
import matplotlib.pyplot as plt

N = 80
bottom = 8
max_height = 4

theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
radii = max_height*np.random.rand(N)
width = (2*np.pi) / N

ax = plt.subplot(111, polar=True)
bars = ax.bar(theta, radii, width=width, bottom=bottom)

# Use custom colors and opacity
for r, bar in zip(radii, bars):
    bar.set_facecolor(plt.cm.jet(r / 10.))
    bar.set_alpha(0.8)

plt.show()

当然,有很多变化和tweek,但这应该可以帮助您入门.

通常,浏览 matplotlib图库通常是一个不错的起点.

>

在这里,我使用bottom关键字将中心保留为空,因为我认为您看到的是一个较早的问题,您使用的图形更像是我所拥有的图形,因此我认为这就是您想要的.要获得上面显示的完整楔形,只需使用bottom=0(或将其省略,因为0是默认值).

I have periodic data and the distribution for it is best visualised around a circle. Now the question is how can I do this visualisation using matplotlib? If not, can it be done easily in Python?

Here I generate some sample data which I would like to visualise with a circular histogram:

import matplotlib.pyplot as plt
import numpy as np

# Generating random data
a = np.random.uniform(low=0, high=2*np.pi, size=50)

There are a few examples in a question on SX for Mathematica.

I would like to generate a plot which looks something like one of the following:

解决方案

Building off of this example from the gallery, you can do

import numpy as np
import matplotlib.pyplot as plt

N = 80
bottom = 8
max_height = 4

theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
radii = max_height*np.random.rand(N)
width = (2*np.pi) / N

ax = plt.subplot(111, polar=True)
bars = ax.bar(theta, radii, width=width, bottom=bottom)

# Use custom colors and opacity
for r, bar in zip(radii, bars):
    bar.set_facecolor(plt.cm.jet(r / 10.))
    bar.set_alpha(0.8)

plt.show()

Of course, there are many variations and tweeks, but this should get you started.

In general, a browse through the matplotlib gallery is usually a good place to start.

Here, I used the bottom keyword to leave the center empty, because I think I saw an earlier question by you with a graph more like what I have, so I assume that's what you want. To get the full wedges that you show above, just use bottom=0 (or leave it out since 0 is the default).

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

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