2

In a discrete time-series graph, I have tried replacing ax.plot(x,y) by ax.vlines(x,y):

  • I get the error: vlines() missing 1 required positional argument: 'ymax'

However, I cannot know the ymax value beforehand. How can I avoid this error ?

Should I compute this value by parsing all the data to display ? Is there a way to tell matplotlib to automatically adapt to the data ?


Some more details about the graph:

The graph is not accurate, due to the drawing of a continuous curve, whereas my data is instead a distribution of discrete values over time. This is why I want to use vlines.

This is the code I create the graph with:

(The exception_time_series object is an object that counts the number of a given exception type at a given time in a program).

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()

And that's the graph I'm getting right now:

Rate of SEH-Exception by second

But I would like to get something of that kind, (that would reflect that it is a irregular distribution over time):

Type of graph that I would like

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169

2 Answers2

2

looks to me like you want to have a bar plot.

ymax is the upper limit for vlines, vlines(0, 0, 1) plots a vertical line at x=0 from y=0 to y=1.

This is a working minimal example:

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()

this is the result: enter image description here

MaxNoe
  • 14,470
  • 3
  • 41
  • 46
1

The ymax here is not actually the yrange - it's the top value of the vertical lines you want. To make a vline plot similar to your current plot, I believe you'd want to set the ymin to an array of zeros and ymax to your y values. If you have negative values in y, you should make ymin and ymax the min/max of 0 and your y array.

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

ax.vlines(x, ymin, ymax)
Ajean
  • 5,528
  • 14
  • 46
  • 69