2016-05-11 11 views
1

私はPlotting labeled intervals in matplotlib/gnuplotの質問への参照をしています。そこに公開されている解決策の問題は、ファイル内の1行のデータだけでは機能しません。matplotlibの間隔を

#!/usr/bin/env python 
# 

import matplotlib.pyplot as plt 
from matplotlib.dates import DateFormatter, MinuteLocator, SecondLocator 
import numpy as np 
from StringIO import StringIO 
import datetime as dt 

a=StringIO("""MMEX 2016-01-29T12:38:22 2016-01-29T12:39:03 SUCCESS 
""") 

#Converts str into a datetime object. 
conv = lambda s: dt.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S') 

#Use numpy to read the data in. 
data = np.genfromtxt(a, converters={1: conv, 2: conv}, 
       names=['caption', 'start', 'stop', 'state'], dtype=None) 
cap, start, stop = data['caption'], data['start'], data['stop'] 

#Check the status, because we paint all lines with the same color 
#together 
is_ok = (data['state'] == 'SUCCESS') 
not_ok = np.logical_not(is_ok) 

#Get unique captions and there indices and the inverse mapping  
captions, unique_idx, caption_inv = np.unique(cap, 1, 1) 

#Build y values from the number of unique captions. 
y = (caption_inv + 1)/float(len(captions) + 1) 

#Plot function 
def timelines(y, xstart, xstop, color='b'): 
    """Plot timelines at y from xstart to xstop with given color.""" 
    plt.hlines(y, xstart, xstop, color, lw=4) 
    plt.vlines(xstart, y+0.005, y-0.005, color, lw=2) 
    plt.vlines(xstop, y+0.005, y-0.005, color, lw=2) 

#Plot ok tl black  
timelines(y[is_ok], start[is_ok], stop[is_ok], 'k') 
#Plot fail tl red 
timelines(y[not_ok], start[not_ok], stop[not_ok], 'r') 

#Setup the plot 
ax = plt.gca() 
ax.xaxis_date() 
myFmt = DateFormatter('%Y-%m-%dT%H:%M:%S') 
ax.xaxis.set_major_formatter(myFmt) 
ax.xaxis.set_major_locator(SecondLocator(interval=3600)) # used to be   SecondLocator(0, interval=20) 

#To adjust the xlimits a timedelta is needed. 
delta = (stop.max() - start.min())/10 

plt.yticks(y[unique_idx], captions) 
plt.ylim(0,1) 
plt.xlim(start.min()-delta, stop.max()+delta) 
plt.xlabel('Time') 
plt.xticks(rotation=70) 
plt.show(block=True) 

私はこのコードをしようとすると、私は次のエラーを取得する:これは私がしようとしているコードです

また
Traceback (most recent call last): 
    File "./testPlot.py", line 49, in <module> 
    timelines(y[is_ok], start[is_ok], stop[is_ok], 'k') 
ValueError: boolean index array should have 1 dimension 

、私はデータ上にダミーの行を追加しようとすると、してみましょう"MMEX 2016-01-01T00:00:00 2016-01-01T00:00:00 SUCCESS"と表示されていますが、プロットは機能しますが、見栄えが良くありません。

提案がありますか?私は解決策を見つけたときに、同じポストにこの質問を入れてみましたが、私は...事前に

おかげで十分な評判を持っていない

+0

あなたは '形状(Y)'、 '形状(スタート)'、 '形状(停止)'と '形状(is_ok)の出力を与えることができます'?私はあなたのデータ配列が同じ次元を持っていないと思います。 – wsj

答えて

0

問題は、あなただけnp.genfromtxtに1つのアイテムを読んだときということですスカラ(0次元)を生成しています。私たちはそれらを少なくとも1Dにする必要があります。

timelines関数を定義する場所のすぐ上にこれらの行を追加すると、すべて正常に動作します。

これはnumpyファンクションnp.atleast_1d()を使用してスカラーを1D numpyアレイに変換します。

#Check the dimensions are at least 1D (for 1-item data input) 
if start.ndim < 1: 
    start = np.atleast_1d(start) 
if stop.ndim < 1:: 
    stop = np.atleast_1d(stop)  
if is_ok.ndim < 1: 
    is_ok = np.atleast_1d(is_ok)  
if not_ok.ndim < 1: 
    not_ok = np.atleast_1d(is_ok) 

出力:

enter image description here

+0

ありがとう非常に!できます!さて、私は元の投稿に(このページにリンクされている)ソリューションを投稿しています。だから、誰かが同じエラーを持っていれば、その答えを見つけることができます。 –

+0

さて、管理者は元の投稿で自分の回答を削除し続けます。誰かが同じ問題を抱えていたらどういうわけか解決策を得る –

+0

これで問題が解決した場合は、回答を受け入れることを検討するかもしれません。 – tom