matplotlib vlines图中未应用y轴的最小值 [英] minimum value of y axis is not being applied in matplotlib vlines plot

查看:62
本文介绍了matplotlib vlines图中未应用y轴的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在matplotlib中进行vlines绘图,并且我在数据集中的所有y值都为> = 0 .我希望我的y轴最底下的刻度线显示为 0 ,但我却得到了-500.

I am doing a vlines plot in matplotlib and I have all my y values in the dataset as >=0. I want my y axis bottom most tick to read 0, but instead, I get -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')

这是我得到的情节:

谁能指出我做错了什么?另外,如果您能将我指向包含我需要的详细信息的文档,那就太好了.谢谢.

Can anyone point to me what I am doing wrong? Also, if you can point me to the docs that have the details I need, that would be great. Thanks.

问题是对 推荐答案

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

You can set the limit manually after plotting the data, like so:

pyplot.ylim(ymin=0)

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

What happens is that Matplotlib adjusts the plot limits so that it looks "best". Sometimes, this implies going beyond the strict range of your data. You must then update the limits after the plot, since each plot updates the limit (if I understand correctly).

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

Now, your approach can work, but you must update the figure after setting the limits:

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

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

(This works in the IPython shell on Mac OS X with the latest version of matplotlib.)

这篇关于matplotlib vlines图中未应用y轴的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