python自学笔记之可视化神器pyecharts(二)

tech2022-08-16  149

以柱形图为例,下列代码较为详细的展示了初始配置项、全局配置项、系列配置项

同时标注了x-y轴翻转,堆叠方法等。

from pyecharts.charts import Bar from pyecharts import options as opts from pyecharts.globals import ThemeType from snapshot_selenium import snapshot from pyecharts.render import make_snapshot columns = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] data1 = [3.5, 7.8, 9.5, 24.6, 27.8, 75.5, 129.5, 125.5, 35.7, 31.2, 7.8, 2.5] data2 = [5.4, 3.6, 8.9, 25.7, 26.4, 72.5, 156.5, 143.5, 44.5, 19.5, 7.2, 1.9] # pyecharts链式调用 bar=( Bar(init_opts=opts.InitOpts( theme=ThemeType.WHITE, bg_color='white', width='900px', height='500px', page_title = "Awesome-pyecharts") ) # init_opts=opts.InitOpts()为初始化配置项。上述均为默认值,若无需配置,直接Bar()即可 # theme:图表主题,默认为ThemeType.WHITE,此外还有ThemeType.LIGHT,ThemeType.PURPLE_PASSION等等 # bg_color:背景颜色 # width、height:画布的长、宽 # page_title:网页标题,默认为Awesome-pyecharts .add_xaxis(columns) .add_yaxis('降雨量', data1) .add_yaxis('蒸发量', data2) # .add_yaxis('降雨量',data1,stack='stack1') # .add_yaxis('蒸发量',data2,stack='stack1') # stack:数据堆叠,相同的stack堆叠 # .reversal_axis() # .set_series_opts(label_opts=opts.LabelOpts(position='right')) # x,y轴翻转,且标签显示的位置在右边 .set_global_opts( title_opts = opts.TitleOpts( title='全年降雨量和蒸发量', subtitle='xx省xx市', title_link = 'https://www.baidu.com/', title_target='self', subtitle_link = 'https://www.baidu.com/', subtitle_target='blank', pos_left = '10%', item_gap = 10, padding=5), # set_global_opts()全局配置项 # title_opts = opts.TitleOpts()标题配置项 # title:标题,支持\n换行 # subtitle:副标题,支持\n换行 # (sub)titile_link:主/副标题跳转url链接 # (sub)title_target:主/副标题跳转链接方式,默认为blank-新窗口打开,而self-当前窗口打开 # pos_left/right/top/bottom:title组件离容器左/右/上/下侧的距离 # left 的值可以是像20这样的具体像素值,可以是像'20%'这样相对于容器高宽的百分比 # 也可以是 'left','center','right','top','middle','bottom' # 如果left的值为'left','center','right',组件会根据相应的位置自动对齐. # item_gap:副标题之间的间距,默认为10 # padding:标题内边距,单位px,默认各方向内边距为5,接受数组分别设定上右下左边距 # padding=5:设置内边距为5 # padding=[5, 10]:设置上下的内边距为5,左右的内边距为10 # padding=[5,10,5,10]:分别设置四个方向的内边距 上、右、下、左 legend_opts = opts.LegendOpts( is_show = True, type_ = 'plain', selected_mode = True, pos_left='50%', item_gap=10, inactive_color = '#ccc', textstyle_opts = None, legend_icon ='pin' ), # legend_opts = opts.LegendOpts()图例配置项 # is_show:是否显示图例,True/False # type_:图例类型,默认为'plain'-普通类型;'scroll'-可滚动翻页的图例,当图例数量较多时可以使用 # selected_mode:图例选择的模式,控制是否可以通过点击图例改变系列的显示状态。默认开启图例选择,可以设成 False 关闭,除此之外也可以设成 'single' 或者 'multiple' 使用单选或者多选模式。 # pos_left/right/top/bottom:图例组件离容器左/右/上/下侧的距离,用法参考上面 # item_gap/width/height:图例每项的间隔默认10/图例图形的宽度,默认50/图例图形的高度,默认14 # inactive_color:图例关闭时的颜色。默认是 #ccc # textstyle_opts:图例组件字体样式,参考 `series_options.TextStyleOpts` # legend_icon:图例项的 icon,ECharts 提供的标记类型包括 'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow', 'none' # 可以通过 'image://url' 设置为图片,其中 URL 为图片的链接,或者 dataURI。 # 可以通过 'path://' 将图标设置为任意的矢量路径 datazoom_opts = opts.DataZoomOpts(), # slider-水平,默认为水平,odatazom_opts = opts.DataZoomOpts(orient="vertical")为垂直 # 具体其余参数不赘述 ) .set_series_opts( label_opts=opts.LabelOpts( is_show=False, position='top', font_size=10, font_style = 'italic', color= '#FF6633', margin = 20, ), # set_series_opts():系列配置项 # label_opts=opts.LabelOpts():标签配置项 # is_show:是否显示标签,对应柱状图上各柱的数据 # position:标签的位置,可选 'top','left','right','bottom','inside','insideLeft','insideRight'..... # font_size:文字的大小 # font_style:字体文体的风格,可选 'normal','italic','oblique' # color:字体的颜色 # margin:刻度标签与轴线之间的距离 markline_opts=opts.MarkLineOpts( data=[ opts.MarkLineItem(type_="min", name="最小值"), opts.MarkLineItem(type_="max", name="最大值"), opts.MarkLineItem(type_ = 'average',name="平均值"), ] ), # markline_opts=opts.MarkLineOpts():标记线配置项 markpoint_opts=opts.MarkPointOpts( data=[ opts.MarkPointItem(type_='max',name='最大值'), opts.MarkPointItem(type_='min',name='最小值'), opts.MarkPointItem(type_='average',name='平均值'), ] # markpoint_opts=opts.MarkPointOpts():标记点配置项 ), ) ) bar.render() make_snapshot(snapshot,bar.render(), "barwow.png") # 保存为png格式图片

最新回复(0)