2016-04-28 37 views
2

matplotlibをPythonでthis answerのスタイルで使用して時計のようにデータをプロットしようとしています。私はデータをプロットするときに奇妙な動作に気付きました。データ点は正しいy値を有していたが、正しいx値、すなわち時刻に現れなかった。私は最初に自分のデータが間違っていると思っていましたが、私の問題を次の作業例で再現すると、間違いは別の場所になければならないという結論に達しました。matplotlibを使ったPython極クロック様のプロット

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() 

これは、次のグラフのような結果になります。 enter image description here

私はポイントのx値は、equalsの定義を考慮すると、時間と並ぶことを期待しているだろう。現在は角度として定義されていますが、1時間として定義しようとしました。なぜこれは当てはまりませんか、データを対応する時間に整列させるにはどうすればよいですか?

答えて

3

Matplotlibは、角度がラジアン単位で、度数ではないことを期待しています(open bug report参照)。あなたはラジアンに変換するにはnumpyの機能np.deg2radを使用することができます。

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() 

これは、次の画像生成:

また

enter image description here

を、あなたはの面で角度を生成するために等号のあなたの定義を変更している可能性がラジアン:equals = np.linspace(0, 2*np.pi, 24, endpoint=False)

+0

ありがとう、これは私の問題を解決しました!興味のある人には、24時間から輻射に時間を変換するには、単に時間を15倍して度を求め、それをラジアンに変換します(確かにもっと簡単な解決策があります)。 'lambda t:np.deg2rad(t * 15)' – Alarik

1

equals配列は度ですが、matplotlibはラジアンを想定しています。だからあなたがする必要があるのは、角度測定をラジアン単位で行うことだけです。

関連する問題