python中pyecharts繪製餅圖

pyecharts包繪製餅圖需要調用Pie

from pyecharts import Pie

Pie.add()方法簽名:

add(name, attr, value,
    radius=None,
    center=None,
    rosetype=None, **kwargs)
  • name -> str
    圖例名稱
  • attr -> list
    屬性名稱
  • value -> list
    屬性所對應的值
  • radius -> list
    餅圖的半徑,數組的第一項是內半徑,第二項是外半徑,默認爲 [0, 75]
    默認設置成百分比,相對於容器高寬中較小的一項的一半
  • center -> list
    餅圖的中心(圓心)座標,數組的第一項是橫座標,第二項是縱座標,默認爲 [50, 50]
    默認設置成百分比,設置成百分比時第一項是相對於容器寬度,第二項是相對於容器高度
  • rosetype -> str
    是否展示成南丁格爾圖,通過半徑區分數據大小,有'radius'和'area'兩種模式。默認爲'radius'
    • radius:扇區圓心角展現數據的百分比,半徑展現數據的大小
    • area:所有扇區圓心角相同,僅通過半徑展現數據大小

1,基本餅圖:

from pyecharts import Pie


attr = ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
v1 = [11, 12, 13, 10, 10, 10]
pie = Pie("餅圖示例")
pie.add(
    "",
    attr,
    v1,
    is_label_show=True,
    is_more_utils=True
)
pie.render(path="Bing1.html")

結果Bing1.html

2,環狀餅圖示例:

from pyecharts import Pie


attr = ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
v1 = [11, 12, 13, 10, 10, 10]
pie = Pie("餅圖-圓環圖示例", title_pos='center')
pie.add(
    "",
    attr,
    v1,
    radius=[40, 75],
    label_text_color=None,
    is_label_show=True,
    is_more_utils=True,
    legend_orient="vertical",
    legend_pos="left",
)
pie.render(path="Bing2.html")

 結果Bing2.html

3, 餅圖和玫瑰圖示例:

from pyecharts import Pie


attr = ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
v1 = [11, 12, 13, 10, 10, 10]
v2 = [19, 21, 32, 20, 20, 33]
pie = Pie("餅圖-玫瑰圖示例", title_pos='center', width=900)
pie.add(
    "商品A",
    attr,
    v1,
    center=[25, 50],
    is_random=True,
    radius=[30, 75],
    rosetype="radius",
    is_label_show=True,
)
pie.add(
    "商品B",
    attr,
    v2,
    center=[75, 50],
    is_random=True,
    radius=[30, 75],
    rosetype="area",
    is_legend_show=False,
    is_label_show=True,
    # is_more_utils=True,
)
pie.render(path="Bing3.html")

結果Bing3.html

4,多個餅圖示例:

from pyecharts import Pie
from pyecharts import Style
# 否則會遇到錯誤NameError: name 'Style' is not defined

pie = Pie('各類電影中"好片"所佔的比例', "數據來着豆瓣", title_pos='center')
style = Style()
pie_style = style.add(
    label_pos="center",
    is_label_show=True,
    label_text_color=None
)

pie.add(
    "", ["劇情", ""], [25, 75], center=[10, 30], radius=[18, 24], **pie_style
)
pie.add(
    "", ["奇幻", ""], [24, 76], center=[30, 30], radius=[18, 24], **pie_style
)
pie.add(
    "", ["愛情", ""], [14, 86], center=[50, 30], radius=[18, 24], **pie_style
)
pie.add(
    "", ["驚悚", ""], [11, 89], center=[70, 30], radius=[18, 24], **pie_style
)
pie.add(
    "", ["冒險", ""], [27, 73], center=[90, 30], radius=[18, 24], **pie_style
)
pie.add(
    "", ["動作", ""], [15, 85], center=[10, 70], radius=[18, 24], **pie_style
)
pie.add(
    "", ["喜劇", ""], [54, 46], center=[30, 70], radius=[18, 24], **pie_style
)
pie.add(
    "", ["科幻", ""], [26, 74], center=[50, 70], radius=[18, 24], **pie_style
)
pie.add(
    "", ["懸疑", ""], [25, 75], center=[70, 70], radius=[18, 24], **pie_style
)
pie.add(
    "",
    ["犯罪", ""],
    [28, 72],
    center=[90, 70],
    radius=[18, 24],
    legend_top="center",
    **pie_style
)
pie.render(path="Bing4.html")

結果Bing4.html 

第一個【劇情】的legend位置比較突出,單獨一行,如何才能都放在一行??

參考:

http://pyecharts.org/#/zh-cn/charts_base?id=pie%EF%BC%88%E9%A5%BC%E5%9B%BE%EF%BC%89

https://cloud.tencent.com/developer/article/1330784

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章