带有matplotlib的Python极地时钟样图 [英] Python polar clock-like plot with matplotlib

查看:68
本文介绍了带有matplotlib的Python极地时钟样图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用

考虑到 equals 的定义,我原以为点的 x 值与小时一致.当前定义为一个角度,但我也尝试将其定义为一个小时.为什么不是这种情况,如何使我的数据与相应的时间对齐?

解决方案

Matplotlib 希望角度以弧度而不是度为单位(请参阅

或者,您可以更改 equals 的定义以产生弧度方面的角度:equals = np.linspace(0, 2*np.pi, 24, endpoint=False)>

I am trying to plot data in a clock-wise fashion using matplotlib in Python in the style of this answer. I noticed weird behaviour when plotting my data; the data points had the correct y value, but would not appear at the correct x values, i.e. times. I first thought that my data was erroneous, but upon recreating my problem with the following working example I came to the conclusion that the mistake must be somewhere else.

import numpy as np
import matplotlib.pyplot as plt     

ax = plt.subplot(111, polar=True)
equals = np.linspace(0, 360, 24, endpoint=False) #np.arange(24)
ones = np.ones(24)
ax.scatter(equals, ones)       

# Set the circumference labels
ax.set_xticks(np.linspace(0, 2*np.pi, 24, endpoint=False))
ax.set_xticklabels(range(24))      

# Make the labels go clockwise
ax.set_theta_direction(-1)       

# Place 0 at the top
ax.set_theta_offset(np.pi/2.0)       

plt.show()

This results in the following plot:

I would have expected that the x values of the points line up with the hours, considering the definition of equals. It is currently defined as an angle, but I also tried defining it as an hour. Why is this not the case and how can I get my data to line up with the corresponding time?

解决方案

Matplotlib expects angles to be in units of radians and not degrees (see the open bug report). You can use the numpy function np.deg2rad to convert to radians:

import numpy as np
import matplotlib.pyplot as plt     

ax = plt.subplot(111, polar=True)
equals = np.linspace(0, 360, 24, endpoint=False) #np.arange(24)
ones = np.ones(24)
ax.scatter(np.deg2rad(equals), ones)       

# Set the circumference labels
ax.set_xticks(np.linspace(0, 2*np.pi, 24, endpoint=False))
ax.set_xticklabels(range(24))      

# Make the labels go clockwise
ax.set_theta_direction(-1)       

# Place 0 at the top
ax.set_theta_offset(np.pi/2.0)       

plt.show()

This produces the following picture:

Alternatively, you could have changed your definition of equals to produce angles in terms of radians: equals = np.linspace(0, 2*np.pi, 24, endpoint=False)

这篇关于带有matplotlib的Python极地时钟样图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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