from pyecharts.charts import Bar, Line, Pie, Map, Scatter from pyecharts import options as opts from pyecharts.charts import Grid # 数据准备 x_data = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] y_data1 = [820, 932, 901, 934, 1290, 1330, 1320] y_data2 = [50, 60, 70, 80, 90, 100, 110] y_data3 = [200, 300, 400, 500, 600, 700, 800] y_data4 = [400, 500, 600, 700, 800, 900, 1000] y_data5 = [300, 400, 500, 600, 700, 800, 900] y_data6 = [100, 200, 300, 400, 500, 600, 700] bar = ( Bar() .add_xaxis(x_data) .add_yaxis("销量", y_data1) .set_global_opts( title_opts=opts.TitleOpts(title="柱状图"), legend_opts=opts.LegendOpts(is_show=False), xaxis_opts=opts.AxisOpts(name="日期"), yaxis_opts=opts.AxisOpts(name="销量"), ) .set_series_opts(label_opts=opts.LabelOpts(formatter="{c}")) ) line = ( Line() .add_xaxis(x_data) .add_yaxis("访问量", y_data2, is_smooth=True) .set_global_opts( title_opts=opts.TitleOpts(title="折线图"), legend_opts=opts.LegendOpts(is_show=False), xaxis_opts=opts.AxisOpts(name="日期"), yaxis_opts=opts.AxisOpts( name="访问量", splitline_opts=opts.SplitLineOpts(is_show=True), axistick_opts=opts.AxisTickOpts(is_show=True), ), ) .set_series_opts(label_opts=opts.LabelOpts(formatter="{c}")) ) pie = ( Pie() .pie = Pie("客户性别比例", "好友总人数:350", title_pos="center") .pie.use_theme("dark") .pie.add('', attr, value, is_label_show=True, is_legend_show=True, legend_top='bottom') ) map_data = [("广东", 104), ("湖南", 71), ("北京", 50), ("上海", 50)] map = ( Map() .add("销量", map_data, "china") .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) .set_global_opts( title_opts=opts.TitleOpts(title="地图"), visualmap_opts=opts.VisualMapOpts(), ) ) custom = "" grid = ( Grid() .add(bar, grid_opts=opts.GridOpts(pos_left="5%", pos_top="5%", width="30%", height="40%")) .add(line, grid_opts=opts.GridOpts(pos_right="5%", pos_top="5%", width="30%", height="40%")) .add(pie, grid_opts=opts.GridOpts(pos_left="35%", pos_top="10%", width="30%", height="80%")) .add(map, grid_opts=opts.GridOpts(pos_right="5%", pos_bottom="5%", width="30%", height="40%")) .add_js_funcs(custom) ) grid.render("123.html") 为什么pie和map图位置都在网页最中间
时间: 2023-10-30 22:08:10 浏览: 139
在你的代码中,Pie 和 Map 图都没有加入到 Grid 中,而是分别调用了 Pie() 和 Map() 方法。
正确的写法应该是:
```python
pie = (
Pie()
.use_theme("dark")
.add(
"客户性别比例",
attr,
value,
is_label_show=True,
is_legend_show=True,
legend_top="bottom",
)
)
map_data = [("广东", 104), ("湖南", 71), ("北京", 50), ("上海", 50)]
map = (
Map()
.add("销量", map_data, "china")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="地图"),
visualmap_opts=opts.VisualMapOpts(),
)
)
grid = (
Grid()
.add(
bar,
grid_opts=opts.GridOpts(pos_left="5%", pos_top="5%", width="30%", height="40%"),
)
.add(
line,
grid_opts=opts.GridOpts(pos_right="5%", pos_top="5%", width="30%", height="40%"),
)
.add(
pie,
grid_opts=opts.GridOpts(pos_left="35%", pos_top="10%", width="30%", height="40%"),
)
.add(
map,
grid_opts=opts.GridOpts(pos_right="5%", pos_bottom="5%", width="30%", height="40%"),
)
.add_js_funcs(custom)
)
```
这样就可以正确地将 Pie 和 Map 图加入到 Grid 中,并且设置它们在网页中的位置了。
阅读全文