3

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

When I run the code you put, I get an error at this line: >>> plt.hist(x,normed=1,bins=bins,histtype='step',cumulative=1) ,Which results in this message: UnboundLocalError: local variable 'ymin' referenced before assignment.
@MaxSamuels I don't see any local variable ymin. Maybe you forgot global somewhere? What is ymin?
Oh, sorry, Forgot to add screenshot to my error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.