直方图没有绘制完整数组

时间:2017-05-23 14:09:00

标签: python arrays matplotlib request subplot

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

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

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

图表仅通过SantaFe输出Gallup,Gallup输出8,SantaFe输出1。 这是代码:

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()

1 个答案:

答案 0 :(得分:1)

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

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之间。还有一个地方(拉顿)降水量在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()

enter image description here