Python matplotlib中的标记间隔

Python matplotlib中的标记间隔,python,matplotlib,Python,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

我提到的问题是,这里公开的解决方案的问题是,它不能只处理文件中的一行数据。这是我正在尝试的代码:

#!/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
回溯(最近一次呼叫最后一次):
文件“/testPlot.py”,第49行,在
时间线(y[正常]、开始[正常]、停止[正常]、'k')
ValueError:布尔索引数组应具有1维
另外,当我尝试在数据上添加一条虚线时,我们说“MMEX 2016-01-01T00:00:00 2016-01-01T00:00:00成功”,图是可行的,但看起来不太好

有什么建议吗?当我找到解决方案时,我试图把这个问题放在同一个帖子上,但我没有足够的声誉


提前感谢

问题是,当您仅阅读带有
np.genfromtxt
的1项时,它会产生标量(0维)。我们需要它们至少是1D

您可以在定义
时间线
函数的上方添加这些行,然后一切正常

这利用
numpy
函数,将标量转换为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)  
#检查尺寸是否至少为1D(对于1项数据输入)
如果start.ndim<1:
开始=np.至少1d(开始)
如果stop.ndim<1::
停止=np.至少1d(停止)
如果正常,ndim<1:
is_ok=np.至少1d(is_ok)
如果不正常,ndim<1:
不正常=np。至少1d(正常)
输出:


问题是,当您仅读取带有
np.genfromtxt
的1项时,它会产生标量(0维)。我们需要它们至少是1D

您可以在定义
时间线
函数的上方添加这些行,然后一切正常

这利用
numpy
函数,将标量转换为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)  
#检查尺寸是否至少为1D(对于1项数据输入)
如果start.ndim<1:
开始=np.至少1d(开始)
如果stop.ndim<1::
停止=np.至少1d(停止)
如果正常,ndim<1:
is_ok=np.至少1d(is_ok)
如果不正常,ndim<1:
不正常=np。至少1d(正常)
输出:


您能给出
形状(y)
形状(开始)
形状(停止)
形状(正常)
的输出吗?我想你的数据数组没有相同的维数。你能给出
形状(y)
形状(开始)
形状(停止)
形状(ok)
的输出吗?我想您的数据数组没有相同的维度。非常感谢!它起作用了!现在,我将在原始帖子上发布解决方案(链接到此页面),以便如果任何人有相同的错误,他/她都可以找到答案。好吧,管理员一直在删除我在原始帖子上的回复。希望如果有人有相同的问题,不知如何解决这个问题,如果你可以考虑接受答案,我点击了“检查”的形象,变成绿色…这意味着我接受它?我是新来的……非常感谢!它起作用了!现在,我将在原始帖子上发布解决方案(链接到此页面),以便如果任何人有相同的错误,他/她都可以找到答案。好吧,管理员一直在删除我在原始帖子上的回复。希望如果有人有相同的问题,不知如何解决这个问题,如果你可以考虑接受答案,我点击了“检查”的形象,变成绿色…这意味着我接受它?我是新来的。。。。