Python 如何从matplotlib绘图中删除微秒?

Python 如何从matplotlib绘图中删除微秒?,python,matplotlib,axis-labels,Python,Matplotlib,Axis Labels,我已经阅读了pylab示例和许多轴格式问题,但仍然无法删除下图中x轴的微秒 尝试更改轴/记号属性及其输出之前的原始代码 #filenames to be read in file0 = 'results' #Get data from file strore in record array def readIn(fileName): temp = DataClass() with open('%s.csv' % fileName) as csvfile:

我已经阅读了pylab示例和许多轴格式问题,但仍然无法删除下图中x轴的微秒

尝试更改轴/记号属性及其输出之前的原始代码

#filenames to be read in
file0 = 'results'         


#Get data from file strore in record array
def readIn(fileName):
    temp = DataClass()
    with open('%s.csv' % fileName) as csvfile:
        temp = mlab.csv2rec(csvfile,names = ['date', 'band','lat'])
    return temp

#plotting function(position number, x-axis data, y-axis data,
#                       filename,data type, units, y axis scale)
def iPlot(num,xaxi,yaxi,filename,types, units,scale):
    plt.subplot(2,1,num)
    plt.plot_date(xaxi,yaxi,'-')
    plt.title(filename + "--%s" % types )
    plt.ylabel(" %s  %s " % (types,units))
    plt.ylim(0,scale)
    plt.xticks(rotation=20)



# Set plot Parameters and call plot funciton
def plot():
    nameB = "Bandwidth"
    nameL = "Latency"
    unitsB = " (Mbps)"
    unitsL = "(ms)"
    scaleB = 30
    scaleL = 500

    iPlot(1,out0['date'],out0['lat'],file0,nameL,unitsL,scaleL)
    iPlot(2,out0['date'],out0['band'],file0,nameB,unitsB,scaleB)

def main():
    global out0 
    print "Creating plots..."

    out0 = readIn(file0)
    plot()

    plt.show()

main()

我试图通过添加以下内容来修改上述代码:

months   = date.MonthLocator()  # every month
days     = date.DayLocator()
hours    = date.HourLocator()
minutes    = date.MinuteLocator()
seconds   = date.SecondLocator()


def iPlot(num,xaxi,yaxi,filename,types, units,scale):
    plt.subplot(2,1,num)
    plt.plot_date(xaxi,yaxi,'-')
    plt.title(filename + "--%s" % types )
    plt.ylabel(" %s  %s " % (types,units))
    plt.ylim(0,scale)

    # Set Locators
    ax.xaxis.set_major_locator(days)
    ax.xaxis.set_minor_locator(hours)

    majorFormatter = date.DateFormatter('%M-%D %H:%M:%S')
    ax.xaxis.set_major_formatter(majorFormatter)
    ax.autoscale_view()

我正在设置的主格式化程序是否被默认重写?有没有一种方法可以关闭微秒而不破坏格式的其余部分?我很不清楚微秒是从哪里来的,因为我的数据中没有微秒

我对你的代码有几个问题。首先,它不起作用(我的意思是,即使我制作了所有模拟样本数据,它也不起作用)。第二,这并不是一个展示错误的最简单的工作示例,我想我不知道你的
日期是什么,我想
matplotlib.dates
?第三,我看不到您的绘图(您的完整标签上也有
'%M-%D
部分)

现在我遇到的问题是,我不知道如何使用
(“%M-%D%H:%M:%S”)
越过这一行,它向我抛出了错误的语法。(Python 2.6.6和3.4上的Matplotlib 1.3.1 Win7)。我看不出你的
ax
是什么,或者你的数据是什么样子的,当涉及到这样的东西时,所有这些都可能是有问题的。即使时间跨度过大,也会导致滴答声“溢出”(特别是当您尝试将小时定位器设置为年份范围时,即我认为这会在7200滴答声时抛出错误?)

同时,这里是我的min工作示例,它没有显示与您相同的行为

import matplotlib as mpl
import matplotlib.pyplot as plt
import datetime as dt

days     = mpl.dates.DayLocator()
hours    = mpl.dates.HourLocator()


x = []
for i in range(1, 30):
    x.append(dt.datetime(year=2000, month=1, day=i,
                             hour=int(i/3), minute=i, second=i))
y = []
for i in range(len(x)):
    y.append(i)

fig, ax = plt.subplots()
plt.xticks(rotation=45)
ax.plot_date(x, y, "-")

ax.xaxis.set_major_locator(days)
ax.xaxis.set_minor_locator(hours)

majorFormatter = mpl.dates.DateFormatter('%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(majorFormatter)
ax.autoscale_view()

plt.show()


(这一切不应该是一个答案,也许它会帮助你,但它太长了,不能作为一个评论)

如果您没有使用子地块,请不要使用它们

只需删除对
subplot()
subplot()
函数的任何提及,就可以使用轴句柄:
ax=plt.gca()
在对
ax
的任何引用之上

可能是这样的:

...
# Set Locators
ax = plt.gca()
ax.xaxis.set_major_locator(days)
ax.xaxis.set_minor_locator(hours)
...

然后,您将得到一个
ValueError:Invalid format string
错误——可能是因为
%D
不是一个字符串。(您可能需要
%m-%d%H:%m:%S
)如果您解决了这个问题,您的绘图将显示在格式化程序中。

在哪里设置
ax
?从
子批次(…)
?你能显示那条线吗?图,ax=plt.subpllots()发生在代码的最顶端你的秒数是浮动的吗?能否将它们四舍五入为整数进行绘图?是否不向
子绘图传递任何参数?它仅仅是
fig,ax=subplot()
?jedwards——是的,cphlewis——它们是datetime你说的一大堆话都是正确的。在阅读了一些答案/评论后,我对阴谋的理解存在根本性的缺陷。。。我将发布完整的原始代码和打印输出,以澄清我的代码是如何工作的。@Tom尝试尽可能将您的问题隔离到一个单独的较小脚本中,而不是发布完整的代码。如果它很长,帮助就更少了。除此之外,祝你好运!我想我只是错过了正确的方式来设置斧头与我做的方式subplots@Tom我没有看到您在上面的任何地方创建
fig
ax
实例。注意我是如何做的,ax=plt.subplot()
所以你也应该做
f,axarr=plt.subplot(2,1,m)
或者甚至可能是
fig,ax=plt.subplot(2)
。在您添加的代码中,您引用了不存在的
ax
实例,这将导致错误。在原始示例中,您使用的是
plt
命令,该命令隐式创建并选择
fig
ax
。代码,其中显示
#两个子批次,轴阵列为1-d