pyecharts是一个非常强大的Python可视化工具库,支持多种图表类型和多种布局方式,其中包括Tab和Legend布局,下面我将详细讲解它们的使用方法和示例说明。
Tab布局
Tab布局是pyecharts中的一种常见布局方式,可以将多种不同类型的图表放在同一个页面中,以Tab切换的形式进行展示。以下是使用Tab布局的完整攻略:
实现步骤
- 导入Tab和各种图表类:
from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Scatter, Tab
- 创建图表对象:
bar = Bar()
line = Line()
scatter = Scatter()
- 向图表对象中添加数据和配置项:
bar.add_xaxis(['A', 'B', 'C', 'D'])
bar.add_yaxis('Data', [10, 20, 30, 40])
bar.set_global_opts(title_opts=opts.TitleOpts(title='Bar Chart'))
line.add_xaxis(['A', 'B', 'C', 'D'])
line.add_yaxis('Data', [20, 30, 40, 50])
line.set_global_opts(title_opts=opts.TitleOpts(title='Line Chart'))
scatter.add_xaxis(['A', 'B', 'C', 'D'])
scatter.add_yaxis('Data', [30, 40, 50, 60])
scatter.set_global_opts(title_opts=opts.TitleOpts(title='Scatter Chart'))
- 创建Tab对象并将各种图表对象添加到Tab中:
tab = Tab()
tab.add(bar, 'Bar Chart')
tab.add(line, 'Line Chart')
tab.add(scatter, 'Scatter Chart')
- 渲染并保存图表:
tab.render('tab.html')
示例说明
下面是一个使用Tab布局展示不同图表的示例:
from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Scatter, Tab
# 初始化各类图表
bar = Bar()
line = Line()
scatter = Scatter()
# 添加数据和配置项
bar.add_xaxis(['A', 'B', 'C', 'D'])
bar.add_yaxis('Data', [10, 20, 30, 40])
bar.set_global_opts(title_opts=opts.TitleOpts(title='Bar Chart'))
line.add_xaxis(['A', 'B', 'C', 'D'])
line.add_yaxis('Data', [20, 30, 40, 50])
line.set_global_opts(title_opts=opts.TitleOpts(title='Line Chart'))
scatter.add_xaxis(['A', 'B', 'C', 'D'])
scatter.add_yaxis('Data', [30, 40, 50, 60])
scatter.set_global_opts(title_opts=opts.TitleOpts(title='Scatter Chart'))
# 将图表添加到Tab中
tab = Tab()
tab.add(bar, 'Bar Chart')
tab.add(line, 'Line Chart')
tab.add(scatter, 'Scatter Chart')
# 渲染并保存图表
tab.render('tab.html')
该示例中创建了一个Tab对象,并向其中添加一个柱状图、一个折线图和一个散点图。Tab对象可以通过add方法将多个图表对象添加到其中,并为每个图表指定一个选项卡名称。最后将Tab渲染成html文件并保存。
Legend布局
Legend布局是pyecharts中的一种常见布局方式,可以将多种相同类型的图表放在同一个页面中,通过Legend切换不同数据系列的显示或隐藏。以下是使用Legend布局的完整攻略:
实现步骤
- 导入Legend和各种图表类:
from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Scatter
from pyecharts.commons.utils import JsCode
- 创建图表对象:
bar = Bar()
line = Line()
scatter = Scatter()
- 向图表对象中添加数据和配置项,并配置Legend:
bar.add_xaxis(['A', 'B', 'C', 'D'])
bar.add_yaxis('Series A', [10, 20, 30, 40])
bar.add_yaxis('Series B', [20, 30, 40, 50])
bar.set_global_opts(title_opts=opts.TitleOpts(title='Bar Chart'),
legend_opts=opts.LegendOpts(type_='scroll', pos_left='center',
orient='vertical', selected_mode='single'))
line.add_xaxis(['A', 'B', 'C', 'D'])
line.add_yaxis('Series A', [20, 30, 40, 50])
line.add_yaxis('Series B', [30, 40, 50, 60])
line.set_global_opts(title_opts=opts.TitleOpts(title='Line Chart'),
legend_opts=opts.LegendOpts(type_='scroll', pos_left='center',
orient='vertical', selected_mode='multiple'))
scatter.add_xaxis(['A', 'B', 'C', 'D'])
scatter.add_yaxis('Series A', [30, 40, 50, 60])
scatter.add_yaxis('Series B', [40, 50, 60, 70])
scatter.set_global_opts(title_opts=opts.TitleOpts(title='Scatter Chart'),
legend_opts=opts.LegendOpts(type_='scroll', pos_left='center',
orient='vertical', selected_mode='multiple',
selector_label_opts=opts.LabelOpts(font_size=12,
color='blue')),
toolbox_opts=opts.ToolboxOpts(is_show=True, feature={'saveAsImage': {}}))
- 创建一个JsCode对象作为Legend的渲染器,使Legend根据当前选项卡中只展示相应数据系列:
js_code = """
var legend_data = params.legendData;
var selected = {};
for (var i = 0; i < legend_data.length; i++) {
selected[legend_data[i]] = false;
}
selected[params.name] = true;
return {
selected: selected
};
"""
bar_legend_renderer = JsCode(js_code)
bar.reversal_axis()
bar.set_series_opts(label_opts=opts.LabelOpts(position='right', font_size=10),
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_='max', name='最大值'),
opts.MarkPointItem(type_='min', name='最小值')]),
emphasis_textcolor='red', legend_opts=opts.LegendOpts(type_='scroll', pos_left='right',
selector_label_opts=opts.LabelOpts(color='red'),
selected_mode='single',
legend_icon='rect'), )
scatter.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{c}'), areastyle_opts=opts.AreaStyleOpts(opacity=0.1),
# animation=False,
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_='average', name='平均值')]),
itemstyle_opts=opts.ItemStyleOpts(color='green'))
scatter_legend_renderer = JsCode(js_code)
scatter.set_global_opts(visualmap_opts=opts.VisualMapOpts(max_=70, min_=30, is_piecewise=True,
pieces=[{'min': 30, 'color': 'green'},
{'min': 50, 'color': 'red'}]),
legend_opts=opts.LegendOpts(type_='scroll', pos_left='right', legend_icon='rectangle',
selected_mode='multiple',
legend_selectedmode='single', legend_formatter='{name}'),
toolbox_opts=opts.ToolboxOpts(is_show=True,
feature={'dataZoom': {}, 'dataView': {}, 'magicType': {},
'restore': {}, 'saveAsImage': {}}),
tooltip_opts=opts.TooltipOpts(trigger='axis', axis_pointer_type='shadow'),
legend_selectedmode='single',
legend_selected=True,
legend_not_selected='Scatter2')
line_legend_renderer = JsCode(js_code)
line.set_series_opts(label_opts=opts.LabelOpts(is_show=True, color='red'),
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_='max', name='最大值'),
opts.MarkPointItem(type_='min', name='最小值')]),
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_='average', name='平均值')]),
markarea_opts=opts.MarkAreaOpts(is_silent=True))
line.set_global_opts(title_opts=opts.TitleOpts(title='Line Chart'), tooltip_opts=opts.TooltipOpts(trigger='axis', axis_pointer_type='shadow'),
legend_opts=opts.LegendOpts(type_='scroll', pos_left='right', legend_icon='roundRect',
selected_mode='multiple', legend_selectedmode='single', legend_formatter='{name}'),
datazoom_opts=opts.DataZoomOpts(), fill_color='blue')
- 创建Tab对象并将各种图表对象添加到Tab中:
tab = Tab()
tab.add(bar, 'Bar Chart')
tab.add(line, 'Line Chart')
tab.add(scatter, 'Scatter Chart')
- 渲染并保存图表:
tab.render('legend.html')
示例说明
下面是一个使用Legend布局展示不同图表的示例:
from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Scatter
from pyecharts.commons.utils import JsCode
# 初始化各类图表
bar = Bar()
line = Line()
scatter = Scatter()
# 添加数据和配置项
bar.add_xaxis(['A', 'B', 'C', 'D'])
bar.add_yaxis('Series A', [10, 20, 30, 40])
bar.add_yaxis('Series B', [20, 30, 40, 50])
bar.set_global_opts(title_opts=opts.TitleOpts(title='Bar Chart'),
legend_opts=opts.LegendOpts(type_='scroll', pos_left='center',
orient='vertical', selected_mode='single'))
line.add_xaxis(['A', 'B', 'C', 'D'])
line.add_yaxis('Series A', [20, 30, 40, 50])
line.add_yaxis('Series B', [30, 40, 50, 60])
line.set_global_opts(title_opts=opts.TitleOpts(title='Line Chart'),
legend_opts=opts.LegendOpts(type_='scroll', pos_left='center',
orient='vertical', selected_mode='multiple'))
scatter.add_xaxis(['A', 'B', 'C', 'D'])
scatter.add_yaxis('Series A', [30, 40, 50, 60])
scatter.add_yaxis('Series B', [40, 50, 60, 70])
scatter.set_global_opts(title_opts=opts.TitleOpts(title='Scatter Chart'),
legend_opts=opts.LegendOpts(type_='scroll', pos_left='center',
orient='vertical', selected_mode='multiple',
selector_label_opts=opts.LabelOpts(font_size=12,
color='blue')),
toolbox_opts=opts.ToolboxOpts(is_show=True, feature={'saveAsImage': {}}))
# 设置Legend的渲染器
js_code = """
var legend_data = params.legendData;
var selected = {};
for (var i = 0; i < legend_data.length; i++) {
selected[legend_data[i]] = false;
}
selected[params.name] = true;
return {
selected: selected
};
"""
bar_legend_renderer = JsCode(js_code)
bar.reversal_axis()
bar.set_series_opts(label_opts=opts.LabelOpts(position='right', font_size=10),
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_='max', name='最大值'),
opts.MarkPointItem(type_='min', name='最小值')]),
emphasis_textcolor='red', legend_opts=opts.LegendOpts(type_='scroll', pos_left='right',
selector_label_opts=opts.LabelOpts(color='red'),
selected_mode='single',
legend_icon='rect'), )
scatter.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{c}'), areastyle_opts=opts.AreaStyleOpts(opacity=0.1),
# animation=False,
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_='average', name='平均值')]),
itemstyle_opts=opts.ItemStyleOpts(color='green'))
scatter_legend_renderer = JsCode(js_code)
scatter.set_global_opts(visualmap_opts=opts.VisualMapOpts(max_=70, min_=30, is_piecewise=True,
pieces=[{'min': 30, 'color': 'green'},
{'min': 50, 'color': 'red'}]),
legend_opts=opts.LegendOpts(type_='scroll', pos_left='right', legend_icon='rectangle',
selected_mode='multiple',
legend_selectedmode='single', legend_formatter='{name}'),
toolbox_opts=opts.ToolboxOpts(is_show=True,
feature={'dataZoom': {}, 'dataView': {}, 'magicType': {},
'restore': {}, 'saveAsImage': {}}),
tooltip_opts=opts.TooltipOpts(trigger='axis', axis_pointer_type='shadow'),
legend_selectedmode='single',
legend_selected=True,
legend_not_selected='Scatter2')
line_legend_renderer = JsCode(js_code)
line.set_series_opts(label_opts=opts.LabelOpts(is_show=True, color='red'),
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_='max', name='最大值'),
opts.MarkPointItem(type_='min', name='最小值')]),
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_='average', name='平均值')]),
markarea_opts=opts.MarkAreaOpts(is_silent=True))
line.set_global_opts(title_opts=opts.TitleOpts(title='Line Chart'), tooltip_opts=opts.TooltipOpts(trigger='axis', axis_pointer_type='shadow'),
legend_opts=opts.LegendOpts(type_='scroll', pos_left='right', legend_icon='roundRect',
selected_mode='multiple', legend_selectedmode='single', legend_formatter='{name}'),
datazoom_opts=opts.DataZoomOpts(), fill_color='blue')
bar_tooltip_formatter = JsCode("""
function (params) {
return params.seriesName + '<br/>' + params.name + ' : ' + params.value[1] + '<br/>' +params.marker+ params.data.mark + '<br/>';
}
""")
bar.set_series_opts(label_opts=opts.LabelOpts(position='right'), animation_delay=1000,
tooltip_opts=opts.TooltipOpts(is_show=True, trigger='axis', formatter=bar_tooltip_formatter))
# 将图表添加到Tab中
tab = Tab()
tab.add(bar, 'Bar Chart')
tab.add(line, 'Line Chart')
tab.add(scatter, 'Scatter Chart')
# 渲染并保存图表
tab.render('legend.html')
该示例中创建了一个Tab对象,并向其中添加一个柱状图、一个折线图和一个散点图。每个图表都有多个数据系列,通过Legend进行切换展示和隐藏。每种图表的Legend在特定选项卡中显示,用户可以选择单个数据系列或多个数据系列进行展示。最后将Tab渲染成html文件并保存。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:pyecharts的Tab和Legend布局详情 - Python技术站