前言
条形图是一种把连续数据画成数据条的表现形式,通过比较不同组的条形长度,从而对比不同组的数据量大小。描绘条形图的要素有3个:组数、组宽度、祖限。绘制条形图时,不同组之间是由空隙的。条形用来比较两个或两个以上的价值(不同时间或者不同条件),只有一个变量,通常用于较小的数据集分析。条形图也可横向排列,或用多维方式表达。
绘制各门店服装销量比较条形图
这里使用了Pyecharts库的faker模块的Faker对象伪造了两组服装销量数据。代码如下:
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
from pyecharts.faker import Fakerdef bar_base() -> Bar:c = (Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)).add_xaxis(Faker.clothes).add_yaxis("门店A", Faker.values()).add_yaxis("门店B", Faker.values()).set_global_opts(title_opts=opts.TitleOpts(title="各门店服装销量比较条形图"),toolbox_opts=opts.ToolboxOpts(),legend_opts=opts.LegendOpts(is_show=True, pos_left='center', pos_top='top', item_width=25, item_height=25),xaxis_opts=opts.AxisOpts(name='门店', name_textstyle_opts=opts.TextStyleOpts(color='red', font_size=20),axislabel_opts=opts.LabelOpts(font_size=15, rotate=-15)),yaxis_opts=opts.AxisOpts(name='销量', name_textstyle_opts=opts.TextStyleOpts(color='red', font_size=20),axislabel_opts=opts.LabelOpts(font_size=15),name_location="middle")).set_series_opts(label_opts=opts.LabelOpts(is_show=True, position='top', color='black', font_size=15)))return c
绘制的图形如下图所示:
绘制横向条形图
绘制横向条形图非常简单,只需在添加完数据后加一行代码.reversal_axis()即可,示例代码如下:
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
from pyecharts.faker import Fakerdef bar_base() -> Bar:c = (Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)).add_xaxis(Faker.clothes).add_yaxis("门店A", Faker.values()).add_yaxis("门店B", Faker.values()).reversal_axis().set_global_opts(title_opts=opts.TitleOpts(title="各门店服装销量比较条形图"),toolbox_opts=opts.ToolboxOpts(),legend_opts=opts.LegendOpts(is_show=True, pos_left='center', pos_top='top', item_width=25, item_height=25)))return c
绘制的图形如下图所示:
绘制堆叠条形图
绘制堆叠条形图非常简单,只需要在添加数据时加上stack="stack1"即可,示例代码如下:
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
from pyecharts.faker import Fakerdef bar_base() -> Bar:c = (Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)).add_xaxis(Faker.clothes).add_yaxis("门店A", Faker.values(), stack="stack1").add_yaxis("门店B", Faker.values(), stack="stack1").set_global_opts(title_opts=opts.TitleOpts(title="各门店服装销量比较条形图"),toolbox_opts=opts.ToolboxOpts(),legend_opts=opts.LegendOpts(is_show=True, pos_left='center', pos_top='top', item_width=25, item_height=25),xaxis_opts=opts.AxisOpts(name='门店', name_textstyle_opts=opts.TextStyleOpts(font_size=20),axislabel_opts=opts.LabelOpts(font_size=15, rotate=0)),yaxis_opts=opts.AxisOpts(name='销量', name_textstyle_opts=opts.TextStyleOpts(font_size=20),axislabel_opts=opts.LabelOpts(font_size=15),name_location="middle")).set_series_opts(label_opts=opts.LabelOpts(is_show=True, position='top', color='black', font_size=15)))return c
图形效果如下图所示:
绘制条形折现组合图
条形图和折线图组合图的实现,就是使用bar.overlap(line)的方法实现组合,注意折线图使用的x轴数据和条形图要一致,示例代码如下:
from pyecharts.charts import Bar, Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType
from pyecharts.faker import Fakerx_data = Faker.clothesdef bar_base() -> Bar:bar = (Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)).add_xaxis(xaxis_data=x_data).add_yaxis("门店A", Faker.values()).add_yaxis("门店B", Faker.values()).set_global_opts(title_opts=opts.TitleOpts(title="各门店服装销量比较条形图"),legend_opts=opts.LegendOpts(is_show=True, pos_left='center', pos_top='top', item_width=25, item_height=25),xaxis_opts=opts.AxisOpts(name='门店', name_textstyle_opts=opts.TextStyleOpts(font_size=20),axislabel_opts=opts.LabelOpts(font_size=15, rotate=0)),yaxis_opts=opts.AxisOpts(name='销量', name_textstyle_opts=opts.TextStyleOpts(font_size=20),axislabel_opts=opts.LabelOpts(font_size=15),name_location="middle")).set_series_opts(label_opts=opts.LabelOpts(is_show=True, position='top', color='black', font_size=15)))line = (Line().add_xaxis(xaxis_data=x_data).add_yaxis("门店c", Faker.values(), symbol='circle', itemstyle_opts={"color": "red", "linewidth": 20},symbol_size=8))return bar.overlap(line)
图形效果如图所示: