gpt4 book ai didi

python - Pandas :分类数据的多个直方图

转载 作者:太空宇宙 更新时间:2023-11-04 05:19:25 28 4
gpt4 key购买 nike

我有以下数据框:

    Type       Cat1       Cat2       Cat3
0 A 0.384000 0.393000 0.458000
1 B 0.603337 0.381470 0.299773
2 C 0.585797 0.452570 0.317607
3 D 0.324715 0.765212 0.717755

我想生成三个直方图,每个“猫”一个。每个直方图必须将每个“类型”(例如,类别)的值显示为底部直方图上的 x 标签。

我一直在尝试使用 df.plot(kind='hist') 来做到这一点,但无济于事:

ax=df.plot(kind='hist',subplots=True,layout=(3,1),title='My title',color='orange',grid=True,legend=False)

但我看到了:

  1. x轴没有显示四种类型,只是一堆数字
  2. y 轴的范围从 0 到 4,但它必须从 0 到 1(这些是频率)
  3. 每个子图中只显示一个条形

我做错了什么?


画错了: enter image description here

最佳答案

IIUC:
您不需要直方图,您只需要条形图。

df.set_index('Type').plot.bar(subplots=True, legend=False)

enter image description here


假设 import matplotlib.pyplot as plt 调整垂直间距

axes = df.set_index('Type').plot.bar(subplots=True, legend=False)
plt.subplots_adjust(hspace=0.35)

enter image description here

关于python - Pandas :分类数据的多个直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40805910/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com