我是Python的初学者。使用matplotlib和numpy绘制直方图时遇到问题。我想研究在汽车使用年限范围内的汽车数量之间的分布。我的x轴是age_of_car,而我的y轴是number_of_car。以下是我的代码:
age_of_car = np.array(['0-<1', '1-<2', '2-<3', '3-<4', '4-<5',
'5-<6', '6-<7', '7-<8', '8-<9', '9-<10','10-<11',
'11<12', '12-<13','13-<14', '14-<15', '15-<16',
'16-<17', '17-<18','18-<19', '19-<20', '20->'])
number_of_car = np.array(['91614', '87142', '57335', '28392',
'21269', '26551', '27412', '41142', '68076', '88583',
'28487', '28439', '8728', '1557', '458', '179',
'423', '444', '421', '410', '5194'])
num_bins = 20
plt.hist([age,number],num_bins)
plt.show()
答案 0 :(得分:1)
首先,要正确显示数据,您需要将<div v-for="doc in documents" :key="doc.id">
<th></th>
<th>
<a href="javascript:void(0)" @click="getChildDocs(doc.id)" :title="doc.type">
<i :class="{'fas': true, 'fa-file': doc.type == 'file', 'fa-dir': doc.type == 'dir', 'fa-lg': true}"></i>
</a>
</th>
<td>{{ doc.name }}</td>
</div>
中的值转换为整数。为此,您可以在创建数组时使用number_of_car
选项。
第二,您的直方图已经完成,因此您应该使用dtype=int
图:
bar
现在,要使xticks更具可读性,您至少有两种解决方案:
增加图形宽度,直到为所有xtick留出足够的空间。为此,可以在创建图形时使用from matplotlib import pyplot as plt
import numpy as np
age_of_car = np.array(['0-<1', '1-<2', '2-<3', '3-<4', '4-<5',
'5-<6', '6-<7', '7-<8', '8-<9', '9-<10','10-<11',
'11<12', '12-<13','13-<14', '14-<15', '15-<16',
'16-<17', '17-<18','18-<19', '19-<20', '20->'])
number_of_car = np.array(['91614', '87142', '57335', '28392',
'21269', '26551', '27412', '41142', '68076', '88583',
'28487', '28439', '8728', '1557', '458', '179',
'423', '444', '421', '410', '5194'], dtype=int)
fig, ax = plt.subplots()
ax.bar(age_of_car, number_of_car)
fig.tight_layout()
plt.show()
选项:
figsize
使用fig, ax = plt.subplots(figsize=(14, 4))