柱状图饼图
- 简单柱状图:
-
- 叠加柱状图
- 柱状图反转
- slider-水平
- 柱状图标签过长解决
- markPoint效果
- 饼图
-
pip3 install --index-url https://pypi.douban.com/simple pyecharts
简单柱状图:
代码
from pyecharts.charts import Bar
bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
bar.render()
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
Bar()
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.add_yaxis("商家B", [6, 20, 38, 4, 70, 95])
.set_global_opts(title_opts=opts.TitleOpts(title="我的第一个图表", subtitle="这里是副标题"))
)
bar.render_notebook()
叠加柱状图
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
Bar()
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90],stack="stack1")
.add_yaxis("商家B", [6, 20, 38, 4, 70, 95],stack="stack1")
.set_global_opts(title_opts=opts.TitleOpts(title="我的第一个图表", subtitle="这里是副标题"))
)
bar.render_notebook()
柱状图反转
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
Bar()
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90],stack="stack1")
.add_yaxis("商家B", [6, 20, 38, 4, 70, 95],stack="stack1")
.set_global_opts(title_opts=opts.TitleOpts(title="我的第一个图表", subtitle="这里是副标题"))
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-翻转 XY 轴"))
)
bar.render_notebook()
slider-水平
import random
from pyecharts.charts import Bar
from pyecharts import options as opts
attr = ["{}天".format(i) for i in range(30)]
v1 = [random.randint(1, 30) for _ in range(30)]
bar = (
Bar()
.add_xaxis(attr)
.add_yaxis("",v1)
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-DataZoom(slider-水平)"),
datazoom_opts=opts.DataZoomOpts(),)
)
bar.render_notebook()
柱状图标签过长解决
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
Bar()
.add_xaxis(
[
"名字很长的X轴标签1",
"名字很长的X轴标签2",
"名字很长的X轴标签3",
"名字很长的X轴标签4",
"名字很长的X轴标签5",
"名字很长的X轴标签6",
]
)
.add_yaxis("商家A", [10, 20, 30, 40, 50, 40])
.add_yaxis("商家B", [20, 10, 40, 30, 40, 50])
.set_global_opts(
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),title_opts=opts.TitleOpts(title="Bar-旋转X轴标签", subtitle="解决标签名字过长的问题"),
)
)
bar.render_notebook()
markPoint效果
from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-MarkPoint(指定类型)"))
.set_series_opts(
label_opts=opts.LabelOpts(is_show=False),
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max", name="最大值"),
opts.MarkPointItem(type_="min", name="最小值"),
opts.MarkPointItem(type_="average", name="平均值"),
]
),
)
)
bar.render_notebook()
饼图
from pyecharts.charts import Pie
from pyecharts import options as opts
pie = (
Pie()
.add("",[list(z) for z in zip(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
[5, 20, 36, 10, 75, 90])])
.set_global_opts(title_opts=opts.TitleOpts(title="饼图基本示例", subtitle="这里是副标题"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
pie.render_notebook()
环形饼图
from pyecharts.charts import Pie
from pyecharts import options as opts
pie = (
Pie()
.add("",[list(z) for z in zip(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
[5, 20, 36, 10, 75, 90])],radius=["40%", "75%"])
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-Radius"),
legend_opts=opts.LegendOpts(orient="vertical", pos_top="2%", pos_left="15%"
),)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
pie.render( )
多饼图显示
pie = (
Pie()
.add(
"",
[list(z) for z in zip(["剧情", "其他"], [25, 75])],
center=["20%", "30%"],
radius=[60, 80],
label_opts=opts.LabelOpts(formatter="{b}: {d}%")
)
.add(
"",
[list(z) for z in zip(["奇幻", "其他"], [24, 76])],
center=["55%", "30%"],
radius=[60, 80],
label_opts=opts.LabelOpts(formatter="{b}: {d}%")
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Pie-多饼图基本示例"),
legend_opts=opts.LegendOpts(
type_="scroll", pos_top="20%", pos_left="80%", orient="vertical"
),
)
)
pie.render()
南丁格尔玫瑰图
def pie_rosetype() -> Pie:
v = ["aa","bb","dd","qq","rrr"]
vv = [10,40,20,15,25]
c = (
Pie()
.add(
"",
[list(z) for z in zip(v, vv)],
radius=[0, 100],
center=["20%", "40%"],
label_opts=opts.LabelOpts(is_show=False),
)
.add(
"",
[list(z) for z in zip(v, vv)],
radius=[0, 100],
center=["50%", "40%"],
rosetype="area",
)
.add(
"",
[list(z) for z in zip(v, vv)],
radius=[0, 100],
center=["80%", "40%"],
rosetype="radius",
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-玫瑰图示例"))
)
return c
pie_rosetype().render_notebook()