python - Generating a matlibplot bar chart from two columns of data -
i trying build vertical bar chart based on examples provided in how plot simple bar chart (python, matplotlib) using input *.txt file? , pylab_examples example code: barchart_demo.py.
# bar chart import numpy np import matplotlib.pyplot plt data = """100 0.0 5 500.25 2 10.0 4 5.55 3 950.0 3 300.25""" counts = [] values = [] line in data.split("\n"): x, y = line.split() values = x counts = y plt.bar(counts, values) plt.show()
current receiving following error: assertionerror: incompatible sizes: argument 'height' must length 15 or scalar
. not sure if plt.bar()
function defined correctly. there may other issues have overlooked in trying replicate 2 mentioned examples.
x, y = line.split() returns tuple of strings. believe need convert them ints , floats. need values.append(x) , values.append(y).
import numpy np import matplotlib.pyplot plt data = """100 0.0 5 500.25 2 10.0 4 5.55 3 950.0 3 300.25""" counts = [] values = [] line in data.split("\n"): x, y = line.split() values.append(int(x)) counts.append(float(y)) plt.bar(counts, values) plt.show()
given 100 value in first line (compared <= 5 rest), makes pretty ugly bar chart though.
Comments
Post a Comment