from pyecharts import options as opts from pyecharts.charts import Pie min_count = 7000 # 将数量小于该值的类型合并 merged_data = [] other_count = 0 for name, count in zip(name_counts['event_name'], name_counts['count']): if count < min_count: other_count += count else: merged_data.append([name, count]) if other_count > 0: merged_data.append(['其他', other_count]) pie = ( Pie() .add( "", merged_data, radius=["40%", "55%"], label_opts=opts.LabelOpts( position="outside", formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c} {per|{d}%} ", background_color="#eee", border_color="#aaa", border_width=1, border_radius=4, rich={ "a": {"color": "#999", "lineHeight": 22, "align": "center"}, "abg": { "backgroundColor": "#e3e3e3", "width": "100%", "align": "right", "height": 10, "borderRadius": [4, 4, 0, 0], }, "hr": { "borderColor": "#aaa", "width": "100%", "borderWidth": 0.2, "height": 0, }, "b": {"fontSize": 16, "lineHeight": 33}, "per": { "color": "#eee", "backgroundColor": "#334455", "padding": [2, 4], "borderRadius": 2, }, }, ), ) .set_global_opts(title_opts=opts.TitleOpts(title="告警类型")) # .render("pie_rich_label.html") ) pie.render_notebook()修改代码使得标签和图分开,远一点
时间: 2023-07-24 15:13:59 浏览: 148
jilin.zip_PYECHARTS 吉林省_echarts 吉林_echarts 地图_jilin_pyecharts 吉林
你可以通过调整标签的位置来使标签和图之间距离远一点。以下是修改代码的示例:
```python
from pyecharts import options as opts
from pyecharts.charts import Pie
min_count = 7000 # 将数量小于该值的类型合并
merged_data = []
other_count = 0
for name, count in zip(name_counts['event_name'], name_counts['count']):
if count < min_count:
other_count += count
else:
merged_data.append([name, count])
if other_count > 0:
merged_data.append(['其他', other_count])
pie = (
Pie()
.add(
"",
merged_data,
radius=["40%", "55%"],
label_opts=opts.LabelOpts(
position="outside", # 将标签位置设为外部
formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c} {per|{d}%} ",
background_color="#eee",
border_color="#aaa",
border_width=1,
border_radius=4,
rich={
"a": {"color": "#999", "lineHeight": 22, "align": "center"},
"abg": {
"backgroundColor": "#e3e3e3",
"width": "100%",
"align": "right",
"height": 10,
"borderRadius": [4, 4, 0, 0],
},
"hr": {
"borderColor": "#aaa",
"width": "100%",
"borderWidth": 0.2,
"height": 0,
},
"b": {"fontSize": 16, "lineHeight": 33},
"per": {
"color": "#eee",
"backgroundColor": "#334455",
"padding": [2, 4],
"borderRadius": 2,
},
},
),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="告警类型"),
legend_opts=opts.LegendOpts(pos_left="center", pos_top="bottom"), # 调整图例位置
)
# .render("pie_rich_label.html")
)
pie.render_notebook()
```
通过将标签的位置设为"outside",标签会显示在图形的外部。通过调整图例的位置,可以将图例放在图形的底部中央,与标签分开并且距离较远。你可以根据需要进一步调整标签和图之间的距离,以及图例的位置。
阅读全文