1、 美国1995年-2009年邮费变化折线图、阶梯图;
from pyecharts.charts import Line
from pyecharts import options as opts
line = Line()
line.add_xaxis(["1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2017", "2008", "2009"])
line.add_yaxis("邮费", [0.32, 0.32, 0.32, 0.32, 0.33, 0.33, 0.34, 0.37, 0.37, 0.37, 0.37, 0.39, 0.41, 0.42, 0.44]) # is_step=True 阶梯图
line.set_global_opts(title_opts=opts.TitleOpts(title="美国1995年-2009年邮费变化"))
line.render("折线图.html")
2、 2000年-2010年热狗大胃王比赛前三名成绩的堆叠柱形图、极坐标系-堆叠柱状图(南丁格尔玫瑰图);数据文件:hot-dog-places.csv
from pyecharts.charts import Bar
from pyecharts import options as opts
import csv
filename = "./hot-dog-places.csv"
data_x = []
data_y = []
with open(filename) as f:
reader = csv.reader(f)
for data_row in reader:
data_x.append(data_row)
x = data_x[0]
y1 = data_x[1]
y1 = [float(i) for i in y1]
y2 = [float(i) for i in data_x[2]]
y3 = [float(i) for i in data_x[3]]
bar = (
Bar()
.add_xaxis(x)
.add_yaxis("冠军", y1, stack="stack1")
.add_yaxis("亚军", y2, stack="stack1")
.add_yaxis("季军", y3, stack="stack1")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="热狗大胃王比赛成绩的堆叠柱形图"))
.render('堆叠柱形图.html')
)
极坐标系-堆叠柱状图(南丁格尔玫瑰图)使用Page中的DraggablePageLayout 布局,运行之后可以移动图像位置
from pyecharts import options as opts
from pyecharts.charts import Polar, Page
import csv
filename = "./hot-dog-places.csv"
data_x = []
data_y = []
with open(filename) as f:
reader = csv.reader(f)
for data_row in reader:
data_x.append(data_row)
x = data_x[0]
y1 = data_x[1]
y1 = [float(i) for i in y1]
y2 = [float(i) for i in data_x[2]]
y3 = [float(i) for i in data_x[3]]
def polar_datazoom_slider() -> Polar:
c = (
Polar(init_opts=opts.InitOpts(width="600px", height="500px"))
.add_schema(radiusaxis_opts=opts.RadiusAxisOpts(data=x),
angleaxis_opts=opts.AngleAxisOpts(is_clockwise=True))
.add("冠军", y1, type_="bar", stack="stack0")
.add("亚军", y2, type_="bar", stack="stack0")
.add("季军", y3, type_="bar", stack="stack0")
.set_global_opts(title_opts=opts.TitleOpts(title="极坐标系-堆叠柱状图例1"))
)
return c
def polar_datazoom() -> Polar:
c2 = (
Polar(init_opts=opts.InitOpts(width="600px", height="500px"))
.add_schema(angleaxis_opts=opts.AngleAxisOpts(data=x, is_clockwise=True))
.add("冠军", y1, type_="bar", stack="stack0")
.add("亚军", y2, type_="bar", stack="stack0")
.add("季军", y3, type_="bar", stack="stack0")
.set_global_opts(title_opts=opts.TitleOpts(title="极坐标系-堆叠柱状图例2"))
)
return c2
def page_draggable_layout():
page = Page(layout=Page.DraggablePageLayout)
page.add(
polar_datazoom_slider(),
polar_datazoom()
)
page.render("极坐标系-堆叠柱状图.html")
if __name__ == "__main__":
page_draggable_layout()
3、 某网站用户感兴趣的领域的投票结果绘制饼图、环形图;数据文件:vote_result.csv
感兴趣的领域,票数
金融,172
医疗保健,136
市场业,135
零售业,101
制造业,80
司法,68
工程与科学,50
保险业,29
其他,41
import pandas as pd
from pyecharts.charts import Pie
from pyecharts import options as opts
data = pd.read_csv('vote_result.csv')
x = data.iloc[:, 0]
y = data.iloc[:, 1]
c = (
Pie(init_opts=opts.InitOpts(width="1200px", height="460px"))
# 饼图用的数据格式是[(key1,value1),(key2,value2)],所以先使用了zip函数将二者进行组合
.add("", [list(z) for z in zip(x, y)])
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
.set_global_opts(
title_opts=opts.TitleOpts(title="用户感兴趣的领域的投票结果", subtitle="饼图"),
legend_opts=opts.LegendOpts(type_="scroll", pos_right="70%", orient="vertical")) # 配置图例
.render("饼图.html")
)
import pandas as pd
from pyecharts.charts import Pie
from pyecharts import options as opts
data = pd.read_csv('vote_result.csv')
x = data.iloc[:, 0]
y = data.iloc[:, 1]
c = (
Pie()
.add("", [list(z) for z in zip(x, y)], radius=["40%", "70%"]) # 半径大小
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
.set_global_opts(
title_opts=opts.TitleOpts(title="用户感兴趣的领域的投票结果", subtitle="饼图"),
legend_opts=opts.LegendOpts(pos_top="15%",pos_left="2%", orient="vertical"))
.render("环形图.html")
)
4、 奥巴马的政治举措民意调查结果的堆叠柱形图;数据文件:approval_rate.csv
政治举措,支持,反对,不发表意见
种族问题,52,38,10
教育,49,40,11
恐怖活动,48,45,7
能源政策,47,42,11
外交事务,44,48,8
环境,43,51,6
宗教政策,41,53,6
税收,41,54,5
医疗保健政策,40,57,3
经济,38,59,3
就业政策,36,57,7
贸易政策,31,64,5
外来移民,29,62,9
from pyecharts.charts import Bar
import pyecharts.options as opts
from pyecharts.globals import ThemeType # 用于设置主题
import pandas as pd
data = pd.read_csv('approval_rate.csv')
bar = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)) # InitOpts:初始化主题
# add_xaxis:加入x轴参数 stack——实现数据堆叠,同个类目轴上系列配置相同的stack值可以堆叠放置
.add_xaxis(data['政治举措'].tolist())
.add_yaxis("支持", data["支持"].tolist(), stack="stack1")
.add_yaxis("反对", data["反对"].tolist(), stack="stack1")
.add_yaxis("不发表意见", data["不发表意见"].tolist(), stack="stack1")
.set_global_opts(title_opts=opts.TitleOpts(title="奥巴马政治举措民意调查结果"))
.render("堆叠柱形图.html")
)
若有更多想要了解关于pyecharts,可点击[(http://pyecharts.org/#/zh-cn/intro)