I have a list list = [['0-50',4],['50-100',11],['100-150',73],['150-200',46]]
and I want to show it on a histogram
using mpld3
in python pyspark
. The first part in each element of list is range which will be on x-axis of histogram
and the second part is the number of people in that range which will on y-axis. How can I make a bar chart using either matplotlib
or mpld3
in pyspark
?
UPDATE: I tried below code based on [this] example 1 and it displays the bar chart
but the output is visually very bad with lots of grey colored area around the plot boundary. How can I get it look clear and better in terms of visualization?
import numpy as np
import matplotlib.pyplot as plt
list = [['0-50',4],['50-100',11],['100-150',73],['150-200',46]]
n_groups = len(list)
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.4
error_config = {'ecolor': '0.3'}
number = []
ranges = []
for item in list:
number.append(item[1])
ranges.append(item[0])
rects1 = plt.bar(index, number, bar_width,
alpha=opacity,
color='b',
error_kw=error_config)
plt.xlabel('Number')
plt.ylabel('range')
plt.xticks(index + bar_width, (ranges[0],ranges[1],ranges[2],ranges[3]))
plt.legend()
plt.tight_layout()
plt.show()