Echarts is a data visualization tool open sourced by Baidu. With its good interactivity and exquisite chart design, it has been recognized by many developers, and Python needless to say. When Python encounters Echarts, it becomes PyEcharts.
PyEcharts and Matplotlib have similar functions, but there is a big difference. Its biggest advantage lies in its interactivity. Pyecharts can generate html format and display information dynamically with the movement of the mouse. This is something that Matplotlib can't do. It's cool.
This article discusses the pyecharts implementation of three basic visualization methods for typed variables.
Visualization of Categorical Variables-Histogram
# 数据预处理
import pandas as pd
data = pd.read_excel(r'.\pythonwd\blogortest\occupationcensus.xlsx')
data.head(10)
Out:
id 就业方向
0 0.0 读研
1 1.0 银行
2 2.0 证券
3 3.0 私企
4 4.0 未就业
5 5.0 证券
6 6.0 证券
7 7.0 证券
8 8.0 证券
9 9.0 未就业
count = data['就业方向'].value_counts() # pd.Series
job = list(count.index)
job_count = count.values.tolist()
print(job)
['私企', '读研', '银行', '证券', '事业单位', '自由职业', '出国', '未就业']
print(job_count)
[40, 29, 22, 20, 11, 10, 8, 7]
# 调用 Bar
from pyecharts.charts import Bar
##链式调用
bar = (
Bar()
.add_xaxis(job)
.add_yaxis('经济学院就业情况', job_count)
)
bar.render('bar.html')
Visualization of Categorical Variables-Pie Chart
from pyecharts.charts import Pie
pie = (
Pie()
.add("", [list(i) for i in zip(job,job_count)])
#.set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"])
#.set_global_opts(title_opts=opts.TitleOpts(title="Pie-设置颜色"))
# .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
.render("pie.html")
)
Visualization of Categorical Variables-Funnel Chart
# 漏斗图
from pyecharts import options as opts
from pyecharts.charts import Funnel
funel = (
Funnel()
.add("", [list(i) for i in zip(job,job_count)])
.set_global_opts(
title_opts=opts.TitleOpts(title="经济学院就业情况漏斗图"))
.render("funnel.html")
)
As you can see, using pyecharts visualization is very simple and clear, and it is a tool that must be mastered. (Here is just the basic usage, I will study the optimization of the graph later).