2

So I've got some data which I wish to plot via a frequency density (unequal class width) histogram, and via some searching online, I've created this to allow me to do this.

import numpy as np
import matplotlib.pyplot as plt

plt.xkcd()
freqs = np.array([3221, 1890, 866, 529, 434, 494, 382, 92, 32, 7, 7])
bins = np.array([0, 5, 10, 15, 20, 30, 50, 100, 200, 500, 1000, 1500])
widths = bins[1:] - bins[:-1]
heights = freqs.astype(np.float)/widths

plt.xlabel('Cost in Pounds')
plt.ylabel('Frequency Density')

plt.fill_between(bins.repeat(2)[1:-1], heights.repeat(2), facecolor='steelblue')
plt.show()  

As you may see however, this data stretches into the thousands on the x axis and on the y axis (density) goes from tiny data (<1) to vast data (>100). To solve this I will need to break both axis. The closest to help I've found so far is this, which I've found hard to use. Would you be able to help?
Thanks, Aj.

1 Answer 1

0

You could just use a bar plot. Setting the xtick labels to represent the bin values.

With logarithmic y scale

import numpy as np
import matplotlib.pyplot as plt

plt.xkcd()
fig, ax = plt.subplots()
freqs = np.array([3221, 1890, 866, 529, 434, 494, 382, 92, 32, 7, 7])
freqs = np.log10(freqs)
bins = np.array([0, 5, 10, 15, 20, 30, 50, 100, 200, 500, 1000, 1500])
width = 0.35
ind = np.arange(len(freqs))
rects1 = ax.bar(ind, freqs, width)
plt.xlabel('Cost in Pounds')
plt.ylabel('Frequency Density')
tick_labels = [ '{0} - {1}'.format(*bin) for bin in  zip(bins[:-1], bins[1:])]
ax.set_xticks(ind+width)
ax.set_xticklabels(tick_labels)
fig.autofmt_xdate()
plt.show() 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! However, part of what I'm trying to do is represent unequal class widths by representing frequency by area. To do this, I need to show the plot's width. Thanks for helping so far though.
@user3033981 ahh, ok. I guess a logarithmic scale in both directions won't help much either. It would be hard to see relative width's on a log scale. You can also set the width of each bar individually in this example, but same problem occurres since the width's would need to be scaled down to make much sense.

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.