Created
December 21, 2016 21:23
-
-
Save rjurney/98810280f601561f34b5af4720932158 to your computer and use it in GitHub Desktop.
Plot a pyspark.RDD.histogram as a pyplot histogram (via bar)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%matplotlib inline | |
buckets = [-87.0, -15, 0, 30, 120] | |
rdd_histogram_data = ml_bucketized_features\ | |
.select("ArrDelay")\ | |
.rdd\ | |
.flatMap(lambda x: x)\ | |
.histogram(buckets) | |
create_hist(rdd_histogram_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_hist(rdd_histogram_data): | |
"""Given an RDD.histogram, plot a pyplot histogram""" | |
heights = np.array(rdd_histogram_data[1]) | |
full_bins = rdd_histogram_data[0] | |
mid_point_bins = full_bins[:-1] | |
widths = [abs(i - j) for i, j in zip(full_bins[:-1], full_bins[1:])] | |
bar = plt.bar(mid_point_bins, heights, width=widths, color='b') | |
return bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your code. But I found that you should add
align=edge
when callingplt.bar
, as the default parameter isalign=center
, which is not what you want in this case.