Home > Software engineering >  Horizontal line to infinity on one side only in matplotlib
Horizontal line to infinity on one side only in matplotlib

Time:08-24

I'd like to plot a line that goes to infinity, but starting from a finite point. For simplicity, let's say that the line can be horizontal. I would like to plot a line from (0, 0) to (inf, 0).

Using hlines:

>>> fig, ax = plt.subplots()
>>> ax.hlines(0, 0, np.inf)
.../python3.8/site-packages/matplotlib/axes/_base.py:2480: UserWarning: Warning: converting a masked element to nan.
  xys = np.asarray(xys)

The result is an empty plot.

axhline has a starting parameter, but it is in axis coordinates rather than data. Similar problem for axline. Is there a way to plot a (horizontal) line with one end in data coordinates and the other at infinity?

The motivation behind this is that I'd like to be able to plot some cumulative probabilities without setting data past the last bin to zero, as here: enter image description here

CodePudding user response:

Part of the issue is that normal plotting methods apply the same transform to the input data. What is required here is to apply a enter image description here

Turning on clipping is necessary, otherwise you could end up with artifacts that look like this:

enter image description here

  • Related