首页 文章

在matplotlib vlines图中未应用y轴的最小值

提问于
浏览
11

我在matplotlib中做了一个vlines图,我在数据集中的所有y值都是 >=0 . 我希望我的y轴最底部的勾选读取 0 ,但相反,我得到-500 .

这是代码:

#!/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的后续行动

1 回答

  • 21

    您可以在绘制数据后手动设置限制,如下所示:

    pyplot.ylim(ymin=0)
    

    会发生什么是Matplotlib调整绘图限制,使其看起来像"best" . 有时,这意味着超出了严格的数据范围 . 然后必须在绘图后更新限制,因为每个绘图都会更新限制(如果我理解正确的话) .

    现在,您的方法可以工作,但您必须在设置限制后更新数字:

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

    (这适用于Mac OS X上的IPython shell,其中包含最新版本的matplotlib . )

相关问题