我試圖根據How to plot a very simple bar chart (Python, Matplotlib) using input *.txt file?和pylab_examples example code: barchart_demo.py中提供的示例構建垂直條形圖。從兩列數據生成matlibplot條形圖
# a bar chart
import numpy as np
import matplotlib.pyplot as plt
data = """100 0.0
5 500.25
2 10.0
4 5.55
3 950.0
3 300.25"""
counts = []
values = []
for line in data.split("\n"):
x, y = line.split()
values = x
counts = y
plt.bar(counts, values)
plt.show()
當前我收到以下錯誤:AssertionError: incompatible sizes: argument 'height' must be length 15 or scalar
。我不確定plt.bar()
函數是否正確定義。在嘗試複製前面提到的兩個示例時,可能會有其他問題被忽略。