具有未知y范围的离散时间序列图

时间:2014-08-01 15:31:47

标签: python matplotlib time-series timeserieschart

在离散的时间序列图中,我尝试将ax.plot(x,y)替换为ax.vlines(x,y)

  • 我收到错误:vlines() missing 1 required positional argument: 'ymax'

但是,我事先无法知道ymax值。 如何避免此错误?

我应该通过解析要显示的所有数据来计算这个值吗?有没有办法告诉matplotlib自动适应数据?


有关该图表的更多详细信息:

由于绘制了连续曲线,图形不准确,而我的数据则是随时间推移的离散值分布。这就是我想使用vlines

的原因

这是我用以下代码创建图表的代码:

(exception_time_series对象是一个对象,它计算程序中给定时间内给定异常类型的数量)。

fig = figure(1) 

for exception_time_series in exceptions_time_series.list_exception_time_series:

    time_values, series_values = exception_time_series.time_values, exception_time_series.series_values

    ax = fig.add_subplot(111, autoscale_on=True )

    dates = np.array(time_values)
    x = dates 
    y = np.array(series_values)

    ax.plot(x, y, label=exception_time_series.exception) # <=== using plot 
    ax.legend()

show()

这就是我现在得到的图表:

Rate of SEH-Exception by second

但是我想得到一些类似的东西(这反映出随着时间的推移它是不规则的分布):

Type of graph that I would like

2 个答案:

答案 0 :(得分:2)

在我看来,你希望有一个bar情节。

ymaxvlines的上限,vlines(0, 0, 1)绘制了从y = 0到y = 1的x = 0处的垂直线。

这是一个有用的最小例子:

import matplotlib.pyplot as plt
import numpy as np
from numpy.random import normal

x = np.linspace(0, 10, 100)
y = normal(0, 1, 100)
bar_width = (max(x) - min(x))/len(x)

plt.bar(x, y, bar_width, facecolor='black')
plt.show()

这是结果: enter image description here

答案 1 :(得分:1)

ymax这里实际上不是yrange - 它是你想要的垂直线的最高值。要制作类似于当前绘图的vline图,我相信你想要将ymin设置为零和ymax到y值的数组。如果y中有负值,则应将ymin和ymax设为最小值/最大值0和y数组。

yz = np.zeros(y.shape)
ymin = np.minimum(yz, y)
ymax = np.maximum(yz, y)

ax.vlines(x, ymin, ymax)