matplotlib 绘制布尔数据和类别未显示

ctehm74n  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(115)

我在Pandas数据框中有一列TFColumn,其中包含布尔值(即TRUE或FALSE)。我尝试使用条形图来绘制TRUE和FALSE值的数量。
但是,条形图下的X轴上显示的不是TRUE或FALSE值,而是数值。如何删除数值轴并显示分类轴?
下面是我为此图运行的代码:

plt.bar(dat[dat.yr == 2019]['TFColumn'].value_counts().index, dat[dat.yr == 2019]['TFColumn'].value_counts().values)

raogr8fs

raogr8fs1#

我想知道如果你的任务可以更简单w/oPandas:

In [4]: from numpy  import random
   ...: from matplotlib.pyplot import show, subplots
   ...: random.seed(20221104)
   ...: TFColumn = random.choice((False, True, True), 18000)
   ...: true = sum(TFColumn) ; false = len(TFColumn)-true
   ...: 
   ...: fig, ax = subplots()
   ...: bars = ax.bar((0, 1), (false, true), 0.4)
   ...: ax.set_xticks((0, 1))
   ...: ax.set_xticklabels(('False', 'True'))
   ...: show()

相关问题