当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python pyspark DataFrame.plot.bar用法及代码示例


本文简要介绍 pyspark.pandas.DataFrame.plot.bar 的用法。

用法:

plot.bar(x=None, y=None, **kwds)

垂直条形图。

参数

x标签或位置,可选

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

y标签或位置,可选

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

**kwds可选的

其他关键字参数记录在 pyspark.pandas.Series.plot() pyspark.pandas.DataFrame.plot() 中。

返回

plotly.graph_objs.Figure

backend!=plotly 时返回自定义对象。当subplots=True(仅限 matplotlib)时返回 ndarray。

例子

基本情节。

对于系列:

>>> s = ps.Series([1, 3, 2])
>>> s.plot.bar()

对于 DataFrame :

>>> df = ps.DataFrame({'lab': ['A', 'B', 'C'], 'val': [10, 30, 20]})
>>> df.plot.bar(x='lab', y='val')

将整个 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 = ps.DataFrame({'speed': speed,
...                    'lifespan': lifespan}, index=index)
>>> df.plot.bar()

可以使用 plotly API 按列拆分图形,而不是堆叠。

>>> from plotly.subplots import make_subplots
>>> 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 = ps.DataFrame({'speed': speed,
...                    'lifespan': lifespan}, index=index)
>>> fig = (make_subplots(rows=2, cols=1)
...        .add_trace(df.plot.bar(y='speed').data[0], row=1, col=1)
...        .add_trace(df.plot.bar(y='speed').data[0], row=1, col=1)
...        .add_trace(df.plot.bar(y='lifespan').data[0], row=2, col=1))
>>> fig

绘制单个列。

>>> 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 = ps.DataFrame({'speed': speed,
...                    'lifespan': lifespan}, index=index)
>>> df.plot.bar(y='speed')

仅绘制 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 = ps.DataFrame({'speed': speed,
...                    'lifespan': lifespan}, index=index)
>>> df.plot.bar(x='lifespan')

相关用法


注:本文由纯净天空筛选整理自spark.apache.org大神的英文原创作品 pyspark.pandas.DataFrame.plot.bar。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。