【Python百日进阶-数据分析】Day130 - plotly柱状图(条形图):go.bar()实例1

4.2 plotly.graph_objects条形图

4.2.1 go的基本条形图

如果Plotly Express不能提供良好的起点,也可以使用更通用的go。

import plotly.graph_objects as go
animals=['giraffes', 'orangutans', 'monkeys']

fig = go.Figure([go.Bar(x=animals, y=[20, 14, 23])])
fig.show()

在这里插入图片描述

4.2.2 分组条形图

使用fig.update自定义图形。

import plotly.graph_objects as go
animals=['giraffes', 'orangutans', 'monkeys']

fig = go.Figure(data=[
    go.Bar(name='SF Zoo', x=animals, y=[20, 14, 23]),
    go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29])
])
# 改变条形图模式
fig.update_layout(barmode='group')
fig.show()

在这里插入图片描述

4.2.3 堆叠条形图

import plotly.graph_objects as go
animals=['giraffes', 'orangutans', 'monkeys']

fig = go.Figure(data=[
    go.Bar(name='SF Zoo', x=animals, y=[20, 14, 23]),
    go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29])
])
# 改变条形图模式
fig.update_layout(barmode='stack')
fig.show()

在这里插入图片描述

4.2.4 带悬停文本的条形图

import plotly.graph_objects as go

x = ['Product A', 'Product B', 'Product C']
y = [20, 14, 23]

# 悬停文本使用 hovertext 关键字参数
fig = go.Figure(data=[go.Bar(x=x, y=y,
            hovertext=['27% 市场占有率', '24% 市场占有率', '19% 市场占有率'])])
# Customize aspect
fig.update_traces(marker_color='rgb(158,202,225)', marker_line_color='rgb(8,48,107)',
                  marker_line_width=1.5, opacity=0.6)
fig.update_layout(title_text='January 2013 销售报告')
fig.show()

在这里插入图片描述

4.2.5 带直接文本标签的条形图

import plotly.graph_objects as go

x = ['Product A', 'Product B', 'Product C']
y = [20, 14, 23]

# 直接文本标签使用 textposition='auto'
fig = go.Figure(data=[go.Bar(
            x=x, y=y,
            text=y,
            textposition='auto',
        )])

fig.show()

在这里插入图片描述

4.2.6 使用uniformtext控制文本大小

如果希望所有文本标签的大小相同,可以使用uniformtext布局参数。minsize属性设置字体大小,mode属性设置无法与所需字体大小匹配的标签的情况:要么隐藏它们,要么显示溢出。在下面的示例中,我们还使用textposition强制文本位于条外。

import plotly.express as px

df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
fig = px.bar(df, y='pop', x='country', text='pop')
fig.update_traces(texttemplate='%{text:.2s}', textposition='outside')
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')
fig.show()

在这里插入图片描述

4.2.7 旋转条形图标签

import plotly.graph_objects as go

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
          'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

fig = go.Figure()
fig.add_trace(go.Bar(
    x=months,
    y=[20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17],
    name='主要产品',
    marker_color='indianred'
))
fig.add_trace(go.Bar(
    x=months,
    y=[19, 14, 22, 14, 16, 19, 15, 14, 10, 12, 12, 16],
    name='次要产品',
    marker_color='lightsalmon'
))

# 在这里,我们修改X轴的角度,从而产生旋转的标签。
fig.update_layout(barmode='group', xaxis_tickangle=-45)
fig.show()

在这里插入图片描述

4.2.8 自定义单个条颜色

import plotly.graph_objects as go

colors = ['lightslategray',] * 5
colors[1] = 'crimson'

fig = go.Figure(data=[go.Bar(
    x=['Feature A', 'Feature B', 'Feature C',
       'Feature D', 'Feature E'],
    y=[20, 14, 23, 25, 22],
    marker_color=colors # 标记颜色可以是单一颜色值,也可以是iterable
)])
fig.update_layout(title_text='最少使用的功能')
fig.show()

在这里插入图片描述

4.2.9 自定义单个条的宽度

import plotly.graph_objects as go

fig = go.Figure(data=[go.Bar(
    x=[1, 2, 3, 5.5, 10],
    y=[10, 8, 6, 4, 2],
    width=[0.8, 0.8, 0.8, 3.5, 4] # 自定义宽度
)])

fig.show()

在这里插入图片描述
具有自定义宽度的条形图可用于制作mekko图(也称为marimekko图、马赛克图或variwide图)。

import plotly.graph_objects as go
import numpy as np

labels = ["apples","oranges","pears","bananas"]
widths = np.array([10,20,20,50])

data = {
    "South": [50,80,60,70],
    "North": [50,20,40,30]
}

fig = go.Figure()
for key in data:
    fig.add_trace(go.Bar(
        name=key,
        y=data[key],
        x=np.cumsum(widths)-widths,
        width=widths,
        offset=0,
        customdata=np.transpose([labels, widths*data[key]]),
        texttemplate="%{y} x %{width} =<br>%{customdata[1]}",
        textposition="inside",
        textangle=0,
        textfont_color="white",
        hovertemplate="<br>".join([
            "label: %{customdata[0]}",
            "width: %{width}",
            "height: %{y}",
            "area: %{customdata[1]}",
        ])
    ))

fig.update_xaxes(
    tickvals=np.cumsum(widths)-widths/2,
    ticktext= ["%s<br>%d" % (l, w) for l, w in zip(labels, widths)]
)

fig.update_xaxes(range=[0,100])
fig.update_yaxes(range=[0,100])

fig.update_layout(
    title_text="Marimekko Chart",
    barmode="stack",
    uniformtext=dict(mode="hide", minsize=10),
)

fig.show()

在这里插入图片描述

