前言
之前采集了我爱我家的13000多条租房信息,准备做下数据分析,也了解到Pyecharts这个可视化工具,于是上手试了一番
pyecharts 是一个用于生成 Echarts 图表的类库。Echarts 是百度开源的一个数据可视化 JS 库。用 Echarts 生成的图可视化效果非常棒,pyecharts 是为了与 Python 进行对接,方便在 Python 中直接使用数据生成图
安装
直接在cmd命令行中输入
pip install pyecharts
对于版本问题,pyecharts使用unicode编码处理所有字符串和文件,你必须在Python 2上使用unicode字符串,而Python2.x不是默认的unicode编码,所以若你用的Python2.x,则需要在开头加上
#coding=utf-8
from future import unicode_literals
示例
下面我们一起来看一下如何使用吧
柱状图
from pyecharts import Bar
def demo1():
# 普通柱状图
bar = Bar("我的第一个图表", "这里是副标题")
# 可以定义图表的主题为暗色
bar.use_theme("dark")
# 通过add() 添加数据及配置 is_more_utils 提供更多工具按钮
bar.add("服装", ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"], [5, 20, 36, 10, 75, 90], is_more_utils=True)
# bar.print_echarts_options() # 该行只为了打印配置项,方便调试时使用
# 默认bar.render() 在当前工程目录下生成 render.html 文件,再用浏览器打开即可,支持path参数
bar.render("D:\\Python\\PycharmProject\\Wiwj\\render1.html")
print("============ demo1 ============")
render1
链式调用生成柱状图数据堆叠
from pyecharts import Bar
def demo2():
# 链式调用生成柱状图数据堆叠
clothes = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
clothes_v1 = [3, 15, 88, 34, 66, 22]
clothes_v2 = [55, 89, 99, 15, 17, 49]
(Bar("柱状图数据堆叠示例")
.add("商家1", clothes, clothes_v1, is_stack=True, is_more_utils=True)
.add("商家2", clothes, clothes_v2, is_stack=True, is_more_utils=True)
.render("D:\\Python\\PycharmProject\\Wiwj\\render2.html"))
print("============ demo2 ============")
render2.png
多图
from pyecharts import Bar, Line
from pyecharts.engine import create_default_environment
def demo3():
# 从 v0.4.0+ 开始,pyecharts 重构了渲染的内部逻辑,改善效率。推荐使用以下方式显示多个图表
bar = Bar("我的第三个图表", "这里是副标题")
bar.add("服装", ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"], [5, 20, 36, 10, 75, 90], is_more_utils=True)
line = Line("我的第三个图表", "这里是副标题")
line.add("服装", ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"], [5, 20, 36, 10, 75, 90], is_more_utils=True)
# 为渲染创建一个默认配置环境: 'html', 'svg', 'png', 'jpeg', 'gif' or 'pdf'
# 这里使用同一个引擎对象,减少了部分重复操作,速度有所提高
env = create_default_environment("html")
env.render_chart_to_file(bar, path='bar.html')
env.render_chart_to_file(line, path='line.html')
print("============ demo3 ============")
bar.png
line.png
numpy,pandas示例
import pandas as pd
import numpy as np
from pyecharts import Bar
def demo4():
# numpy pandas示例
title = "bar chart"
index = pd.date_range('3/8/', periods=6, freq='M')
df1 = pd.DataFrame(np.random.randn(6), index=index)
df2 = pd.DataFrame(np.random.randn(6), index=index)
dtvalue1 = [i[0] for i in df1.values]
dtvalue2 = [i[0] for i in df2.values]
_index = [i for i in df1.index.format()]
bar = Bar(title, "Profit and loss situation")
bar.add('profit', _index, dtvalue1)
bar.add('loss', _index, dtvalue2)
bar.render("D:\\Python\\PycharmProject\\Wiwj\\render4.html")
print("============ demo4 ============")
render4.png
带有最大最小以及平均线的柱形图
from pyecharts import Bar
def demo5():
# 带有最大最小以及平均线的柱形图
month = ["{}月".format(i) for i in range(1, 13)]
data1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
data2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
bar = Bar("柱状示例图")
bar.add("蒸发量", month, data1, mark_line=["average"], mark_point=["max", "min"])
bar.add("降水量", month, data2, mark_line=["average"], mark_point=["max", "min"])
bar.render("D:\\Python\\PycharmProject\\Wiwj\\render5.html")
print("============ demo5 ============")
render5.png
饼状图
from pyecharts import Pie
def demo6():
# 饼状图
food = ["手抓饼", "手抓饼加鸡蛋", "手抓饼加火腿", "手抓饼加培根", "手抓饼加里脊", "手抓饼加全部"]
shop1 = [5, 6, 7, 9, 8, 12]
shop2 = [5, 5.5, 7, 7, 7, 10]
pie = Pie("小摊手抓饼价格", title_pos="center", width=900)
pie.add("第一家店", food, shop1, center=[25, 50], is_random=True, radius=[30, 75], rosetype='radius')
pie.add("第二家店", food, shop2, center=[75, 50], is_random=True, radius=[30, 75], rosetype='area',
is_legend_show=False, is_label_show=True)
pie.render("D:\\Python\\PycharmProject\\Wiwj\\render6.html")
print("============ demo6 ============")
render6.png
折线图 + 柱状图
from pyecharts import Bar, Line, Overlap
def demo7():
# 折线图 + 柱状图
month = ["{}月".format(i) for i in range(7, 13)]
salary = [4300, 3300, 3850, 3880, 3680, 3880]
spending = [3300, 2600, 2200, 2300, 2840, 3000]
bar = Bar('月薪-开销示例')
bar.add("月薪", month, salary)
line = Line()
line.add("开销", month, spending)
# 创建一个交叠图
overlap = Overlap()
# 添加柱形图
overlap.add(bar)
overlap.add(line)
overlap.render("D:\\Python\\PycharmProject\\Wiwj\\render7.html")
print("============ demo7 ============")
render7.png
Bar3D(3D 柱状图)
from pyecharts import Bar3D
def demo8():
# Bar3D(3D 柱状图)
bar3d = Bar3D("3D 柱状图示例", width=1200, height=600)
x_axis = [
"12a", "1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a",
"12p", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p"
]
y_axis = [
"Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday", "Sunday"
]
data = [
[0, 0, 5], [0, 1, 1], [0, 2, 0], [0, 3, 0], [0, 4, 0], [0, 5, 0],
[0, 6, 0], [0, 7, 0], [0, 8, 0], [0, 9, 0], [0, 10, 0], [0, 11, 2],
[0, 12, 4], [0, 13, 1], [0, 14, 1], [0, 15, 3], [0, 16, 4], [0, 17, 6],
[0, 18, 4], [0, 19, 4], [0, 20, 3], [0, 21, 3], [0, 22, 2], [0, 23, 5],
[1, 0, 7], [1, 1, 0], [1, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0],
[1, 6, 0], [1, 7, 0], [1, 8, 0], [1, 9, 0], [1, 10, 5], [1, 11, 2],
[1, 12, 2], [1, 13, 6], [1, 14, 9], [1, 15, 11], [1, 16, 6], [1, 17, 7],
[1, 18, 8], [1, 19, 12], [1, 20, 5], [1, 21, 5], [1, 22, 7], [1, 23, 2],
[2, 0, 1], [2, 1, 1], [2, 2, 0], [2, 3, 0], [2, 4, 0], [2, 5, 0],
[2, 6, 0], [2, 7, 0], [2, 8, 0], [2, 9, 0], [2, 10, 3], [2, 11, 2],
[2, 12, 1], [2, 13, 9], [2, 14, 8], [2, 15, 10], [2, 16, 6], [2, 17, 5],
[2, 18, 5], [2, 19, 5], [2, 20, 7], [2, 21, 4], [2, 22, 2], [2, 23, 4],
[3, 0, 7], [3, 1, 3], [3, 2, 0], [3, 3, 0], [3, 4, 0], [3, 5, 0],
[3, 6, 0], [3, 7, 0], [3, 8, 1], [3, 9, 0], [3, 10, 5], [3, 11, 4],
[3, 12, 7], [3, 13, 14], [3, 14, 13], [3, 15, 12], [3, 16, 9], [3, 17, 5],
[3, 18, 5], [3, 19, 10], [3, 20, 6], [3, 21, 4], [3, 22, 4], [3, 23, 1],
[4, 0, 1], [4, 1, 3], [4, 2, 0], [4, 3, 0], [4, 4, 0], [4, 5, 1],
[4, 6, 0], [4, 7, 0], [4, 8, 0], [4, 9, 2], [4, 10, 4], [4, 11, 4],
[4, 12, 2], [4, 13, 4], [4, 14, 4], [4, 15, 14], [4, 16, 12], [4, 17, 1],
[4, 18, 8], [4, 19, 5], [4, 20, 3], [4, 21, 7], [4, 22, 3], [4, 23, 0],
[5, 0, 2], [5, 1, 1], [5, 2, 0], [5, 3, 3], [5, 4, 0], [5, 5, 0],
[5, 6, 0], [5, 7, 0], [5, 8, 2], [5, 9, 0], [5, 10, 4], [5, 11, 1],
[5, 12, 5], [5, 13, 10], [5, 14, 5], [5, 15, 7], [5, 16, 11], [5, 17, 6],
[5, 18, 0], [5, 19, 5], [5, 20, 3], [5, 21, 4], [5, 22, 2], [5, 23, 0],
[6, 0, 1], [6, 1, 0], [6, 2, 0], [6, 3, 0], [6, 4, 0], [6, 5, 0],
[6, 6, 0], [6, 7, 0], [6, 8, 0], [6, 9, 0], [6, 10, 1], [6, 11, 0],
[6, 12, 2], [6, 13, 1], [6, 14, 3], [6, 15, 4], [6, 16, 0], [6, 17, 0],
[6, 18, 0], [6, 19, 0], [6, 20, 1], [6, 21, 2], [6, 22, 2], [6, 23, 6]
]
range_color = ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf',
'#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
bar3d.add(
"",
x_axis,
y_axis,
[[d[1], d[0], d[2]] for d in data],
is_visualmap=True,
visual_range=[0, 20],
visual_range_color=range_color,
grid3d_width=200,
grid3d_depth=80,
)
bar3d.render("D:\\Python\\PycharmProject\\Wiwj\\render8.html")
print("============ demo8 ============")
render8.png
词云图
from pyecharts import WordCloud
def demo9():
# 词云图
name = [
'Sam S Club', 'Macys', 'Amy Schumer', 'Jurassic World', 'Charter Communications',
'Chick Fil A', 'Planet Fitness', 'Pitch Perfect', 'Express', 'Home', 'Johnny Depp',
'Lena Dunham', 'Lewis Hamilton', 'KXAN', 'Mary Ellen Mark', 'Farrah Abraham',
'Rita Ora', 'Serena Williams', 'NCAA baseball tournament', 'Point Break']
value = [
10000, 6181, 4386, 4055, 2467, 2244, 1898, 1484, 1112,
965, 847, 582, 555, 550, 462, 366, 360, 282, 273, 265]
wordcloud = WordCloud(width=1300, height=620)
wordcloud.add("", name, value, word_size_range=[20, 100])
wordcloud.render("D:\\Python\\PycharmProject\\Wiwj\\render9.1.html")
wordcloud = WordCloud(width=1300, height=620)
wordcloud.add("", name, value, word_size_range=[30, 100],
shape='diamond')
wordcloud.render("D:\\Python\\PycharmProject\\Wiwj\\render9.2.html")
print("============ demo9 ============")
render9.1.png
render9.2.png
以上仅仅是使用了一部分的图表,还有更多的样式可以使用,可以参考pyecharts的中文手册来练习
另外...圣诞快乐~
print('微信公众号搜索 "猿狮的单身日常" ,Java技术升级、虫师修炼,我们 不见不散!')
print('也可以扫下方二维码哦~')
猿狮的单身日常