Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib: how to remove vertical line from cdf graph for datetime values

The following code draws the cdf for datetime values:

import matplotlib.pyplot as plt
import matplotlib.dates as dates
import numpy as np; np.random.seed(42)
import pandas as pd

objDate = dates.num2date(np.random.normal(735700, 300, 700))

ser = pd.Series(objDate)
ax = ser.hist(cumulative=True, density=1, bins=500, histtype='step')

plt.show()

cdf of datetime values

How can I remove the vertical line at the right-most end of graph? The approach mentioned here doesn't work as replacing line#9 with:

ax = ser.hist(cumulative=True, density=1, bins=sorted(objDate)+[np.inf], histtype='step')

gives

TypeError: can't compare datetime.datetime to float

like image 781
Saad Avatar asked Apr 28 '26 20:04

Saad


1 Answers

The CDF is actually drawn as a polygon, which in matplotlib is defined by a path. A path is in turn defined by vertices (where to go) and codes (how to get there). The docs say that we should not directly alter these attributes, but we can make a new polygon derived from the old one that suits our needs.

poly = ax.findobj(plt.Polygon)[0]
vertices = poly.get_path().vertices

# Keep everything above y == 0. You can define this mask however
# you need, if you want to be more careful in your selection.
keep = vertices[:, 1] > 0

# Construct new polygon from these "good" vertices
new_poly = plt.Polygon(vertices[keep], closed=False, fill=False,
                       edgecolor=poly.get_edgecolor(),
                       linewidth=poly.get_linewidth())
poly.set_visible(False)
ax.add_artist(new_poly)
plt.draw()

You should arrive at something like the figure below:

CDF with offending points removed

like image 130
bnaecker Avatar answered May 01 '26 10:05

bnaecker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!