附上pyecharts中文文档链接http://pyecharts.org/#/zh-cn/intro
vote_result.csv链接:https://pan.baidu.com/s/1k3C0Y53j6mRJvwbN5EW8aw
提取码:azdg
hot-dog-places.csv链接:https://pan.baidu.com/s/1y9Qwikoj_qkcLJYFrmYCQA
提取码:ylln
#从pyecharts.charts导入绘制图表的类型
from pyecharts.charts import Line, Bar , Pie, Polar
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot # # 使用 snapshot-selenium 渲染图片
from pyecharts import options as opts
# 内置主题类型可查看 pyecharts.globals.ThemeType
from pyecharts.globals import ThemeType
绘制折线图的步骤:
1. 导入需要的库包
2. 创建数据或者导入数据文件
3. 绘制折线图
from pyecharts.charts import Line, Bar #从pyecharts.charts导入绘制图表的类型
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
from pyecharts import options as opts
from pyecharts.globals import ThemeType
v1 = ["1995", "1996", "1997", "1998", "1999",
"2000", "2001", "2002", "2003", "2004", "2005", "2006",
"2007", "2008", "2009"]
v2 = [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]
line = Line(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION))
line.set_global_opts(title_opts=opts.TitleOpts("折线图"))
line.add_xaxis(v1)
line.add_yaxis('邮费', v2)
line.set_global_opts(title_opts=opts.TitleOpts(title="邮费变化折线图"),
legend_opts=opts.LegendOpts())
bar = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.ROMANTIC))
.add_xaxis(v1)
.add_yaxis('柱形', yaxis_data=v2)
)
page = Page(layout=Page.DraggablePageLayout) # 避免 两个图对象 的数值冲突
make_snapshot(snapshot, line.overlap(bar).render('./资料/邮费变化折线图.html'), "./资料/line.png")
绘制阶梯图的步骤:
1. 导入需要的库包
2. 创建数据或者导入数据文件
3. 绘制阶梯图
from pyecharts.charts import Line, Bar, Page # 从pyecharts.charts导入绘制图表的类型
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
from pyecharts import options as opts
from pyecharts.globals import ThemeType
line1 = (
Line(init_opts=opts.InitOpts(ThemeType))
.add_xaxis(v1)
.add_yaxis("邮费", v2, is_step=True, is_symbol_show=True)
.set_global_opts(title_opts=opts.TitleOpts("邮费变化阶梯图"))
)
make_snapshot(snapshot, line1.render('./资料/阶梯图.html'), "./资料/阶梯图.png")
绘制堆叠柱状图的步骤:
1. 导入需要的库包
2. 创建数据或者导入数据文件
3. 绘制堆叠柱状图
from pyecharts.charts import Bar
from pyecharts import options as opts
import csv
from pyecharts.globals import ThemeType
data = pd.read_csv("./资料/hot-dog-places.csv")
datax = []
with open("./资料/hot-dog-places.csv") as f:
reader = csv.reader(f)
for datarow in reader:
datax.append(datarow)
x = datax[0]
y1 = datax[1]
y2 = datax[2]
y3 = datax[3]
bar = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.WESTEROS))
.add_xaxis(x)
.add_yaxis('A', y1, stack=True)
.add_yaxis('B', y2, stack=True)
.add_yaxis('C', y3, stack=True)
.set_global_opts(title_opts=opts.TitleOpts("堆叠柱状图"),
xaxis_opts=opts.AxisOpts(name="时间"))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_series_opts(label_opts=opts.LabelOpts(is_show=True, font_size=14))
)
bar.render("./资料/堆叠柱状图.html")
绘制极坐标系堆积柱状图的步骤:
1. 导入需要的库包
2. 创建数据或者导入数据文件
3. 绘制极坐标系堆积柱状图
from pyecharts.charts import Polar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
import csv
datax = []
with open("./资料/hot-dog-places.csv") as f:
reader = csv.reader(f)
for datarow in reader:
datax.append(datarow)
x = datax[0]
y1 = datax[1]
y2 = datax[2]
y3 = datax[3]
polar = (
Polar(init_opts=opts.InitOpts(theme=ThemeType.SHINE, height="700px", width="500px"))
.add_schema(
radiusaxis_opts=opts.RadiusAxisOpts(data=x, type_="category"),
# is_clockwise默认为逆时针,相当于fasle,True为顺时针方向
angleaxis_opts=opts.AngleAxisOpts(is_clockwise=True)
)
.add(series_name='A', data=y1, type_="bar", stack="stack0")
.add(series_name='B', data=y2, type_="bar", stack="stack0")
.add(series_name='C', data=y2, type_="bar", stack="stack0")
.set_global_opts(title_opts=opts.TitleOpts("极坐标系堆积柱状图"))
)
polar.render("./资料/极坐标系堆积柱状图.html")
绘制饼图的步骤:
1. 导入需要的库包
2. 创建数据或者导入数据文件
3. 绘制饼图
# 导入所需要的的库包
from pyecharts.charts import Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType
import pandas as pd
# 读取数据
data = pd.read_csv("./资料/vote_result.csv")
A = data.iloc[1:, 0]
B = data.iloc[1:, 1]
# 绘制饼图
pie = (
Pie(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE))
.add(series_name="", data_pair=[list(z) for z in zip(A, B)])
.set_global_opts(title_opts=opts.TitleOpts("饼图"))
)
pie.render("./资料/饼图.html")
绘制环形图的步骤:
1. 导入需要的库包
2. 创建数据或者导入数据文件
3. 绘制环形图
# 导入所需要的的库包
from pyecharts.charts import Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType
import pandas as pd
# 读取数据
data = pd.read_csv("./资料/vote_result.csv")
A = data.iloc[1:, 0]
B = data.iloc[1:, 1]
pie = (
Pie(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))
.add(series_name="", data_pair=[list(z) for z in zip(A, B)], radius=[40, 100])
.set_global_opts(title_opts=opts.TitleOpts("环形图"))
)
pie.render("./资料/环形图.html")