Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib cumulative frequency graph with extra line in Python

When I run my code to generate a graph of the relative cumulative frequencies of a dataset, my graphs come out with a line straight down at the point the graph crosses the line y=1 on the right side, like this one.

The y-axis is limited to the range y=0 to y=1, representing 0% to 100% of the cumulative frequency, once the graph reached y=1, or 100%, it should continue at y=1 until the upper limit of the x-axis, which goes from x=0 to x=2, similar to this graph.

Is there some way to ensure that the historgram contiues at y=1 after y=1 has been reached? I need my x-axis to remain on the range [0,2] and the y-axis on the range [0,1].

Here is my Python code I use to generate my graphs:

import matplotlib.pyplot as plt 
# ...
plt.ylabel('Relative Cumulative Frequency')
plt.xlabel('Normalized Eigenvalues')
plt.hist(e.real, bins = 50, normed=1, histtype='step', cumulative=True)
# Limit X and Y ranges   
plt.xlim(0, 2)
plt.ylim(0, 1)

Thanks, Max

like image 972
Max Samuels Avatar asked Dec 20 '25 23:12

Max Samuels


1 Answers

You can do this by creating your own bins and setting the last bin to np.Inf:

import matplotlib.pyplot as plt
import numpy as np
...
x = np.random.rand(100,1)

plt.ylabel('Relative Cumulative Frequency')
plt.xlabel('Normalized Eigenvalues')

binsCnt = 50
bins = np.append(np.linspace(x.min(), x.max(), binsCnt), [np.inf])
plt.hist(x, bins = bins, normed=1, histtype='step', cumulative=True)
# Limit X and Y ranges
plt.xlim(0, 2)
plt.ylim(0, 1)
plt.show()

plot

like image 186
Dimitri Podborski Avatar answered Dec 23 '25 13:12

Dimitri Podborski



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!