pandas 如何绘制 Dataframe 的直方图

eqoofvh9  于 2023-01-19  发布在  其他
关注(0)|答案(1)|浏览(151)

我需要在直方图上绘制 Dataframe 的一行,以便x轴具有x轴中的列数,纵坐标具有它们的值。
我的数据示例

data = [1.7,1.8,2.0,3.2]

在x轴上,我想从1到4的数字,在纵坐标上是相应的值。
像这样:
enter image description here,但是在从1到4的轴上。
先谢了

jgovgodb

jgovgodb1#

您可以执行以下操作:

import pandas as pd
from matplotlib import pyplot as plt

# put data in dataframe
df = pd.DataFrame([1.7, 1.8, 2.0, 3.2])

# make indexes start at 1
df.index += 1

# create axes for plot
fig, ax = plt.subplots()

# plot using "bar" rather than histogram
df.plot.bar(
    width=1,  # make sure bars fill their ranges and edges touch
    ax=ax,
    rot=0,  # make sure labels are upright
    fc="none",  # make bars unfilled 
    ec="k",  # make bar edges black "k"
)

# remove legend if you want to
ax.get_legend().remove()

fig.show()

相关问题