2011-04-05 17 views
11

我正在做matplotlib中的vlines图,并将数据集中的所有y值都设为>=0。我想我的y轴最底部刻度读取0,但相反,我得到-500。y轴的最小值未应用于matplotlib vlines plot

下面是代码:

#!/usr/bin/env python 

import numpy as np 
from matplotlib import pyplot as plt, dates as mdates 
import datetime as dt, time 

# Read the data and turn it into a numpy array 
#store = map(lambda line: map(int, line.strip().split()), open(name + '.txt').readlines()) 
store = [ 
    [1293606162197, 0, 0], 
    [1293605477994, 63, 0], 
    [1293605478057, 0, 0], 
    [1293605478072, 2735, 1249], 
    [1293606162213, 0, 0], 
    [1293606162229, 0, 0], 
] 

nstore = np.array(store) 

# Get arrays of each columns in the store array 
d = nstore[:,0] 
y1 = nstore[:,1] 
y2 = nstore[:,2] 

# Get arrays of values to be passed to matplotlib 
s = d/1000 
dts = map(dt.datetime.fromtimestamp, s) 
fds = mdates.date2num(dts) 

# new figure and subplot 
fig = plt.figure() 
ax = fig.add_subplot(111) 

# Plot using vlines 
ax.vlines(fds, [0], y1, 'red') 

# set xaxis tick settings 
ax.xaxis.set_major_locator(mdates.MinuteLocator()) 
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d %H:%M')) 

for label in ax.xaxis.get_ticklabels(): 
    label.set_rotation('vertical') 

fig.subplots_adjust(bottom=.25) 

# Set the y axis bottom limit to 0 
ax.set_ylim(bottom=0) # <<- THIS DOES NOT SEEM TO BE WORKING 

# Save the plot figure 
fig.savefig('out.png') 

和这里的情节,我得到: enter image description here

任何人都可以点我什么,我做错了什么?另外,如果你能指出我需要的详细信息,那就太好了。谢谢。

问题是Creating graph with date and time in axis labels with matplotlib

回答

19

跟进您可以绘制数据后手动设置的限制,就像这样:

pyplot.ylim(ymin=0) 

什么情况是,Matplotlib调整情节限制等等它看起来“最好”。有时候,这意味着超出了数据的严格范围。您必须在之后更新极限,因为每个绘图更新了极限(如果我理解正确)。现在

,你的方法可以工作,但你必须在设置限制后更新图:(此作品在Mac OS X上的IPython的外壳与matplotlib的最新版本)

ax.set_ylim(bottom=0) 
pylot.draw() 

+0

啊,谢谢EOL,让它工作。在面向对象的风格中是否存在这种方法的等价物,因为我将同时并行地绘制许多图。 – 2011-04-05 06:54:13

+0

@Shrikant:我在我的答案中添加了一个面向对象的方法。 – EOL 2011-04-05 06:58:05

+0

我得到一个'TypeError:draw_wrapper()需要至少2个参数(1个给定)'与'ax.draw()',在Ubuntu上运行。 – 2011-04-05 07:15:10