pandas.Series.plot.bar#

Series.plot.bar(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.barh

水平条形图。

DataFrame.plot

对DataFrame进行打印。

matplotlib.pyplot.bar

用matplotlib绘制条形图。

示例

基本情节。

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

将整个数据帧绘制为条形图。每列都被分配了不同的颜色,每一行都沿水平轴嵌套在一组中。

>>> 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.bar(rot=0)
../../_images/pandas-Series-plot-bar-2.png

绘制DataFrame的堆叠条形图

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

图形可以按列拆分,而不是嵌套 subplots=True 。在本例中,一个 numpy.ndarraymatplotlib.axes.Axes 都被退回了。

>>> axes = df.plot.bar(rot=0, subplots=True)
>>> axes[1].legend(loc=2)  
../../_images/pandas-Series-plot-bar-4.png

如果您不喜欢默认颜色,您可以指定希望如何为每一列着色。

>>> axes = df.plot.bar(
...     rot=0, subplots=True, color={"speed": "red", "lifespan": "green"}
... )
>>> axes[1].legend(loc=2)  
../../_images/pandas-Series-plot-bar-5.png

绘制单列。

>>> ax = df.plot.bar(y='speed', rot=0)
../../_images/pandas-Series-plot-bar-6.png

仅绘制DataFrame的选定类别。

>>> ax = df.plot.bar(x='lifespan', rot=0)
../../_images/pandas-Series-plot-bar-7.png