4.2.10 自定义单个条形起点

import plotly.graph_objects as go

years = ['2016','2017','2018']

fig = go.Figure()
fig.add_trace(go.Bar(x=years, y=[500, 600, 700],
                base=[-500,-600,-700],
                marker_color='crimson',
                name='expenses'))
fig.add_trace(go.Bar(x=years, y=[300, 400, 700],
                base=0,
                marker_color='lightslategrey',
                name='revenue'
                ))

fig.show()

在这里插入图片描述

4.2.11 彩色和样式条形图

在本例中,布局的几个参数是定制的,因此直接使用go很方便。布局(…)构造函数而不是调用fig.update。

import plotly.graph_objects as go

years = [1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
         2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012]

fig = go.Figure()
fig.add_trace(go.Bar(x=years,
                y=[219, 146, 112, 127, 124, 180, 236, 207, 236, 263,
                   350, 430, 474, 526, 488, 537, 500, 439],
                name='Rest of world',
                marker_color='rgb(55, 83, 109)'
                ))
fig.add_trace(go.Bar(x=years,
                y=[16, 13, 10, 11, 28, 37, 43, 55, 56, 88, 105, 156, 270,
                   299, 340, 403, 549, 499],
                name='China',
                marker_color='rgb(26, 118, 255)'
                ))

fig.update_layout(
    title='美国塑料废料出口',
    xaxis_tickfont_size=14,
    yaxis=dict(
        title='USD (millions)',
        titlefont_size=16,
        tickfont_size=14,
    ),
    legend=dict(
        x=0,
        y=1.0,
        bgcolor='rgba(255, 255, 255, 0)',
        bordercolor='rgba(255, 255, 255, 0)'
    ),
    barmode='group',
    bargap=0.15, # 相邻位置坐标的钢筋之间的间隙。
    bargroupgap=0.1 # 同一位置坐标的钢筋之间的间隙。
)
fig.show()

在这里插入图片描述

4.2.12 相对条形图模式

使用“relative”条形模式时,条形相互堆叠,轴下方为负值,轴上方为正值。

import plotly.graph_objects as go
x = [1, 2, 3, 4]

fig = go.Figure()
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16]))
fig.add_trace(go.Bar(x=x, y=[6, -8, -4.5, 8]))
fig.add_trace(go.Bar(x=x, y=[-15, -3, 4.5, -8]))
fig.add_trace(go.Bar(x=x, y=[-1, 3, -3, -4]))

fig.update_layout(barmode='relative', title_text='相对模式')
fig.show()

在这里插入图片描述

4.2.13 具有排序或有序类别的条形图

对于类别名称的字母数字顺序,将categoryorder设置为“类别升序”或“类别降序”,或者对于值的数字顺序,将categoryorder设置为“总升序”或“总降序”。categoryorder了解更多信息。请注意,目前不可能按特定轨迹对条形图进行排序,只能按总值进行排序。当然,如果需要更多自定义,可以在绘制数据之前对其进行排序。
本例使用categoryorder按字母顺序排列条形图:“类别升序”

import plotly.graph_objects as go

x=['b', 'a', 'c', 'd']
fig = go.Figure(go.Bar(x=x, y=[2,5,1,9], name='Montreal'))
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa'))
fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto'))

fig.update_layout(barmode='stack', xaxis={'categoryorder':'category ascending'})
fig.show()

在这里插入图片描述
此示例演示如何通过将categoryorder定义为“array”来自定义排序顺序,从而从属性categoryarray派生排序。

import plotly.graph_objects as go

x=['b', 'a', 'c', 'd']
fig = go.Figure(go.Bar(x=x, y=[2,5,1,9], name='Montreal'))
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa'))
fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto'))

fig.update_layout(barmode='stack', xaxis={'categoryorder':'array', 'categoryarray':['d','a','c','b']})
fig.show()

在这里插入图片描述
本例使用categoryorder“total descending”按降序排列条形图

import plotly.graph_objects as go

x=['b', 'a', 'c', 'd']
fig = go.Figure(go.Bar(x=x, y=[2,5,1,9], name='Montreal'))
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa'))
fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto'))

fig.update_layout(barmode='stack', xaxis={'categoryorder':'total descending'})
fig.show()

在这里插入图片描述

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
python数据分析:基于plotly的动态可视化绘图.pdf》是一本关于使用Python进行数据分析的书籍,主要介绍了如何利用plotly库进行动态可视化绘图。 数据分析是通过收集、清洗、加工、分析和呈现大量数据来揭示其中潜在规律和洞察的过程。而可视化则是将数据以图表等可视化方式展示,帮助我们更直观地理解和解读数据。plotlyPython中一款功能强大的可视化库,它可以生成各种交互式图表,支持动态可视化和在线展示。 《python数据分析:基于plotly的动态可视化绘图.pdf》通过实例演示了如何使用plotly库进行数据分析中常用的可视化操作,包括折线图、柱状图、散点图、饼图、地理图等。使用plotly库可以轻松创建动态可视化图表,通过调整参数和设置动画效果,可以让图表更生动、更具吸引力。 动态可视化是一种通过动态变化的图表来展示数据的方法。它可以帮助我们更好地理解数据的变化趋势和规律,发现数据中隐藏的特征和模式。在数据分析中,动态可视化常用于时间序列数据、实时数据等领域,能够直观地展示数据的变化过程和动态趋势。 综上所述,《python数据分析:基于plotly的动态可视化绘图.pdf》是一本介绍如何使用plotly库进行动态可视化绘图的书籍,通过实例演示了使用plotly库创建各种动态可视化图表的方法和技巧,对于对数据分析和可视化有兴趣的人来说,是一本不可多得的学习资料。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岳涛@心馨电脑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值