pandas.Series.plot.barh#

Series.plot.barh(x=None, y=None, **kwargs)[源代码]#

绘制水平条形图。

水平条形图是一种用矩形条形图表示定量数据的曲线图,条形图的长度与它们所表示的值成比例。条形图显示了不同类别之间的比较。曲线图的一条轴显示正在比较的特定类别,另一条轴表示测量值。

参数
x标签或位置,可选

允许绘制一列与另一列的对比图。如果未指定,则使用DataFrame的索引。

y标签或位置,可选

允许绘制一列与另一列的对比图。如果未指定,则使用所有数字列。

color字符串、类似数组或dict,可选

DataFrame的每个列的颜色。可能的值包括:

  • 由名称、RGB或RGBA代码引用的单个颜色串,

    例如‘red’或‘#a98d19’。

  • 按名称、RGB或RGBA引用的颜色串序列

    代码,它将递归地用于每一列。例如 [“绿色”、“黄色”] 每列的条形将以绿色或黄色交替填充。如果只有一列要打印,则仅使用颜色列表中的第一种颜色。

  • {列名}形式的词典COLOR},这样每列都将

    相应地涂上颜色。例如,如果您的列被调用 ab ,然后传递{‘a’:‘green’,‘b’:‘red’}将为列添加颜色条 a 以绿色和条形表示的列 b 穿红色的。

1.1.0 新版功能.

**kwargs

中记录了其他关键字参数 DataFrame.plot()

退货
Matplotlib.axs.或其中的np.ndarray

Ndarray与一个一起返回 matplotlib.axes.Axes 每列当 subplots=True

参见

DataFrame.plot.bar

垂直条形图。

DataFrame.plot

使用matplotlib绘制DataFrame的绘图。

matplotlib.axes.Axes.bar

使用matplotlib绘制垂直条形图。

示例

基本示例

>>> df = pd.DataFrame({'lab': ['A', 'B', 'C'], 'val': [10, 30, 20]})
>>> ax = df.plot.barh(x='lab', y='val')
../../_images/pandas-Series-plot-barh-1.png

将整个DataFrame绘制为水平条形图

>>> speed = [0.1, 17.5, 40, 48, 52, 69, 88]
>>> lifespan = [2, 8, 70, 1.5, 25, 12, 28]
>>> index = ['snail', 'pig', 'elephant',
...          'rabbit', 'giraffe', 'coyote', 'horse']
>>> df = pd.DataFrame({'speed': speed,
...                    'lifespan': lifespan}, index=index)
>>> ax = df.plot.barh()
../../_images/pandas-Series-plot-barh-2.png

绘制DataFrame的堆叠Barh图

>>> ax = df.plot.barh(stacked=True)
../../_images/pandas-Series-plot-barh-3.png

我们可以为每一列指定颜色

>>> ax = df.plot.barh(color={"speed": "red", "lifespan": "green"})
../../_images/pandas-Series-plot-barh-4.png

将DataFrame的一列绘制为水平条形图

>>> speed = [0.1, 17.5, 40, 48, 52, 69, 88]
>>> lifespan = [2, 8, 70, 1.5, 25, 12, 28]
>>> index = ['snail', 'pig', 'elephant',
...          'rabbit', 'giraffe', 'coyote', 'horse']
>>> df = pd.DataFrame({'speed': speed,
...                    'lifespan': lifespan}, index=index)
>>> ax = df.plot.barh(y='speed')
../../_images/pandas-Series-plot-barh-5.png

绘制DataFrame与所需列的对比图

>>> speed = [0.1, 17.5, 40, 48, 52, 69, 88]
>>> lifespan = [2, 8, 70, 1.5, 25, 12, 28]
>>> index = ['snail', 'pig', 'elephant',
...          'rabbit', 'giraffe', 'coyote', 'horse']
>>> df = pd.DataFrame({'speed': speed,
...                    'lifespan': lifespan}, index=index)
>>> ax = df.plot.barh(x='lifespan')
../../_images/pandas-Series-plot-barh-6.png