直方图未绘制完整数组

Histogram is not plotting the full array

我正在编写 python 代码以将区域插入直方图中。但是,直方图并未绘制所呈现的完整阵列。我测试了数组以通过打印两个数组来找出发生这种情况的原因。结果最终对于信息是准确的,但与数据数组相比却不准确。以下是数组:

['Farmington', 'Gallup', 'Grants', 'Las Vegas', 'Raton', 'Santa Fe', 'Taos', 'Tijeras', 'Tucumcari']

[0.002, 0, 0, 0.008, 0.225, 0.0, 0.0, 0.0, 0.01]

该图表仅通过 SantaFe 输出 Gallup,以及 8 个 Gallup 和 1 个 SantaFe。 这是代码:

import matplotlib.pyplot as plt
rainhist = [0.002, 0, 0, 0.008, 0.225, 0.0, 0.0, 0.0, 0.01]
cityhist = ['Farmington', 'Gallup', 'Grants', 'Las Vegas', 'Raton', 'Santa Fe', 'Taos', 'Tijeras', 'Tucumcari']
print(cityhist)
print(rainhist)
table = plt.subplot()
table.hist(rainhist, bins=10)
table.set_title("New Mexico North")
table.set_xlabel("Areas")
table.set_ylabel("Accumulation (in.)")
table.set_xticklabels(cityhist, rotation_mode="anchor")
plt.show()

您需要以不同方式解释直方图:

import matplotlib.pyplot as plt
rainhist = [0.002, 0, 0, 0.008, 0.225, 0.0, 0.0, 0.0, 0.01]
cityhist = ['Farmington', 'Gallup', 'Grants', 'Las Vegas', 'Raton', 
            'Santa Fe', 'Taos', 'Tijeras', 'Tucumcari']
table = plt.subplot()
table.hist(rainhist, bins=10)
table.set_title("New Mexico North")
table.set_ylabel("Number of areas")
table.set_xlabel("Accumulation (in.)")
plt.show()

有 8 个地区的降水量在 0 到 0.0225 英寸之间。有一个地方(Raton)的降水量在 0.2025 到 0.225 英寸之间。

可能 rainhist 中的值已经是 显示为条形的值。然后您可以简单地绘制它们而无需再次对它们进行直方图绘制。

import matplotlib.pyplot as plt
rainhist = [0.002, 0, 0, 0.008, 0.225, 0.0, 0.0, 0.0, 0.01]
cityhist = ['Farmington', 'Gallup', 'Grants', 'Las Vegas', 'Raton', 
            'Santa Fe', 'Taos', 'Tijeras', 'Tucumcari']
ax = plt.subplot()
ax.bar(range(len(rainhist)), rainhist)
ax.set_xticks(range(len(rainhist)))
ax.set_xticklabels(cityhist, rotation=90)
ax.set_ylabel("Accumulation (in.)")
plt.tight_layout()
plt.show()